Wednesday, 29 June 2016

How to write a C Program & what are Requirements ?

Creating the program

• A programmer uses a text editor to create or modify files                 containing C code
• The contents must obey C syntax
• The filename must end by convention “.c”
   Ex: sample.c
• Code is also known as source code
• A file containing source code is called a source file

Compiling the program

• There are many C compilers around.
• Sun compiler - cc
• GNU C - gcc
• Borland compiler, Turbo C compiler, Quick C compiler
• root@home> gcc –Wall sample.c
• Note: There may be a minor variations of
  library functions between different compilers

Running the sample program in Linux based Operating Systems

• If there are no errors in sample.c, this command produces an executable file in the same directory by name a.out
• root@home> ./a.out
• This executes your program, printing any results to the screen

Structure of C program

program header comment

preprocessor directives (if any)

int main ( )
{
statement(s)
return 0 ;
}

Description of hello.c 

  •  A C program starts execution with a function called main()‫‏‬
  • When a C program is started by the OS, a special startup routine is called before main(). 
  • This startup routine takes values from the OS (command line arguments and the environment) and sets things up so that main() is called first.
  • The startup routine also invoked exit() on return from main(). exit() releases all to reusable resources and then return to the kernel

Comments

  • A comment is descriptive text used to help a reader of the program understand its content.
  • All comments must begin with characters /*and end with the characters */.
  • These are called comment delimiters.
  • Nested comments are not allowed.

Preprocessor Directives


  • Lines that begin with a # are called preprocessor directives   
  • Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code.
  • This header file was included because it contains information about the printf ( ) function that is used in this program.

int main ()


  • Every program must have a function called main. This is where program execution begins.
  • The reserved word “int” indicates that main() returns an integer value.
  • The parentheses following the reserved word “main” indicate that it is a function

The Function Body


  • A left brace (curly bracket) -- { -- begins the body of every function.
  • A corresponding right brace -- } -- ends the function body

printf (“Hello, World!\n”) ;

  • This line is a C statement
  •  It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, World!\n”
  • Notice that this line ends with a semicolon
  • Note: All statements in C end with a semicolon

return 0

  •  Because function main() returns an integer value, there must be a statement that indicates what this value is.
  • The statement “return 0 ;” indicates that main() returns a value of zero to the operating system.
  • A value of 0 indicates that the program successfully terminated execution.
  • Don't worry about this concept now. Just remember to use the statement.



1 comment: