Short Overview of C
Short Overview of C
1
Function declaration ..................................................................................................................... 14
Function definition ........................................................................................................................ 14
Function call .................................................................................................................................. 14
Call by value .................................................................................................................................. 15
Call by reference ........................................................................................................................... 15
Local, Global and Static variable ................................................................................................... 15
Recursion....................................................................................................................................... 16
POINTER ............................................................................................................................................ 16
Pointer Arithmetic ......................................................................................................................... 17
Pointer to pointer .......................................................................................................................... 18
2
Short Overview of C
C is a powerful general purpose language which is known for its efficiency, flexibility, and
low-level features. It is developed by Dennis Ritchie in the early 1970s at Bell Labs.
1. Procedural Language : It follows top down approach to solve problems, focusing on
procedures or functions.
2. Low level feature: C provides low-level access to memory through pointers, allowing
manipulation of memory address. This feature makes C suitable for programming and
developing Operating Systems.
3. Efficiency: C is known for its efficiency and speed. It allows direct access to hardware,
making it suitable for developing embedded system and real-time application.
4. Portability: C program is highly portable, they can run on different hardware on minimal or
no modification due to availability of compilers and ANSI C standardization.
5. Structured Language : It supports structure programming constructs like functions, loops,
and conditional statements making it easy to organize and manage code.
6. Extensibility: C is highly extensible, allowing developers to build libraries with “.h”
extension an reuse code.
7. Standard Library: C comes with a standard library that provides functions for common
tasks like input/output operations, string manipulation, mathematical calculation, etc.
Comment Line
Comment line is used for increasing the readability of the program. It is useful in explaining
the program and generally used for documentation. It is enclosed within the decimeters.
Comment line can be single or multiple line but should not be nested. It can be anywhere in
the program except inside string constant & character constant.
//It is a single line comment
/*
It is a multiline (multiple line) comment.
*/
Preprocessor Directive:
Preprocessor directives in C are commands that alter source code before compilation. It
includes #include<library_name>, #define, #ifdef, etc.
3
Character set
A character denotes any alphabet, digit or special symbol used to represent information. The
alphabets, numbers and special symbols when properly combined form constants, variables
and keywords.
Alphabet A,B,…………,Y,Z
a, b, …………., y, z
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Identifiers
Identifiers are user defined word used to name of entities like variables, arrays, functions,
structures etc. Rules for naming identifiers are:
1) name should only consists of alphabets (both upper and lower case), digits and underscore
(_) sign.
2) first characters should be alphabet or underscore
3) name should not be a keyword
4) since C is a case sensitive, the upper case and lower case considered
differently, for example code, Code, CODE etc. are different identifiers.
5) identifiers are generally given in some meaningful name such as value, u_name, Age,
daTa etc.
Keyword
There are certain words reserved for doing specific task, these words are known as reserved
word or keywords. These words are predefined and always written in lower case or small
letter. These keywords can’t be used as a variable name as it assigned with fixed meaning.
There is 32 keywords exists in C.
auto break case char const continue default do double
else enum extern float for goto if inline int
long register restrict return short signed sizeof static struct
switch typedef union unsigned void volatile while
4
The keyword “inline” and “restrict” is introduced in C99.
Data types
Data types refer to an extensive system used for declaring variables or functions of different
types before its use. The type of a variable determines how much space it occupies in storage
and how the bit pattern stored is interpreted. The value of a variable can be changed any time.
C has the following 4 types of data types
basic built-in data types: int, float, double, char
Enumeration data type: enum
Derived data type: pointer, array, structure, union
Void data type: void
The size and range of the different data types on a 64 bit machine is given below:
int 4 0 to 4,294,967,295
short 2 0 to 65,535
long 8 0 to 18,446,744,073,709,551,615
char 1 0 to 255
Constants
Constant is a any value that cannot be changed during program execution. In C, any number,
single character, or character string is known as a constant. A constant is an entity that doesn’t
change whereas a variable is an entity that may change.
Variables
Variable is a data name which is used to store some data value or symbolic names for storing
program computations and results. The value of the variable can be change during the
5
execution. The rule for naming the variables is same as the naming identifier. Before used in
the program it must be declared. Declaration of variables specify its name, data types and
range of the value that variables can store depends upon its data types.
Syntax: datatype variableName;
Eg: int a; char b; float c;
Expressions
An expression is a combination of variables, constants, operators and function call.It can be
arithmetic, logical and relational for example:-
int z= x+y // arithmatic expression
a > b //relational
a == b // logical
func(a, b) // function call
Operator
This is a symbol use to perform some operation on variables, operands or with the constant.
Some operator required 2 operand to perform operation or Some required single operation.
Several operators are there those are, arithmetic operator, assignment, increment , decrement,
logical, conditional, comma, size of , bitwise and other
1. Arithmatic Operator
This operator used for numeric calculation. These are of either Unary arithmetic operator,
Binary arithmetic operator. Unary operator needs one operand to perform operation such as
++, --, !, and binary needs two operand and its operators are +(addition), -(subtraction),
*(multiplication), /(division), %(modulus).
2. Assignment Operator
A value can be stored in a variable with the use of assignment operator. The assignment
operator(=) is used in assignment statement and assignment expression. Operand on the left
hand side should be variable and the operand on the right hand side should be variable or
constant or any expression. When variable on the left hand side is occur on the right hand
side then we can avoid by writing the compound statement. For example,
int x= y;
int Sum=x+y+z;
6
3. Increment and Decrement
The Unary operator ++, --, is used as increment and decrement which acts upon single
operand. Increment operator increases the value of variable by one .Similarly decrement
operator decrease the value of the variable by one
4. Relational Operator
It is use to compared value of two expressions depending on their relation. Expression that
contain relational operator is called relational expression. Here the value is assign according
to true or false value. Eg: (a>=b) || (b>20), (b>a) && (e>b).
5. Conditional Operator
It sometimes called as ternary operator. Since it required three expressions as operand and it
is represented as (? , :).
Syntax
exp1 ? exp2 :exp3;
6. Bitwise Operator
Bitwise operator permit programmer to access and manipulate of data at bit level Various
bitwise operator enlisted are
one's complement (~)
bitwise AND (&)
bitwise OR (|)
bitwise XOR (^)
left shift (<<)
right shift (>>)
These operator can operate on integer and character value but not on float and double. In
bitwise operator the function showbits( ) function is used to display the binary representation
of any integer or character value.
7. Logical or Boolean Operator
Operator used with one or more operand and return either value zero (for false) or one (for
true). The operand may be constant, variables or expressions. And the expression that
combines two or more expressions is termed as logical expression. C has three logical
operators :
Operator Meaning
&& AND
|| OR
7
! NOT
Control Statement
Generally C program statement is executed in a order in which they appear in the program.
But sometimes we use decision making condition for execution only a part of program, that is
called control statement. Control statement defined how the control is transferred from one
part to the other part of the program. There are several control statement like if...else, switch,
while, do....while, for loop, break, continue, goto etc
if statement
Statement execute set of command like when condition is true and its syntax is
if (condition)
Statement;
The statement is executed only when condition is true. If the if statement body is consists of
several statement then better to use pair of curly braces. Here in case condition is false then
compiler skip the line within the if block.
if…..else Statement
it is bidirectional conditional control statement that contains one condition & two possible
action. Condition may be true or false, where non-zero value regarded as true & zero value
regarded as false. If condition are satisfy true, then a single or block of statement executed
otherwise another single or block of statement is executed.
Syntax:
if (condition)
{
//Block of statements
}
else
{
//Block of statements
}
Else statement cannot be used without if or no multiple else statement are allowed within one
if statement. It means there must be a if statement with in an else statement.
8
Nesting of if …else
When there are another if else statement in if-block or else-block, then it is called nesting of
if-else statement.
Syntax:
if (condition){
if (condition){
//Block of Statements;
}
else{
//Block of Statements;
}
}
If….else LADDER
In this type of nesting there is an if else statement in every else part except the last part. If
condition is false control pass to block where condition is again checked with its if statement.
Syntax:
if (condition1){
//Block of Statements;
}
else if (condition2)
//Block of Statements;
}
else if (condition3)
//Block of Statements;
}
else
//Block of Statements;
}
This process continue until there is no if statement in the last block. if one of the condition is
satisfy the condition other nested “else if” would not executed. But it has disadvantage over if
else statement that, in if else statement whenever the condition is true, other condition are not
checked. While in this case, all condition are checked
9
Loops in C
It is a block of statement that performs set of instructions. It is also called iteration. In loops
Repeating particular portion of the program either a specified number of time or until a
particular no of condition is being satisfied.
There are three types of loops in c
1.While loop
2.do..while loop
3.for loop
While loop:
The test condition may be any expression .when we want to do something a fixed no of times
but not known about the number of iteration, in a program then while loop is used.
Here first condition is checked if, it is true body of the loop is executed else, If
condition is false control will be come out of loop.
Syntax:
Initialization //Ex: int i = 0;
While( condition ){ //Ex: while( i < 10 )
//Block of statements
Increment/Decrement; //Eg: i++ or i--;
}
Do...While loop:
Here firstly statement inside body is executed then condition is checked. If the condition is
true again body of loop is executed and this process continue until the condition becomes
false. Unlike while loop semicolon is placed at the end of while.
There is minor difference between while and do while loop, while loop test thecondition
before executing any of the statement of loop. Whereas do while loop test condition after
having executed the statement at least one within the loop.
Syntax:
Initialization //Ex: int i = 0;
do{
//Block of statements
Increment/Decrement; //Eg: i++ or i--;
} While( condition ) //Ex: while( i < 10 )
10
for loop
In a program, for loop is generally used when number of iteration are known in advance. The
body of the loop can be single statement or multiple statements.
Syntax :
for( initialization; condition; increment/decrement){
//Block of codes
}
Nesting of loop
When a loop written inside the body of another loop then, it is known as nesting of loop. Any
type of loop can be nested in any type such as while, do while, for.
Break statement(break)
Sometimes it becomes necessary to come out of the loop even before loop condition becomes
false then break statement is used. Break statement is used inside loop and switch statements.
It cause immediate exit from that loop in which it appears and it is generally written with
condition. It is written with the keyword as break. When break statement is encountered loop
is terminated and control is transferred to the statement, immediately after loop or situation
where we want to jump out of the loop instantly without waiting to get back to conditional
state. When break is encountered inside any loop, control automatically passes to the first
statement after the loop. This break statement is usually associated with if statement.
Example:
for(int i = 0; i < 10; i++){
if(I > 5)
break;
}
Continue statement (continue)
Continue statement is used for continuing next iteration of loop after skipping some statement
of loop. When it encountered control automatically passes through the beginning of the loop.
It is usually associated with the if statement. It is useful when we want to continue the
program without executing any part of the program.
The difference between break and continue is, when the break encountered loop is terminated
and it transfer to the next statement and when continue is encounter control come back to the
beginning position.
Example:
11
for(int i = 0; i < 10; i++){
if(I == 5) continue;
}
ARRAY
Array is the collection of similar data types or homogenous data stored in contiguous memory
location. Array of character is a string. Each data item of an array is called an element. And
each element is unique and located in separated memory location. Each of elements of an
array share a variable but each element having different index no. known as subscript.
An array can be a single dimensional or multi-dimensional and number of subscripts
determines its dimension. And number of subscript is always starts with zero. One
dimensional array is known as vector and two dimensional arrays are known as matrix.
ADVANTAGES: array variable can store more than one value at a time where other variable
can store one value at a time.
Example:
int arr[100];
An int array occupies memory address from 2000 to size*4 to store value as:
arr[0] arr[1] arr[2] arr[3] and so
on...
10 20 30 40
2000 2004 2008 2012 and so
on…
12
It means the matrix consist of 2 rows and 3 columns
For example:-
20 2 7
8 3 15
Accessing 2-d array
For processing 2-d array, we use two nested for loops. The outer for loop corresponds to the
row and the inner for loop corresponds to the column.
Ex:
int arr2D [2][2] = {{0,0}, {2,2}};
for(int row = 0; row < 5; row++){
for(int column = 0; column < 5; column++)
printf(“%d \t”, arr[row][column]);
printf(“\n”);
}
FUNCTION
A function is a self contained block of codes or sub programs with a set of statements that
perform some specific task or coherent task when it is called.
Any ‘C’ program contain at least one function i.e main().
There are basically two types of function those are
1. Library function
2. User defined function
The user defined functions defined by the user according to its requirement
System defined function can’t be modified, it can only read and can be used. These function
are supplied with every C compiler
Source of these library function are pre complied and only object code get used by the user
by linking to the code by linker
14
Call by value
In the call by value copy of the actual argument is passed to the formal argument and the
operation is done on formal argument. When the function is called by ‘call by value’ method,
it doesn’t affect content of the actual argument. Changes made to formal argument are local
to block of called function so when the control back to calling function the changes made is
vanish.
Example:
main() {
int x = 5, y = 5;
add(x,y);
}
Call by reference
Instead of passing the value of variable, address or reference is passed and the function
operate on address of the variable rather than value. Here formal argument is alter to the
actual argument, it means formal arguments calls the actual arguments.
Example:
main() {
int x = 5, y = 5;
swap(&x, &y);
}
Local, Global and Static variable
Local variable: variables that are defined with in a body of function or block. The local
variables can be used only in that function or block in which they are declared.
Syntax:
main() {
int x = 5, y = 5;
}
Global variable: The variables that are defined outside of the function is called global
variable. All functions in the program can access and modify global variables.
Syntax:
int x ;
int main() {
15
x = 5;
}
int dummy(){
x= 6;
}
Static variables: static variables are declared by writing the key word static. -syntax:- static
data type variable name; static int a; -the static variables initialized only once and it retain
between the function call. If its variable is not initialized, then it is automatically initialized to
zero.
int dummy(){
static int x= 6;
}
Recursion
When function calls itself (inside function body) again and again then it is called as recursive
function. In recursion calling function and called function are same. It is powerful technique
of writing complicated algorithm in easiest way. According to recursion problem is defined in
term of itself. Here statement with in body of the function calls the same function and same
times it is called as circular definition. In other words recursion is the process of defining
something in form of itself.
Syntax :
Int main(){
main() //Calling Itself
}
POINTER
A pointer is a variable that store memory address or that contains address of another variable
where addresses are the location number always contains whole number. So, pointer contain
always the whole number. It is called pointer because it points to a particular location in
memory by storing address of that location.
Syntax:
dataType * pointer name;
When pointer declared, it contains garbage value i.e. it may point any value in the memory.
Two operators are used in the pointer i.e. address operator(&) and indirection operator or
dereference operator (*).
16
Indirection operator gives the values stored at a particular address.
Address operator cannot be used in any constant or any expression.
Pointer Expression
Pointer assignment
int i=10;
int *p=&i; //value assigning to the pointer
Here declaration tells the compiler that P will be used to store the address of
integer value or in other word P is a pointer to an integer and *p reads the value at
the address contain in p.
P++;
printf(“value of p=%d”);
We can assign value of 1 pointer variable to other when their base type and data type is same
or both the pointer points to the same variable as in the array.
Int *p1,*p2;
P1=&a[1];
P2=&a[3];
We can assign constant 0 to a pointer of any type for that symbolic constant ‘NULL’ is used
such as
*p=NULL;
It means pointer doesn’t point to any valid memory location.
Pointer Arithmetic
Pointer arithmetic is different from ordinary arithmetic and it is perform relative to the data
type(base type of a pointer).
Example:-
If integer pointer contain address of 2000 on incrementing we get address of 2004
instead of 2001, because, size of the integer is of 4 bytes.
Operation performed in pointer are:-
Addition
int i=100;
int *p;
p=&i;
p++;
p = p +2;
17
Subtraction
Ex:-
int i=22;
int *p1=&a;
p1--;
p1=p1-2;
Pointer to pointer
Addition of pointer variable stored in some other variable is called pointer to pointer variable.
Or
Pointer within another pointer is called pointer to pointer.
Syntax:-
Data type **p;
int x=22;
int *p=&x;
int **p1=&p;
18