C Programs

Program on numbers

Program on string

Programs of arrays

Programs on pattern

Help others by sharing the content!

Flow of C Program

There are several steps involved from the stage of writing a C program to the stage of getting it executed.
All these steps of flow of c program are shown in following diagram,

flow of c program

Steps of flow of C program

  • You can observe from the figure that our program passes through several processors before it is ready to be executed.
  • As shown in figure, source code is a hand written code by programmer and it is stored with .c extension.
  • Then preprocessor replaces #include(files), #define(macro) etc by their respective values.
    This expanded source code gets stored in a file with .i extension.
  • When this expanded source code is compiled, it produce object code and this object code gets stored with .o extension.
  • After this object code is transfer to linker. Linker linked the object code of library functions.
  • The resultant executable code gets stored with .exe extension.

In this way, we are executing any program. Each program goes through these steps.

Help others by sharing the content!

C Macro

Macro expansion

Let see an example of macro:


Here preprocessor name of macro is SIZE which has already been defined before main( ).

This statement is called ‘macro definition’ or just ‘macro’.
Preprocessor replaces the name of macro i.e, SIZE with 5 with every occurrence of SIZE in the program.
These replace part will be given to the compiler for compilation before the source code passes to the compiler.
When compiler sees the #define, it goes through the entire program and replaces all the macro template with the appropriate macro expansion.

Lets see another example


  • Here, SIZE and X are called as ‘macro templates’, whereas, 5 and 5+5 are called their corresponding ‘macro expansions’.
  • A macro template and its macro expansion are separated by blanks or tabs.
  • A space between # and define is optional.
  • A macro definition is never to be terminated by a semicolon.
  • In C programming it is customary to use capital letters for macro template.
  • A #define directive could be used even to replace a condition.
  • A #define directive could be used to replace even an entire C statement.
  • A macro can be redefine.

Why use #define?

Using #define you can produce more efficient and more easily understandable programs.
Probably, we have made the program easier to read.
Suppose we use a value of SIZE is 5 in a program and it appears many times in your program.
This value may have to be changed.
Then you need to go through the program and manually change each occurrence of the SIZE.
Therefore, if you have defined SIZE in a #define directive, you only need to make one change, in the #define directive.

Difference between macro and function.

  • Usually macros make the program run faster but increase the program size, whereas functions make the program smaller and compact.
  • In function, execution time is more because passing arguments to a function and getting back the returned value does take time.
    This gets avoided with macros since they have already been
    expanded and placed in the source code before compilation.
  • In function data type checking is done, whereas in macro data type checking is not done.

Examples of #define:

  • Example of macro with arguments

    Here firstly, g(5,6) is replace by 5*6 i.e equals to 30.
    Then f(4,30) is replaced by 4+30 = 34.
    Therefore it prints 34 as output.
  • macros can be redefine.
  • The directives can be placed anywhere in a program.

    Here ASCII value of space i.e. 32 will print.
  • .

    a = RES(b+2)
    = b+2 * b+2
    = 3+2 * 3+2
    = 3 + 6 + 2
  • macro templates should have macro expansion.
  • If macro template is written inside ” ” then, macro template won’t be replace by macro expansion.
  • .

    ans = a*a*a = 5+2 * 5+2 * 5+2 = 5 + 10 + 10 + 2 = 27
  • .
Help others by sharing the content!

C Header Files

File Inclusion

  • The second preprocessor directive is file inclusion.
  • This directive causes one file to be included in another.
  • The general command for file inclusion is:

    C preprocessor
  • It is a command given by programmer to preprocessor to include header file in the source program.
  • This used when we have a very large program, the code is best divided into several different files, each containing a set of related functions.
  • There are some functions and some macro definitions that we need almost in all programs.
  • These commonly use functions can be stored in a file, and that file can be included in every program.

There are two ways to write #include statement.

  1. #include “filename”
  2. #include <filename>

Each header file ended with .h extension.
.h extension stands for ‘header file’ that contain function declarations which we can be used in our main C program.
The prototypes of all the library functions are grouped into different categories and then stored in different header files.

For example:

  • math.h – prototypes of all mathematics related functions are stored in the header file ‘math.h’.
  • conio.h – prototypes of console input/output functions are stored in the header file ‘conio.h’.
  • stdio.h – prototypes of printf(), scanf() functions are stored in the header file ‘stdio.h’.

It is also possible to create your own header files in which you can declare your own functions and use in your main program.
For example:

This line tells preprocessor to get myheader.h from the local directory and add the content to the current source file.
There are some rules for creating own header files.
It can be start with number also,

It is not allowed to use space in between name of header file,

following defination of header ia also not allowed.

Help others by sharing the content!

C Preprocessor

