The start of a C program
 

  • A C program will start execution in the main( ) function

Your first C program: hello.c

The Hello World program in C:

 #include <stdio.h>

 int main( int argc, char* argv[] )            
 {
    printf( "Hello World !\n" );
 } 

 

DEMO: demo/C/set1/hello.c

 

Your first C program: hello.c
 

  • How to compile the hello.c program:

       gcc  hello.c    // generates executable file "a.out"  
      
       gcc -o hello hello.c // generate exec file "hello"
      

  • How to run the compiled (executable) code:

          a.out  // if compiled with gcc hello.c
       or
          hello  // if compiled with gcc -o hello hello.c  
      

Your first C program: hello.c

The #include compiler directive in C:

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {
      printf( "Hello World !\n" );
   } 

 

The #include <stdio.h> directive tells the C pre-processor to include the file stdio.h inside /usr/include folder

The file /usr/include/stdio.h is C's standard IO header file

This file contains constant and variable definitions to allow C programs to perform commonly used input/output operations.

Comment:   we will study the #include directive in more detail later...

The "standard I/O header file" stdio.h

  • The stdio.h file contains variables (= "objects") needed for terminal input/output operations

      • stdin = the standard input device (= the keyboard device)
        (In Java: System.in)

      • stdout = the standard output device (= the terminal)
        (In Java: System.out)

  • When a C program wants to perform I/O operations, it must include stdio.h at the start (= head) of the C program:

         #include <stdio.h>             
      

    (For this reason, include files are called "header" files)

  • Note:

    • The Java compiler always imports the java.lang package which contains the System class

Your first C program: hello.c

The Hello World program in C:

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {
      printf( "Hello World !\n" );
   } 

 

This line is the header of the definition of the main() function

 

Your first C program: hello.c

The Hello World program in C:

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {
      printf( "Hello World !\n" );
   } 

The printf( ) function prints the parameter string to the terminal (output)