Coupon Accepted Successfully!


Programming And Data Structures Programming In C


C Program File

All the C programs are writen into text files with extension “.c” for example hello.c. You can use “vi” editor to write your C program into a file.

C Compilers

When you write any program in C language then to run that program you need to compile that program using a C Compiler which converts your program into a language understandable by a computer. This is called machine language (ie. binary format).

Preprocessor Commands:
 These commands tells the compiler to do preprocessing before doing actual compilation. Like #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation.

 are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is returned using return statement.The C Programming language provides a set of built-in functions. In the above example printf() is a C built-in function which is used to print anything on the screen.

 are used to hold numbers, strings and complex data for manipulation.

Statements & Expressions : 
Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.

 are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the example above.

C - Reserved Keywords

The following names are reserved by the C language. Their meaning is already defined, and they cannot be re-defined to mean anything else.






































C Preprocessor, Directives and Header Files:

C Preprocessor :

As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file. One of the major functions of C preprocessor is Tokenizing. The final step of the preprocessor is to link the resulting program with necessary programs and library.

This directive is used for text substitution. Every occurrence of the identifier is substituted by the substitution_token. The primary advantage is that it increases the readability of the program.

# include directive:

This directive searches for a header or source file, which can be processed by the implementation, to be include in the program.

Header files:

Header files are a collection of macros, types, functions and constants. Any program that needs those functions can include the corresponding header files.

List of some commonly used Header file and their purposes:



Data Type

Data type determines the kind of data which is going to be stored in the variable declared to of that type. The biggest advantage of data types is efficient use of memory space.


C Variable types

The Programming language C has two main variable types

  • Local Variables
  • Global Variables

Local Variables

  • Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block.
  • When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
  • When execution of the block starts the variable is available, and when the block ends the variable ‘dies’.

Global Variables

Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it.

If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables and local variables with the same name.

C - Storage Classes

A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.

There are following storage classes which can be used in a C Program

  • auto
  • register
  • static
  • extern

Auto - Storage Class

Auto is the default storage class for all local variables.


int Count;

auto int Month;


The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.

egister - Storage Class

Register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary ‘&’ operator applied to it (as it does not have a memory location).


register int Miles;


Register should only be used for variables that require quick access - such as counters. It should also be noted that defining ‘register’ goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implementation restrictions.

tatic - Storage Class

Static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.

static int Count;

int Road;



printf(“%d\n”, Road);


static variables can be ‘seen’ within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.Static can also be defined within a function. If this is done the variable is initalised at run time but is not reinitalized when the function is called. This inside a function static variable retains its value during various calls.

Static variables are initialized to 0 automatically.

Definition vs Declaration : 
Before proceeding, let us understand the difference between defintion and declaration of a variable or function. Definition means where a variable or function is defined in realityand actual memory is allocated for variable or function. Declaration means just giving a reference of a variable and function. Through declaration we assure to the complier that this variable or function has been defined somewhere else in the program and will be provided at the time of linking. In the above examples char *func(void) has been put at the top which is a declaration of this function where as this function has been defined below to main() function.

Extern - Storage Class

Extern is used to give a reference of a global variable that is visible to ALL the program files. When you use ‘extern’ the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.

C - Using Constants

  • Octal constants are written with a leading zero - 015.
  • Hexadecimal constants are written with a leading 0x - 0x1ae.
  • Long constants are written with a trailing L - 890L.

‘\n’ newline

‘\t’ tab

‘\\’ backslash

‘\’’ single quote

‘\0’ null ( Usedautomatically to terminate character string )

Defining Constants

int const a = 1;

const int a =2;

The enum Data type

enum is the abbreviation for ENUMERATE, and we can use this keyword to declare and initialize a sequence of integer constants. Here’s an example:

enum colors {RED, YELLOW, GREEN, BLUE};

The default value for the first one in the list - RED in our case, has the value of 0. The rest of the undefined constants have a value 1 more than the one before, so in our case, YELLOW is 1, GREEN is 2 and BLUE is 3.

But you can assign values if you wanted to:

enum colors {RED=1, YELLOW, GREEN=6, BLUE };

Now RED=1, YELLOW=2, GREEN=6 and BLUE=7.

C - Operator Types


What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. C language supports following type of operators.

  • Arithmetic Operators
  • Logical (or Relational) Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

Arithmetic Operators:



Logical (or Relational) Operators:


Bitwise Operators:

Bitwise operator works on bits and perform bit by bit operation.

Assume if A = 60; and B = 13; Now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101


A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011


Assignment Operators:

There are following assignment operators supported by C language:


Misc Operators

There are few other operators supported by C Language.



Precedence of C Operators:


Operators Precedence in C++:


C - Flow Control Statements

C provides two styles of flow control:

  • Branching
  • Looping

Branching is deciding what actions to take and looping is deciding how many times to take a certain action.


Branching is so called because the program chooses to follow one branch or another.

if statement

If the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.

Test Your Skills Now!
Take a Quiz now
Reviewer Name