Defination of C preprocessor

  • The C preprocessor is not part of the comipler, but is a seperate step in compilation process.

  • Preprocessor is a program that processes source program before it is passed to the compiler.
  • The preprocessor offers several features called preprocessor directives.
  • Each of these preprocessor directives begin with a pound symbol(#) symbol.
  • The directives can be placed anywhere in a program but are most often placed at the beginning of a program, before the first function definition.

C preprocessor

We are going to learn the following preprocessor directives:

Following is a list of all important preprocessor directives:

    • #define – Substitutes a preprocessor macro.
    • #include – Inserts a particular header from another file.
    • #undef – Undefines a preprocessor macro.
    • #ifdef – Returns true if this macro is defined.
    • #ifndef – Returns true if this macro is not defined.
    • #if – Tests if a compiler time condition is true.
    • #endif – Ends preprocessor conditional.

Let us understand these features of preprocessor one by one.

Help others by sharing the content!

C program to count number of words in a string

Problem statement

Take a string from user and count number of words in a string. And then return count to user as an output.

Program to count words in string


Help others by sharing the content!

C program to count number of blank spaces in string

Problem statement

Take a string from user and count number of blank spaces in string. And then return count to user as an output.

Program to count blank spaces in string



In above program we take a string from user and store it in character array a. Then using while loop we fetch each location from string and compare with blank space. Therefore if pointer point to character having value zero then count will increase.

And when string will finish it return value of count which denotes number of blank spaces in string.

Help others by sharing the content!


ASCII stands for American Standard Code for Information Interchange
There are 256 different types of characters available in computers. Each character has their unique ASCII value.

The value range from 0 to 255. Out of the 256, first 128 are often called ASCII characters and the next 128 as Extended ASCII characters.
Most computers typically reserve 1 byte (8 bits) to represent a character in ASCII.

Following is a program to find ASCII value for each character.

Types of character Number of character
Capital letters 26
Small-case letters 26
Numbers 10
Special symbols 32
Control characters 34
Graphics characters 128

ASCII value of Capital letters

Value Character
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90

ASCII value of small-case letters

Value Character
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122

ASCII value of Digits

Value Character
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9

ASCII value of Symbols

Value Character
0 null
32 (space)
33 !
35 #
36 $
37 %
38 &
40 (
41 )
42 *
43 +
46 .
47 /
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
91 [
92 \
93 ]
94 ^
95 _
96 `
123 {
124 |
125 }
126 ~
Help others by sharing the content!

C Storage Classes

We all know variables have a data type, but they also have a storage class.
We have not mentioned storage classes yet because storage classes have defaults.
If we don’t specify the storage class of a variable in its declaration, the compiler will
assume a storage class depending on the context in which the variable is used. thus, variables have certain default storage classes.

We know that variables are stored in a memory location.
There are mainly two types of memory locations in a computer – Memory and CPU registers.
It is a variable storage class in which variable values are stored.
Each storage class decides the following things:

  • storage place: Where the variable would be stored.
  • default initial value: if initial value is not specifically assigned.
  • scope of the variable : where the value of a variable is accessible.
  • life time of a variable : how long varible is exist.

Types of storage classes in C

There are 4 types of storage classes available in C language. They are,

  1. Automatic storage class
  2. Register storage class
  3. Static storage class
  4. External storage class

Let’s see each storage classes one by one.

Automatic storage class

auto keyword is used to declare variable is of automatic stroge class.

For example: auto int x=10;

Storage place Memory
Default initial value Garbage value
Scope Local
Life time within the block in which it is defined.

Now lets see an example to understand how an automatic storage class variable is declared and what is the initial value if variable is not defined.

Example –


The variable is declared of automatic storage class by using auto keyword.
where 2074 and 654 are garbage values of i and j. So it is clear that if you do not define any value to variable then compiler will initialize it garbage value.

The Scope and life of an automatic variable are illustrated in the following program.


In above program the compiler treats the three a’s as totally different variables.
Because they are defined in different blocks.
Since the lifetime of automatic storage class is within the block where it is defined, the value of a is changed.

Register storage class

register keyword is used to declare variable is of register storage class.

For example: register int x=10;

Storage place CPU registers
Default initial value Garbage value
Scope Local
Life time within the block in which it is defined.

A value stored in a CPU register can always be accessed faster than the one that is stored in memory. But the number of CPU registers are limited. Therefore if space is not available in registers then the variable will automatically be treated as a auto variable.
We can not find the address of a register variable because register variables are represented by there names. eg – AX, BX, CX.


Static storage class

static keyword is used to declare variable is of static storage class.
Mainly, the static variable is used to count how many times a function has been called.
Reinitialisation of static variable is not allowed. Only one time value get initializsed

For example: static int x=10;

Storage place Memory
Default initial value Zero
Scope Local
Life time throuthout the program



The function show( ) gets called from main( ) thrice. Each time it increments the value of i and prints it.
Static variable doesn’t disappear when the function is no longer active. Their values persist.
If the control comes back to the same function again the static variables have the same values they had last time around. And it is not reinitialized.

External storage class

extern keyword is used to declare variable is of static storage class.
For example – extern int a=20;
External variables differ from another variable in their scope i.e. global.
As external variables scope is global, they are declared outside all functions.

Storage place Memory
Default initial value Zero
Scope Global
Life time across the file



Here, x and y both are global variables. Both variables are defined outside the all functions.

Which storage class is use when?

Rules for usage of different storage classes in different programming situation.

  • By default, if variable is not declared explicitly then it is of auto storage class.
  • Use register storage class for only those variables that are being used very often in a program. Because CPU registers are limited. Mainly register storage class is use as loop counters.
  • If you want the value of a variable to persist between different function calls, then use static storage class.
  • When you have multiple functions in the program that use a common variable, then use extern storage class.
    This would avoid the declaring a variable in each function and save the memory space. Because it is active throughout the life of the program.

Difference between the local and static variable

Local variable Static variable
Storage class Local variables are belong to automatic storage class Static variables are belong to static storage class
Storage Stack section Data section
Default value Garbage value Zero
Reinitialisation Local variable can be reinitialisation. Static variable can not br reinitialise
Scope Within the function throughout the program
Life time Lifetime of local variable is within the block. Lifetime of static variable is throughout the program.
Output 0 0 0 0 1 2

In above programs, there are two functions, main() and show().
show() gets called from main() three time. Each time it increments the value of x and prints it.
The difference between them is that when variable x is auto, it is re-initialized to zero.

On the other hand, if x is static, it is initialized to 0 only once. It is never initialized again.
In short, if the storage class is static then the statement static int x = 0 is executed only once, irrespective of how many times the same function is called.

Help others by sharing the content!

C Programs

Program on numbers

Program on string

Programs of arrays

Programs on pattern

Help others by sharing the content!