Monday, February 7, 2011

C Programming - Part 3

Structure of a ‘C’ program
This is the structure of program which shows how it looks like.

      pre-processor directives
      global declarations
      function prototypes
      main( )
      {
      local variables to function main ;
      statements associated with function main ;
      }
      f1( )
      {
      local variables to function f1 ;
      statements associated with function f1 ;
      }
      f2( )
      {
      local variables to function f2 ;
      statements associated with function f2 ;
      }


Structure of my first ‘C’ program

   /* This is
      a comment */
   // This is a one-line comment


   # include <stdio.h>     /* includes header files */
  int  main( )     /* Must have a main function. First function executed */
   {
   printf ("Hello World!");      /* stdio functions */
   }



After making this program we need to compile it.One of the compiler we are using is gcc compiler . gcc compiler is best compiler for C programs.The full form of gcc is GNU Compiler Collection .



To use this compiler try as follows:-


cmd prompt>gcc test.c -o test
The prompt we are using id linux prompt.
test.c is the name of the file where you have saved your program
test will be the name of our output compiled file i.e executable file or binary file.
we can run our program as
cmd prompt> ./test


if we are not giving the output file name then by default it creates the a.out output file .

Description of our basic program is as follows:-

#(hash)   is the Preprocessor directives and lines included in the code of our programs  are not program statements but directives for the preprocessor. These lines are always preceded by  (#). The preprocessor is executed before the actual compilation of code begins, therefore the preprocessor digests all these directives before any code is generated by the statements.
These preprocessor directives extend only across a single line of code. As soon as a newline character is found, the preprocessor directive is considered to be end. No semicolon (;) is expected at the end of a preprocessor directive.

stdio.h--  is the standard input ouput header file and in which all input functions      ( scanf ) and all ouput functions (printf) are included. We are including the description of printf function into our current program and also we are giving the parameter value in this printf function as
    printf("Hello world !");


int main ( ) {

int-- return type means , main function whose return value is integer . 
main ( )-- is the function name  .
( ) in the blank  braces we give  command line arguments  to the main function .


printf("Hello world ");---- printf is for printing the output value on the screen . 

this is the basic program description .We will start from initial  .Then we move forward . We will make it simple to understand .Also we should know that what is written in our program that what is the meaning of each particular keyword . Then only we will be able to get the C skills . So wait for the next step.

Thanks 
Er.Surender Sharma
linux Kernel Developer   

1 comment:

  1. now I have the clear picture of this language

    ReplyDelete