7. Why?
• Discovered a piece of C code in a book or the internet that solves your
problem
• Copied it into your program and somehow made it compile
• No testing whatsoever
• Program runs and gives you right output for your small set of conveniently
selected input
• Job done. Decided to comment your code later. Forgotten all about it.
• Three months later someone comes and tells you to change your code for
another input or someone comes and tells you that your code is not working.
• Open your source code and stare at it ….. trying to make sense of what you
did
• Delete everything and go back to search a book or the internet to solve
your problem
8. hello world
#include <stdio.h>
int main(void)
{ text file named hello.c
printf(“Hello Worldn”);
return 0;
}
$ gcc hello.c compile
$ a.out
Hello World execute
$
10. a simple C program
Instructs the preprocessor to
#include <stdio.h> add the contents of the header
file stdio.h into hello.c
int main(void)
next line
{
printf(“Hello Worldn”);
return 0;
}
11. a simple C program
Instructs the preprocessor to
#include <stdio.h> add the contents of the header
file stdio.h into hello.c
int main(void)
{
printf(“Hello Worldn”);
return 0;
}
Why does ‘stdio.h ‘have
angle brackets <>?
What is # include ?
What is stdio.h?
Where is it stored?
12. a simple C program
#include <stdio.h> Pre processor
int main(void)
directive
{
printf(“Hello Worldn”);
return 0;
}
• Instructions meant for the pre-processor
• Always being with a ‘#’ symbol
• #include puts every line in the file stdio.h into hello.c
• Other pre processor directive examples: #define, #ifdef
#pragma, etc.
13. a simple C program
#include <stdio.h> Pre processor
int main(void)
directive
{
printf(“Hello Worldn”);
return 0;
}
• < angle brackets > tells the #include directive to search for the file
stdio.h in the standard C header file location.
• The default standard C header files in a Unix machine is
/usr/header,
• We can use “ ” instead of < > to tell the include directive to first
search for the file in the same directory as your C file, then the
standard .
14. a simple C program
#include <stdio.h> Standard C headers
int main(void)
{
printf(“Hello Worldn”);
return 0;
}
• A file ending with ‘.h’ is known as a C header file.
• A set of header files are available by default with the C
programming language. These are known as standard C headers
• The standard C headers contains declarations of system functions
that allows you to invoke system calls and system libraries
15. a simple C program
#include <stdio.h> Standard C headers
int main(void)
{
printf(“Hello Worldn”);
return 0;
}
• stdio.h is one of the standard C headers that defines all standard
input and output functions
• In this program, we have an output function printf which is
declared in stdio.h
• stdio.h location is /usr/header/stdio.h
16. a simple C program
#include <stdio.h>
main is the first function called
int main(void) when you execute your
{ program.
printf(“Hello Worldn”); next line
return 0;
}
17. a simple C program
#include <stdio.h>
main is the first function called
int main(void) when you execute your
{ program.
printf(“Hello Worldn”); next line
return 0;
}
What is int main(void)? Who calls main() function?
Who decided the name ‘main()’?
Why can’t I write my own
function ‘start()’ ?
18. a simple C program
#include <stdio.h>
int main(void)
Main function
{
printf(“Hello Worldn”);
return 0;
}
• Functions are a set of C instructions enclosed under a particular name. Eg:
function
name
return type int add(int a, int b) parameters
{
int sum; instructions
sum = a + b;
return sum;
}
• Functions make our code readable
19. a simple C program
#include <stdio.h>
int main(void)
Main function
{
printf(“Hello Worldn”);
return 0;
}
•main() is a special function that is first called when a C program is executed. Every C
program must have only one main() function to execute.
• int main( void ) tells us that the main() function takes no parameters as input and
returns an integer as output
•The main function is called by runtime environment of an operating system (Eg: in
Unix, the program is executed by the shell interpreter. )
20. a simple C program
#include <stdio.h>
int main(void)
C Standard 99
{
printf(“Hello Worldn”);
return 0;
}
• C Standard 99 – is one of the many standards that put forward rules on how C
programming language should be designed on different platforms (like
Unix, Windows. Solaris, etc). This ensures a common functionality on all platforms
• C Standard 99 – an ISO defined standard states that a C program should have the any
one of the two main() function definitions int main(void)
int main(int argc, char* argv[])
21. a simple C program
#include <stdio.h>
int main(void)
{ printf function prints the
printf(“Hello Worldn”); characters “Hello World ”
return 0; to the standard output
} stream.
next line
22. a simple C program
#include <stdio.h>
int main(void)
{ printf function prints the
printf(“Hello Worldn”); characters “Hello World ”
return 0; to the standard output.
}
What do you mean
by ‘standard output
stream’?
23. a simple C program
#include <stdio.h>
Standard Streams
int main(void)
{
printf(“Hello Worldn”);
return 0;
}
• Standard output in C language is defined as the output to the terminal screen. ( in
Unix, it is the shell window where the program was executed)
• Along with standard output stream, C defines the standard input stream (keyboard)
and standard error stream(screen again)
• These are also defined in stdio.h as per C Standard 99 definitions
24. a simple C program
#include <stdio.h>
int main(void)
{
printf(“Hello Worldn”);
return 0; return the value 0.
}
25. a simple C program
#include <stdio.h>
int main(void)
{
printf(“Hello Worldn”);
return 0; return the value 0.
}
Who did we
just send ‘0’ to?
Why are we
returning ‘0’ ?
26. a simple C program
#include <stdio.h>
int main(void)
{
printf(“Hello Worldn”);
return 0; End of program
}
• The last line of main() function has a return 0; line where 0 is defined as a successful
execution in C Standard 99.
• It tells the runtime environment that the C program successfully executed all its
lines of code.
• The significance of this return value arises when the runtime environment executes
multiple C programs. It helps it keep track of how many programs successfully
executed.
27. compile
Run a program named
gcc to compile the file
hello.c
$ gcc hello.c Notice that another
file called a.out
is created in the
same folder
28. compile
Run a program named
gcc to compile the file
hello.c
$ gcc hello.c Notice that another
file called a.out
is created in the
same folder
What exactly do
you mean by
‘compile’?
What is gcc ?
Whats inside my
executable ‘a.out’ ?
29. compile
$ gcc hello.c Compile
• The process of converting a high level language (such as C language) instructions to a
low level language (such as machine language 1’s and 0’s) instructions
• Strictly speaking, the process of compiling does not yield an executable file… It is
only one of the many steps involved in creating it.
30. compile
$ gcc hello.c What really
Preprocessor (gcc) preprocessor to expand
happens is…
macros and includes header
hello.i files
Compiler (gcc) actual compilation of
preprocessed source code
hello.s to assembly language
Assembler (as) convert assembly language
into machine code and
generate an object file
hello.o
Linker (ld) linking of object file with
C run time libraries to
create an executable
a.out
31. compile
GNU Compiler
$ gcc hello.c
Collection
(GCC)
• This is a compiler system that is produced by the GNU (GNU Not Unix) Project.
• Originally named GNU C Compiler, later changed after the inclusion of languages like
C++, Ada, Fortran, Lisp, Java, Objective-C, Go, and many more.
• Its open source!!
• Website: http://gcc.gnu.org
32. compile
$ gcc hello.c a.out
• The executable that is the available after the entire compilation and linking process
• Its all 1’s and 0’s (binary) which only the machine can understand… you definitely
cannot
• All executables that are generated are always named a.out by default (can be
overridden with the –o flag in gcc )
• ‘a.out’ name comes from a really old file format for executable files. Nowadays all
executable files follow the ELF (Executable and Linkable Format)
33. execute
$ a.out Run a.out and see
Hello World the line “Hello World”
$ on the screen.
34. execute
$ a.out Run a.out and see
Hello World the line “Hello World”
$ on the screen.
Wow!! That looked easy
How did it all happen?
35. execute
$ a.out From Executable
Hello World
$ to a Process
pid
22134
Memory Operating a.out Hard
starting
System Disk
address
CPU
• a.out file is loaded by the operating system to memory (RAM) as an executing
process.
• Each process gets a unique id called process id (pid)
36. execute
$ a.out Memory Layout
Hello World of your C program
$
Command line
arguments, Environment
variables
Stack Segment
Now you know where the
‘segment’ in segmentation
fault comes from
Heap Segment
BSS Segment
Initialized Data Segment
Text Segment Memory Region
38. what next?
File I/O Data types
Standard & structures
C Library
Pointers & Memory
Allocation Debugging
Macro and Version
Pre Processor Management
Multi file
projects
39. the creator of C…
Dennis Ritchie
9th Sept 1941 – 12th Oct 2011
40. Choose a job you love,
and you will never have to work a day in your life
- Confucious
thank you