Tuesday, August 29, 2006

Overview of C

C program is collection of translation unit combined through linking. A translation unit is also called as source file, which is collection of declarations.

Declarations are of two types:

  1. Block Declaration
  2. Function

  1. Block Declaration
    1. Specifier

i. Storage Class Specifier

It tells the compiler how to store the variables. Types of Storage Class Specifiers are

i. Extern

Extern is used to specify that object is stored with external linkage else where in program.

Generally extern declarations are put into header files that are just included.

Scope – Global, Lifetime – Lifetime of the Application.

ii. Static

Lifetime – Lifetime of the Application.

Static variables with block or function scope are called as static local variables.

Static variables with file scope are called as static global variables (ie internal linkage).

iii. Auto

All local variables are by default “auto”.

iv. Register

It tells the compiler that access to the object should be made as fast as possible.

If CPU registers are not full and object is small, then they are stored in CPU registers.

Register specifier can only be applied to local variables and formal parameters (and not to global variables).

Also, you cannot use ‘&’ operator, since they can be stored in CPU registers (which is not addressable like memory).

ii. Typedef

You can define new name for existing data type by using ‘typedef’ keyword.

Eg. typedef float BALANCE;

BALANCE a1,a2;

iii. Type Specifier

· Simple type Specifier

a. Four Basic Datatype

char, int, float, double.

Size – char is 1 byte while size of int is word length of execution environment. (ie for DOS – 16 bit, Win98- 32 bit).

void is either explicitly declared as return type of function or is used to create generic pointers.

C does not supports Boolean data type but it supports the concept through integer type.

Nonzero – true

Zero – false.

b. Modifiers

signed, unsigned, long, short.

· Class / Enum Specifier

a. Class i.e. struct, union

Semicolon separated list

b. Enum

Comma separated list.

· CV Qualifier

a. const

It tells the compiler that value cannot be changed by the programmer ie can be used only as rvalue.

In addition to basic data types, arrays, structures, unions can also be defined as const.

Pointer & const (3 situations):

1. Pointer constant

int* const ptr

2. Object constant

const int* ptr

3. Both constant

const int* const ptr

b. volatile

It tells the compiler that value of the variables can be changed in ways that is not explicitly specified by the programmer. (eg. variable can hold system clock, or port. So it tells compiler not to optimize certain expression).

Situation where both const and volatile can be used:

Value can be changed by external conditions only eg. value of the port.

const volatile char* port = (const volatile char*)0x30;

    1. Initializer-Declarator List

Can be comma separated eg. int a,b,c;

It is divide into two parts:

i. Declarator

· Identifier

Name of identifier has to follow four restrictions:

i. First character should be character or underscore. Remaining characters can be character or underscore or digit.

ii. Identifier in C is case sensitive

iii. Name of identifier cannot be a keyword.

iv. Length of identifier name:

a. Internal identifier – 63 characters

b. External identifier – 31 characters.

There are 32 keywords in C (auto, extern, static, register; char, int, float, double, void; signed, unsigned, long, short; typedef, struct, union, enum; const, volatile; case, default, if, else, switch, for, while, do, return, goto, break, continue, sizeof).

New context dependent keyword are introduced by typedef, struct, union, enum.

· Pointer Operator Declarator

Similar to Qualifier.

v. Initializer

It can be initializer list or assignment expression.

  1. Function
    1. Function Declaration

Contains only Header (which contains formal parameters)

int fn(char , int *, char []);

    1. Function Definition

i. Header

ii. Body

Is sequence of statements ie. Compont Statement.

Other types of statement:

· Labeled Statement

identifier : statement

Used for switch-

a. case const_expression : statement

b. default : statement

· Selection Statement

a. Two Way Selection

i. if, Nested else if, if else if ladder

ii. ? alternative

b. Multiway Selection


(Generally has break after each case).

· Iteration statement

a. for

b. while

c. do while

· Jump Statement

a. return expression;

b. goto identifier;

c. break;

d. continue;

Difference between break and continue:

Break forces termination of loop and program control and resumes at next statement following the loop

Continue forces first statement of iteration to take place skipping all statements in between.

· Expression Statement

An expression is turned into a statement by placing a semicolon after it.

It completes any pending side effects and discards the expression value.

Null expression statement:

a; 3; ;

No value, no side effect and no compiler error


It always reduces to single value.

It consist of :

· Variables

Variable is a named location in memory that is used to hold a value that can be modified by the program.

Information for compiler:

a. Type Qualifier (const, volatile)

It tells compiler how variables can be accessed or modified.

b. Storage Class Specifier (static, auto, register, extern)

It tells compiler how to store variables.

· Operators

a. Assignment

Variable = expression;

Multiple assignment possible (a=b=c=5;)

Compound assignment:

a+=b; x%=4;

b. Arithmetic

+ - * / % ++ --

% yields remainder of integer division (therefore cannot be used with float).

c. Relational & Logical

6 relational operator < > <= >= == !=

3 logical operator &&(AND) ||(OR) !(NOT)

d. Ternary

exp1 ? exp2 : exp3

exp1 is evaluated. If exp1 is true, exp2 is evaluated else exp2 is evaluated and that becomes the value of expression

e. Bitwise

f. Other operator

Sizeof, pointer( * , &), array ( [] ), comma, ( . ->), Parenthesis

Precedence of Operator:

Parenthesis Highest

Unary (Postfix, Prefix)

Binary (A, S, R, B, L)



Comma Lowest


Only unary prefix, assignment and ternary are right associative. All other are left associative.

Other important points:

C Linkage:

  1. Internal Linkage – file scope objects declared static
  2. External Linkage – function and global variables
  3. No Linkage – Local variables (therefore are known only in block).

Declaration declares name and type of the object. Definition causes the storage to be allocated to the objects.

Generally, declaration of the object is also their definition. But if we precede the declaration with extern, we declare the variable without defining it. Therefore, a variable can have multiple declarations (using extern) but only one definition.

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.