Sie sind auf Seite 1von 19

Review Of C programming

INTRODUCTION
In 1972, the Dennis Ritchie at Bell Labs developed C language incorporating the best feature of both BCPL and B languages. WHY IS C LANGUAGE POPULAR? 1. C is a machine independent and highly portable language. 2. It is easy to learn. 3. C language allows manipulation of BITS, BYTES and ADDRESSES. 4. It has a rich set of operators to solve scientific and business applications. 5. User can create their own functions and add to C library. 6. It has a large library of functions.

C BASICS
From this section onwards we discuss Review of C programming, that you have learnt in your previous year. This chapter gives you an overview of C language and tells about various components of a C program, such as character set, keywords, constants, variables, data types, basic input-output statements, decision statements etc. The review of C programming section will definitely help you to brush up your C knowledge acquired so far.

C LANGUAGE COMPONENTS
The four main components of C language are: 1. The character set 2. Tokens a. Keywords b. Identifiers c. Constants d. Operators 3. Variables 4. Data types

Operators

An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on data stored in variables. The variables that are operated are termed as operands. C operators can be classified into the following types
1. 2. 3. 4. 5. 6. 7. 8. 9. Arithmetic operators Relational operators Logical operators Conditional operators (Ternary) Increment and decrement (Unary) Bitwise operators Shorthand operators Assignment operators Special operator +.-.*,/,% <,<=,>,>=,==,!= !, &&, || ?: ++,-!, &,|,~,^,<<,>> +=,-+,*=,/=,%= = sizeof, , (comma)

Variables A variable is an object or element that may take on any value of a specified type. Variables are nothing but identifiers, which are used to identify various programming elements. Each variable in reality represents the name of a memory location in which a value can be stored. They are used to denote constants, arrays, functions, structures, unions, files and may other such element used in a program.

Example: Here are some examples of valid variable names


Name years sum stud_name total num salary acc_no

Data types Data types indicate the type of data a variable can have. A data type requires us to know. a. Its representation in the program. b. Memory allocated for it. c. The range of values that can be used. d. The possible operations that can be performed. Data types are classified as i. Simple or Fundamental data types (int, float,double, char, long int , short int and unsigned) ii. Derived data types (Derived data include arrays, functions, pointers, references, constants, structures, unions and enumerations)

THE STRUCTURE OF A C PROGRAM


Every C program includes the following components : i ii iii iv v Preprocessor directives main( ) A pair of flower brackets { } Declarations and statements User created sub-programs or functions

Program: The following program illustrates the structure of a C program.


#include <stdio.h> #define PI 3.1415 main( ) { int float r; a,c;

printf (Enter the radius of a circle); scanf (%d, &r); a = PI * r * r; c = 2 * PI * r; printf ( Area = %f, a); printf (Circumference = %f,c); }

Console I/O functions

i. ii.

Formatted Console I/O Unformatted Console I/O

Formatted Console I/O


printf(format control string, argument list); Specifier %c %d %f %s %x %u %ld Meaning print a character print a signed decimal integer print signed float value print a string print hexadecimal integer print an unsigned integer print a signed long decimal integer

scanf(format control string, argument list)

Example

i. ii. iii.

scanf(%d,&n); scanf(%c, &ch); scanf(%s%s,name,address);

Unformatted Console I/O


Unformatted console I/O functions cannot control the format of reading and writing the data. All the unformatted console I/O functions are defined in stdio.h header file. i. character input functions a) getch( ) and getche( ) b) getchar( ) ii.character output functions a) putchar( ) iii. character string output functions a) gets( ) iv. character output functions a) puts( )

TYPES OF STATEMENTS
They are, i. ii. iii. iv. v. vi Null statement Expression statement Compound statement Selection statement Looping statement Jumping statement

Selection (conditional) statement


The selection statements allow to choose the set-of-instructions for execution depending upon an expression's truth value. if statement Format: if ( expression ) statement;

if-else statement Format: if (expression) statement1; else statement2;

nested-if statement Format: if (expression1) if (expression 2) statement else statement else if (expression 3) statement else statement

1; 2; 3; 4;

THE switch STATEMENT

switch(control expression) {

case case-label-1 :

statement-list-1; break; case case-label-2 : statement-list-2; break; ...... default : default block;

Looping statement
The concept of loops is fundamental to structured programming. A loop is a program construct that causes a statement to be executed again and again. The process of repeating the execution of a certain set of statements again and again is termed as looping. C has several such statements that can be used to form loops they are

i. ii. iii.

while statement do-while statement for statement

THE while STATEMENT while (test condition) { Statement 1; Statement 2; .. } Statement n+1;

THE do-while STATEMENT do { Statements 1; Statements 2; .. } while (test condition); Statements n+1;

THE

for

STATEMENT

for(Expression 1;Expression 2;Expression 3) { Statements 1; Statements 2; .. } Statements n+1; Where 1. Expression 1 represents the initialization expression. 2. Expression 2 represents the expression for the final condition. 3. Expression 3 represents the increment or decrement expression.