Translate

Labels

Thursday 20 December 2012

INTRODUCTION


Dennis ritchie at AT & T bell laboratories developed the C language three decades ago.C language is the general purpose language .it is efficient, flexible and portable language.c has wide diversity of operators and commands.c has been effectively utilized for the development of system software like, operating systems, compilers, text processor, and database management systems.C is well suited for developing applications programs too.c language is composed of the following basic types of elements:

  •  constants,
  • identifiers,
  • operators,
  • punctuation and
  •  keywords.


A SAMPLE C PROGRAM:

c programs are made up of functions.A program cannot be written without function.A function may be pre-defined or user-defined.The C program code is given below:

#include<stdio.h>   /*preprocessor statement*/
main()                       /*function header  statement*/ 
{
printf("hello");        /*function call statement*/   
}

here, main() is calling function and printf() is the called function.
After the execution the program will display the message"hello".main() function is the user defined one.When the C program runs the control is transferred to this function.
This is called as the program's entry point.The program has two function, one is the user-defined which is main()function and the other one is pre-defined which is printf()function
The first line in the program #include<stdio.h> is a preprocessor statement.#include is a preprocessor directive .Preprocessor is a software program that will expand the source code
while it is compiled.Stdio.h (standard input and output header file, the content of the pre-defined output and input function like printf() and scanf() etc..., 
The C compiler is able to recognize the pre-defined functions only because of their  declaration in the appropriate header files.

Statements
Each and every line of a C program can be considered as a statement. There are four types of 
statements.They are 

  • preprocessor statement,
  • function header statement,
  • declaration statement,
  • executable statement.


#include<stdio.h> -> preprocessor statement
main()                       ->function header statement 
{
int a,b,c;                   ->variable declaration statement 
int sub(int,int);       ->function declaration statement     
a=17;                         ->executable statement
}  

Input and output statements
As we have seen already,the function printf() is used to display the results on the standard 
output(screen).Actually,the first parameter of the printf() function is a string which is used to control the output and hence it can be called as"control string".This parameter is used to format the output for display and hence we also call  it as a "formatting string".
for example:
To print a value of integer datatype
int x; /*variable xis declared as integer*/
x=17;
printf("%d",x);
 In the above example,'%d' is used as a formatting character within the control string of printf() function to display the value of an integer.The control string of printf() function can take three types of characters.They are,

  • ordinary characters,
  • formatting characters,
  • escape sequence characters.


In case of printf("hello"); statement, the control string has ordinary characters only

FORMATTING SPECIFICATIONS
FORMATTING CHARACTERS DATA TYPE
%d                                 Int
%f                                   Float
%c                                 Char
%s                                 Char[]
%ld                                 Long int
%lf                                 Long float
ESCAPE SEQUENCE CHARACTERS

ESCAPE SEQUENCE NON-GRAPHIC CHARACTER
\a                 Bell
\b                 Back space
\n                 New line/ line feed
\t                 Horizontal tab
\v                 Vertical tab
\\                 Back slash
\’ or \”         Single/ double quotes
\o                 Octal number
\x                 Hexa decimal number
\0                 Null
\f                 Form feed
\r                 Carriage return


Input (scanf())
To read a value from the keyboard(standard input),the function scanf() is used.The protype of scanf() is similar to the protype of the printf().It can take the variable number of the parameter.To read a value of an integer variable is given below:

int x;
scanf("%d",&x);

while the scanf() function is being executed,the system waits for the user's input.The user has to provide data through keyboard .The data will be placed in the location of x only after the "enter " key is pressed on the keyboard The second parameter of the above scanf() function &x,which represents the address of x.& is the address of operator and when it is being used with a variable,it provides the address of the variable.     


No comments: