Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

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.



INTRODUCTION TO C PROGRAMMING LANGUAGE

C ? 


  • C is a programming language.
  • Developed at AT &T’s Bell Laboratories of USA in 1972.
  •  It was designed and written by Dennis Ritchie.
Versions of ‘C’
  • K & R C
  •  ANSI C
  •  ISO C
  •  ISO 9899
  •  C99
First Edition of ‘C’

  •  In 1978, the first edition of Kernighan and Ritchie's The C  Programming Language was published.
  •  It introduced the following features to the existing versions of  C
  •  structure data type
  •  long int data type
  •  unsigned int data type
  • The += operator

c programming
C Programming Tutorial-Dennis Ritchie

ANSI C

In 1989, C was first officially standardized by ANSI standards committees also included following new features
  1.  functions returning struct or union
  2.  void * data type
  3.  const qualifier to make an object Read Only
  4.  assignment for struct data types
ISO 9899
  •  The ANSI C standard, with a few minor modifications, was adopted as ISO standard number ISO 9899. 
  • The first ISO edition of this document was published in 1990(ISO 9899:1990)
  • ISO 9899:1999, which was published in 1999 is commonly referred to as "C99". 
  • It was adopted as an ANSI standard in March 2000.
New features in C99
  • Inline functions
  • Freeing of restrictions on the location of variable declarations 
  • The addition of several new data types, including long long int
  • An explicit Boolean data type
  • Official support for comments beginning with “// ” as in C++
  • Several new library functions and Header files