Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Full C language
Description: All detailed notes of C language with programs and examples

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


HI - TECH COMPUTERS
C Programming
The C Language is developed by Dennis Ritchie for creating
system applications that directly interact with the hardware
devices such as drivers, kernels, etc
...


Mother language

TE
CH

It can be defined by the following ways:
System programming language

Procedure-oriented programming language
Structured programming language
Mid-level programming language
1) C as a mother language

C language is considered as the mother language of all the
modern programming languages because most of the compilers,
JVMs, Kernels, etc
...

It

provides

-

programming languages follow C syntax, for example, C++, Java,
the

core

concepts

like

the

array,

strings,

functions, file handling, etc
...

2) C as a system programming language
A

system

programming

language

is

used

to

create

system

software
...
It is generally used to create hardware devices,
OS,

drivers,

kernels,

etc
...

It can't be used for internet programming like Java,
...


C - LANGUAGE

1

HI - TECH COMPUTERS
3) C as a procedural language
A

procedure

is

known

as

a

function,

method,

routine,

subroutine, etc
...

A procedural language breaks the program into functions, data
structures, etc
...


In

C,

variables

and

function

prototypes must be declared before being used
...
Structure means to break a program into
parts or blocks so that it may be easy to understand
...


It

makes

modify
...


C

-

language program is converted into assembly code, it supports
pointer arithmetic (low-level), but it is machine independent
(a feature of high-level)
...
e
...
It is machine dependent, fast to run
...

A High-Level language is not specific to one machine, i
...
,
machine independent
...

C Program

#include ...
S
...

Dennis Ritchie is known as the founder of the c language
...

operating

C

language

system
...


Let's see the programming languages that were developed before
C language
...
It provides many features that
are given below
...
Simple
2
...
Mid-level programming language
4
...
Memory Management
7
...
Pointers
9
...
Extensible
1) Simple
C

is

a

simple

language

TE
CH

5
...

Unlike

-

2) Machine Independent or Portable
assembly

different

language,

machines

with

c

programs

some

machine

can

be

executed

specific

on

changes
...


HI

3) Mid-level programming language
Although, C is intended to do low-level programming
...
It also supports the features of a high-level language
...

4) Structured programming language
C is a structured programming language in the sense that we
can break the program into parts using functions
...
Functions also provide code
reusability
...

6) Memory Management
It supports the feature of dynamic memory allocation
...

7) Speed
there

are

lesser

TE
CH

The compilation and execution time of C language is fast since
inbuilt

overhead
...
We can directly interact
with the memory by using the pointers
...

9) Recursion
In

C,

we

provides

can
code

call

the

function

reusability

for

within

every

the

function
...


It

Recursion

10) Extensible
C

language

-

enables us to use the approach of backtracking
...

First C Program

Before starting the abcd of C language, you need to learn how
to write, compile and run the first c program
...
h>
int main(){
printf("Hello C Language");
return 0;
}
C - LANGUAGE

5

HI - TECH COMPUTERS
#include ...
The printf() function is defined in stdio
...

int main() The main() function is the entry point of every
program in c language
...

return 0 The return 0 statement, returns execution status to
unsuccessful execution
...
The 0 value is used for successful execution and 1 for
How to compile and run the c program
By menu

Now click on the compile menu then compile sub menu to compile
the c program
...

By shortcut

Or, press ctrl+f9 keys compile and run the program directly
...


-

c program output
You can view the user screen any time by pressing the alt+f5
keys
...

What is a compilation?
The compilation is a process of converting the source code
into object code
...

The compiler checks the source code for the syntactical or
structural errors, and if the source code is error-free, then
it generates the object code
...
The compilation
process can be divided into four steps, i
...
, Pre-processing,
Compiling, Assembling, and Linking
...


The

preprocessor takes the preprocessor directive and interprets
it
...
h>, the directive is available in
the program, then the preprocessor interprets the directive
and replace this directive with the content of the 'stdio
...


TE
CH

The following are the phases through which our program passes
before being transformed into an executable form:
● Preprocessor
● Compiler
● Assembler
● Linker
Preprocessor

The source code is the code which is written in a text editor
and the source code file is given an extension "
...
This
source code is first passed to the preprocessor, and then the

-

preprocessor expands this code
...

Compiler

The code which is expanded by the preprocessor is passed to

HI

the compiler
...


Or

we

can

say

that

the

C

compiler

converts

the

pre-processed code into assembly code
...


The

name

of

the

object

file

generated

by

the

assembler is the same as the source file
...
obj,' and in UNIX, the extension is
'o'
...
c', then the
name of the object file would be 'hello
...


C - LANGUAGE

7

HI - TECH COMPUTERS
Linker
Mainly, all the programs written in C use library functions
...
lib'

(or

'
...
The main working of the linker is to combine the
object

code

program
...
It links the object code
of these files to our program
...
The
output of the linker is the executable file
...


In

DOS,

the

extension

of

the

executable file is '
...
out'
...

Let's understand through an example
...
c

HI

1
...
h>
2
...
{
4
...


return 0;

6
...
e
...
c, is passed to the
preprocessor,
code

into

and

the preprocessor converts the source

expanded

source

code
...
i
...
The extension of the assembly code would
be hello
...


TE
CH

● This assembly code is then sent to the assembler, which
converts the assembly code into object code
...


The

loader

will

then

load

the

executable file for the execution
...
Both functions are inbuilt library

functions, defined in stdio
...


-

printf() function

The printf() function is used for output
...


The syntax of printf() function is given below:

HI

printf("format string",argument_list);
The

format

string

can be %d (integer), %c (character), %s

(string), %f (float) etc
...
It reads the input
data from the console
...


C - LANGUAGE

9

HI - TECH COMPUTERS
#include ...

The

printf("cube

of

number

is:%d

",number*number*number)

statement prints the cube of number on the console
...

#include ...
It is used to
many times
...
Its value can be changed, and it can be reused
It is a way to represent memory location through symbol so
that it can be easily identified
...
The int, float, char are the data
types
...
8;
char c='A';

Rules for defining variables
A variable can have alphabets, digits, and underscore
...
It can't start with a digit
...

C - LANGUAGE

11

HI - TECH COMPUTERS
A variable name must not be any reserved word or keyword, e
...

int, float, etc
...

It must be declared at the start of the block
...


C - LANGUAGE

12

HI - TECH COMPUTERS
Global Variable
A variable that is declared outside the function or block is
called a global variable
...
It is available to all the functions
...

int value=20;//global variable
int x=10;//local variable
}
Static Variable

TE
CH

void function1(){

A variable that is declared with the static keyword is called
static variable
...

void function1(){
int x=10;//local variable

-

static int y=10;//static variable
x=x+1;
y=y+1;

printf("%d,%d",x,y);

HI

}
If you call this function many times, the local variable will
print the same value for each function call, e
...


But

the static variable will print the incremented

value in each function call, e
...
11, 12, 13 and so on
...
We can explicitly declare an
automatic variable using auto keyword
...


TE
CH

external variable
...
c
#include "myfile
...
h>
void printValue(){

printf("Global variable: %d", global_variable);
}

-

Data Types in C
A data type specifies the type of data that a variable can
store such as integer, floating, character, etc
...

Data Types

Basic Data Type

int, char, float, double

Derived Data Type

array, pointer, structure, union

Enumeration Data Type

enum

Void Data Type

void

HI

Types

C - LANGUAGE

14

HI - TECH COMPUTERS
Basic Data Types
The

basic

data

types

are

integer-based

and

floating-point

based
...

The memory size of the basic data types may change according
to 32 or 64-bit operating system
...
Its size is given according to
32-bit architecture
...
You cannot use it as a variable
name, constant name, etc
...


break

case

char

const

continue

default

do

double

else

enum

extern

float

for

goto

if

int

long

register

return

short

signed

sizeof

static

struct

switch

typedef

union

unsigned

void

volatile

while

HI

auto

-

A list of 32 keywords in the c language is given below:

C - LANGUAGE

16

HI - TECH COMPUTERS
C Identifiers
C

identifiers

example,

represent

variables,

the

name

functions,

in

the

arrays,

C

program,

structures,

for

unions,

labels, etc
...

If the identifier is not used in the external linkage, then it
is called as an internal identifier
...


We can say that an identifier is a collection of alphanumeric
characters that begins either with an alphabetical character
or

an

underscore,

programming

elements

structures,

unions,

characters

which
such

are

as

labels,

(uppercase

and

used

to

represent

various

variables,

functions,

arrays,

etc
...

is

a

total

of

63

alphanumerical

characters

-

There

represent the identifiers
...


is

simply

There

can

a

symbol

that

is

be

many

types

of

used

to

perform

operations

like

arithmetic, logical, bitwise, etc
...

● Arithmetic Operators

● Shift Operators
● Logical Operators
● Bitwise Operators

TE
CH

● Relational Operators

● Ternary or Conditional Operators
● Assignment Operator

Precedence of Operators in C

The precedence of operator species that which operator will be
evaluated

first

and

next
...


-

operator direction to be evaluated; it may be left to right or
Let's understand the precedence by the example given below:
1
...

The

precedence

and

associativity

of

C

operators

is

below:

C - LANGUAGE

18

given

HI - TECH COMPUTERS

Category

Operator

Associativity

Postfix

() [] ->
...
, on the given operands
...
are the
examples of arithmetic operators
...


(+)

Operator

used

to

add

two

given

operands
...

Syntax
C = A + B;

For example, there are two operands, 5 and 6, and we want to
the

sum

of

these

numbers
...

#include ...
h>
int main ()
{

// declare integer variables
int num1, num2, res;
// declare float data type number
float f1, f2, res1;
// declare double variables
double d1, d2, res2;
printf (" Enter two integer numbers: ");
scanf ("%d %d", &num1, &num2);
res = num1 + num2; // use + operator
C - LANGUAGE

20

HI - TECH COMPUTERS
printf (" Enter two float numbers: \n ");
scanf ("%f %f", &f1, &f2);
res1 = f1 + f2; // use + operator
printf (" Enter two double data type numbers: \n ");
scanf ("%lf %lf", &d1, &d2);
res2 = d1 + d2; // use + operator

TE
CH

printf (" The sum of two integer numbers: %d \n", res);
printf (" The sum of two float numbers: %f \n ", res1);
printf (" The sum of two double numbers: %lf", res2);
return 0;
}
Output

Enter two integer numbers: 25
35
Enter two float numbers:
79
...
79
Enter two double data type numbers:
234
...
341
The sum of two integer numbers: 60
The sum of two float numbers: 147
...
464000
Minus Operator

The minus operator is denoted by the minus (-) symbol
...


different

The

types,

data

such

as

type
int,

of

the

float,

given

number

double,

long

can

double,

etc
...


C - LANGUAGE

be

21

HI - TECH COMPUTERS
Syntax
C = A - B;
For example, there are two operands, 15 and 6, and we want
their subtraction result
...

/* program to subtract the two numbers using minus operator */
#include ...
h>

// declare integer variables
int num1, num2, res;

// declare float data type number
float f1, f2, res1;

// declare double variables
double d1, d2, res2;

printf (" Enter two integer numbers: ");

-

scanf ("%d %d", &num1, &num2);
res = num1 - num2; // use + operator
printf (" Enter two float numbers: \n ");

HI

scanf ("%f %f", &f1, &f2);
res1 = f1 - f2; // use + operator
printf (" Enter two double data type numbers: \n ");
scanf ("%lf %lf", &d1, &d2);
res2 = d1 - d2; // use + operator
printf

("

The

subtraction of two integer numbers: %d \n",

("

The

subtraction

res);
printf

of

two

float

numbers: %f \n ",

res1);
printf (" The subtraction of two double numbers: %lf", res2);
return 0;
}
C - LANGUAGE

22

HI - TECH COMPUTERS
Output
Enter two integer numbers: 56
11
Enter two float numbers:
78
...
45
Enter two double data type numbers:
1234
...
7149
The subtraction of two integer numbers: 45

The subtraction of two float numbers: 22
...
916200
Multiplication Operator

The multiplication operator is represented as an asterisk (*)
symbol, and it is used to return the product of n1 and n2
numbers
...


C = A * B;

HI

For example, there are two operands 15 and 6, and we want to
get their product
...

/* program to multiply two numbers using astrisk (*) operator
...
h>
#include ...
5
3
...
324
798
...
750000
C - LANGUAGE

24

%lf",

HI - TECH COMPUTERS
The multiplication of two double numbers: 187019
...


Using

division

operator (/), we can divide the int, float, double and long

Syntax
C = A / B;

TE
CH

data types variables
...
5 and 5
...
So, we use the '/' Operator
between the given numbers that return float data 5
...


/* program to divide the first number by second number using
division (/) operator
...
h>
#include ...
32
248
...
7691
687
...
862247
The division of two double numbers: 6
...

Syntax

C = A % B;

For example, there are two operands 25 and 4, and we want to
get

their

modulus

result
...

/*

program

to

return

the

remainder

of

two

numbers

modulus (%) operator
...
h>
#include ...


It

is

used

A

is

10,

to

increase the integer value by 1
...


C - LANGUAGE

27

HI - TECH COMPUTERS
/* program to demonstrate the increment (++) operator
...
h>
#include ...

Syntax

B = A--;
For

example,

decrement

suppose

operator

the

integer

decreases

the

value
operand

of

A

value

is
by

10,

the

1

that

returns 9
...
*/
#include ...
h>
int main ()
C - LANGUAGE

28

HI - TECH COMPUTERS
{
// declare integer variables
int num1, res;
printf (" Enter the positive number: ");
scanf ("%d", &num1);
res = num1--; // use decrement operator
printf (" The decremented value is: %d ", num1);

TE
CH

return 0;
}
Output

Enter the positive number: 10

HI

-

The decremented value is: 9

C - LANGUAGE

29

HI - TECH COMPUTERS
Relational Operator in C
Relational

Operators

are

the

operators

used

to

create

a

relationship and compare the values of two operands
...


Equal To Operator (==)

TE
CH

Types of Relational Operators

It is used to compare both operands and returns 1 if both are
equal or the same, and 0 represents the operands that are not
equal
...

#include ...
h>
int main ()

HI

{
int a = 5;

int b = 10;

// Use Equal To Operator

printf (" a == b : %d", (a == b));
if (a == b)
printf ("\n %d is equal to %d", a, b);
else
printf (" \n %d is not equal to %d", a, b);

C - LANGUAGE

30

HI - TECH COMPUTERS
int x = 5;
int y = 5;
// Use Equal To Operator
printf (" \n x == y : %d", (x == y));
if (a == b)
printf (" \n %d is equal to %d", x, y);
else
return 0;
}
Output
a == b : 0
5 is not equal to 10
x == y : 1
5 is not equal to 5

TE
CH

printf ("\n %d is not equal to %d", x, y);

-

Not Equal To Operator (!=)
The Not Equal To Operator is the opposite of the Equal To
Operator

and

is

represented as the (!=) operator
...

Syntax:

Opr1 != Opr2;

Let's create a simple program to use the Not equal to (!=)
operator for comparing the values of variable in C
...
h>
#include ...


C - LANGUAGE

32

HI - TECH COMPUTERS
Syntax:
A < B;
Here the operand A is less than operand B
...
h>
int main ()
{
int num1, num2;

TE
CH

compare the operand value in C
...
");

HI

}
else
{

printf (" \n The value of num2 is less than num1
...


C - LANGUAGE

33

HI - TECH COMPUTERS
Greater than Operator (>)
The operator checks the value of the left operand is greater
than the right operand, and if the statement is true, the
operator is said to be the Greater Than Operator
...

Let's create a program to use the greater than operator (>) to
compare the operand value in C
...
h>
int main ()
{
int num1, num2;

printf (" Enter the value of num1: ");

-

scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);

HI

// use greater than operator (>)
if (num1 > num2)

{

printf (" \n The value of num1 is greater than num2
...
");
}
return 0;
}

C - LANGUAGE

34

HI - TECH COMPUTERS
Output
Enter the value of num1: 20
Enter the value of num2: 30
The value of num2 is greater than num1
...

Syntax:
A <= B;

Here, operand A is less than or equal to operand B
...


Let's create a program to use the Less than Equal to operator

-

(<=) to compare the operand value in C
...
h>
int main ()
{

HI

int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use less than equal to operator (<=)
if (num1 < num2)
{
printf (" \n The value of num1 is less than num2
...
");
}
else
{
printf (" \n The value of num2 is less than num1
...

Greater than Equal To Operator (>=)
The

operator

checks

whether

the

left

operand's

value

is

greater than or equal to the right operand
...


-

is true, the operator is said to be the Greater than Equal to

Syntax:

HI

A >= B;
Here, operand A is greater than equal to the right operand B
...

Let's

create

a

program

to

use

the

Greater

than

Equal

operator to compare the operand value in C
...
h>
int main ()
{
int num1, num2;
printf (" Enter the value of num1: ");
C - LANGUAGE

36

To

HI - TECH COMPUTERS
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use greater than equal to operator (>=)
if (num1 > num2)
{

TE
CH

printf (" \n The value of num1 is greater than num2
...
");
}
else
{

printf (" \n The value of num2 is greater than num1
...


C - LANGUAGE

37

HI - TECH COMPUTERS
Assignment Operator in C
There

are

different

kinds

of

the

operators,

such

as

arithmetic, relational, bitwise, assignment, etc
...


The

assignment

operator

is

used

to

assign the value, variable and function to another variable
...


TE
CH

Example of the Assignment Operators:

A = 5; // use Assignment symbol to assign 5 to the operand A
B = A; // Assign operand A to the B

B = &A; // Assign the address of operand A to the variable B
A = 20 \ 10 * 2 + 5; // assign equation to the variable A
Assignment Operator in C

Simple Assignment Operator (=):

It is the operator used to assign the right side operand or

Syntax

-

variable to the left side variable
...


Program1
...
h>
#include ...

Syntax

HI

A += B;
Or

A = A + B;

Let's create a program to use the Plus and assign operator in
C
...
c
#include ...
h>
int main ()
{
// initialize variables
C - LANGUAGE

39

HI - TECH COMPUTERS
int n1, n2, c;
n1 = 5;
n2 = 10;
n2 += n1;
printf (" \n The value of n1: %d", n1);
printf (" \n The value of n2: %d", n2);

}
Output
The value of a: 5
The value of b: 15

TE
CH

return 0;

Subtract and Assign Operator (-=):

The operator is used to subtract the left operand with the
right operand and then assigns the result to the left operand
...

Program3
...
h>
#include ...

Syntax
A *= B;
Or
A = A * B;

Let's create a program to use the multiply and assign operator

-

(*=) in C
...
h>
#include ...

Syntax
A /= B;
Or
A = A / B;

Let's create a program to use the divide and assign operator
(/=) in C
...
c

-

#include ...
h>
int main ()
{

HI

// initialize variables
int n1, n2, c;
n1 = 5;

n2 = 10;

n2 /= n1; // Use divide and Equal operator (b = b / a)

printf (" \n The value of n1: %d", n1);
printf (" \n The value of n2: %d", n2);
return 0;
}
Output
The value of n1: 5
The value of n2: 2
C - LANGUAGE

42

HI - TECH COMPUTERS
Modulus and Assign Operator (%=):
An

operator

used

between

the

left

operand

and

the

right

operand divides the first number (n1) by the second number
(n2) and returns the remainder in the left operand
...

#include ...
h>
int main ()
{

// initialize variables

-

int n1, n2, c;
printf (" Enter the value of n1: ");

HI

scanf ("%d", &n1);
printf (" \n Enter the value of n2: ");
scanf ("%d", &n2);
n1 %= n2; // Use modulus and Equal operator (a = a % b)

printf (" \n The modulus value of n1: %d", n1);
return 0;
}
Output
Enter the value of n1: 23
Enter the value of n2: 5
The modulus value of n2: 3
C - LANGUAGE

43

HI - TECH COMPUTERS

Pre-increment and Post-increment Operator in C
Increment

operators

are

the

operator

of

the

C

programming

language used to increase the given variable's value by 1
...
On the other hand, the increment
operator can increase the given value by 1 after assigning the

TE
CH

variable
...

Pre-increment and Post-increment Operator in C
Properties of the Increment operator
There

are

some

properties

follows:

of

the

increment

operator,

as

The increment operator is used to increase the current value
of the variable by 1
...

It is the operator represented by the double plus (++) symbol
...


The

pre-increment operator is used to increment the value of an
operand by 1 before using it in the mathematical expression
...

Syntax
x = ++a;

C - LANGUAGE

44

HI - TECH COMPUTERS
In

the

above

syntax,

the

value

of

variable

'a'

is

first

incremented by 1 before using in the expression
...

#include ...
h>
int main ()

TE
CH

{

// declaration of the variables
int a = 7;
int b = 2

// print the value of the increment operator

printf (" Use the pre-increment operator " );
printf (" The value of a is %d ", a);
printf (" The value of b is %d ", b);
// use pre-increment operator

-

b = ++a;
printf (" After using the pre-increment operator ");
printf (" The value of a is %d ", a);
printf (" The value of b is %d ", b);

HI

return 0;
}

Output

Use the pre-increment operator
The value of a is 7
The value of b is 2
After using the pre-increment operator
The value of a is 8
The value of b is 10

C - LANGUAGE

45

the

HI - TECH COMPUTERS
Example

2:

Let's

create

another

program

to

use

the

pre-increment operator in mathematical expression
...
c
#include ...
h>
int main ()
{
int a, b, c, d, x;

TE
CH

// declare integer variables

// initialization of the variables
a = 5;
b = 7;
c = 12;
d = 15;
//

use

pre-increment

operator

in

the

mathematical

expression

-

x = ++a + ++b + ++c + ++d;
printf( " The value of x is: %d ", x);
// print the updated value of a, b, c, and d

HI

printf (" \n The updated value of a = %d, b = %d, c = %d and d
= %d ", a, b, c, d);
return 0;

}

Output

The value of x is: 43
The updated value of a = 6, b = 8, c = 13 and d = 16

C - LANGUAGE

46

HI - TECH COMPUTERS
Post-increment Operator
Post-increment is an increment operator, represented as the
double

plus

(a++)

symbol

followed

by

an

operator

'a'
...


In

other

words,

the

variable's

original value is used in the expression first, and then the

Syntax
x = a++;

TE
CH

post-increment operator updates the operand value by 1
...

Example

1:

Let's

create

a

simple

program

to

use

post-increment operator in C programming language
...
c

-

#include ...
h>
int main ()
{

HI

// declaration of the variables
int a = 7;
int b = 0;

// print the value of the increment operator
printf (" Before using the post-increment operator " );
printf (" \n The value of a is %d ", a);
printf (" \n The value of b is %d ", b);
// use post increment operator
b = a++;
printf (" \n\n After using the post-increment operator ");
printf (" \n The value of a is %d ", a);
C - LANGUAGE

47

the

HI - TECH COMPUTERS
printf (" \n The value of b is %d ", b);
return 0;
}
Output
Before using the post-increment operator
The value of a is 7
The value of b is 0

The value of a is 8
The value of b is 7
Example

2:

Let's

TE
CH

After using the post-increment operator

create

another

program

to

use

the

post-increment operator in mathematical expression
...
h>
#include ...

#include ...
h>
int main ()
{
int x, y, z, exp;

-

printf (" Enter the value of x: ");
scanf (" %d", &x);

printf (" \n Enter the value of y: ");
scanf (" %d", &y);

HI

printf (" \n Enter the value of z: ");
scanf (" %d", &z);
printf (" \n Before using the increment operator: ");
printf (" \n The original value of x: %d", x);
printf (" \n The original value of x: %d", y);
printf (" \n The original value of x: %d", z);
// use pre-increment and post-increment operator
exp = x++ + ++x + ++y + y++ + ++z;
printf (" \n\n After using the increment operator: ");
printf (" \n The result of the expression is: %d", exp);
printf (" \n The updated value of x: %d", x);
C - LANGUAGE

49

HI - TECH COMPUTERS
printf (" \n The updated value of y: %d", y);
printf (" \n The updated value of z: %d", z);
return 0;
}
Output

Enter the value of y: 12
Enter the value of z: 15

TE
CH

Enter the value of x: 7

Before using the increment operator:
The original value of x: 7

The original value of x: 12
The original value of x: 15

After using the increment operator:
The result of the expression is: 58

-

The updated value of x: 9
The updated value of y: 14

HI

The updated value of z: 16

C - LANGUAGE

50

HI - TECH COMPUTERS
Unary Operator in C
In this section, we will discuss the unary operator in the C
programming language
...

A unary operator is an operator used to operate on a single
to

return

a

new

value
...


In

Unary

Operator, operators have equal priority from right to left
side associativity
...

Unary Minus (-)
Unary Plus (+)
Increment (++)

HI

Decrement (--)
Logical Negation (!)
Address Operator (&)
Sizeof() operator
Unary Minus (-)

The Unary Minus operator is represented using the symbol (-)
...
It means it changes the positive
number

to

the

negative, and a negative number becomes the

positive number using the unary minus operator
...
h>
#include ...


int b = -(a); // use unary minus operator to change the value
int n1 = 20;
int n2 = -30;

printf (" The value of a: %d \n", a);
printf (" The value of b: %d \n", b);

-

printf (" The value of -n1: %d \n", -n1);
printf (" The value of -n2: %d ", -n2);
return 0;

HI

}
Output

The value of a: 5

The value of b: -5
The value of -n1: -20
The value of -n2: 30

C - LANGUAGE

52

HI - TECH COMPUTERS
Unary plus (+)
The unary plus operator is represented as the "+" symbol, and
it does not change to the operand value
...
h>
#include ...
The "++" symbol represents the operand's value is
by

1
...

Pre Increment: The pre-increment operator is represented as
(++a), which means the value of variable 'a' is increment by 1
before using operand to the expression
...

Post Increment: The (a++) symbol represents the post-increment
operator, which means the value of 'a' is incremented by 1
after

assigning

the

original

value

to

the

expression

or

For example:
x = 10;
A = x++;

TE
CH

another variable
...

#include ...
h>

-

int main ()
{

int x, y, a, b; // declare local variable
a = 10;

HI

x = ++a; // It shows pre increment operator
printf (" Pre Increment Operator");
// Here the value of x is increased by 1
...
", x);
printf (" \n The value of a is %d
...
", y);
C - LANGUAGE

54

HI - TECH COMPUTERS
// get updated value of b
printf (" \n The value of b is %d
...

The value of a is 11
...

The value of b is 21
...


The

is

Unary

opposite

decrement

to

the

unary

operator

is

-

represented by the double minus (--) symbol, and it is used to
decrease the operand value by 1 according to the decrement's
types
...


HI

Pre Decrement: The pre decrement operator is denoted as (--a)
symbol, meaning the operand value is decreased by 1 before
assigning to another variable or expression
...


C - LANGUAGE

55

HI - TECH COMPUTERS
Syntax
int post = a--;
#include ...
h>
int main ()
{
int x, y, a, b; // declare local variable

TE
CH

a = 10;

x = --a; // It shows pre decrement operator
printf (" Pre Decrement Operator");

// Here the value of x is decreased by 1
...
", x);
printf (" \n The value of a is %d
...
", y);
// get updated value of b

HI

printf (" \n The value of b is %d
...

The value of a is 9
...

C - LANGUAGE

56

HI - TECH COMPUTERS
The value of b is 19
...

Syntax

TE
CH

sizeof(data_variable);
#include ...
h>
int main ()
{

// declaration of different types of data variables
int x;
float y;
char ch;

//

use

-

double z;
sizeof()

operator and pass the different data type

variable to get their size
...
For example, if the operand is true, the logical
not operator (!) reverses and return false; if the operand is
false, the logical operator returns true
...
h>
int main ()
{

-

#include ...

Syntax
int a = 5;

int b = &a; // variable b hold the address of variable a
Example 7:
#include ...
h>

-

int main ()
{

// declare variables
int a = 10;

HI

int b;
// use addressof (&) operator to assign the address
b = &a;

printf (" The value of variable a is: %d", a);
printf (" \n The address of variable b is: %d", b);
return 0;
}
Output
The value of variable a is: 10
The address of variable b is: 6487704

C - LANGUAGE

(&)

variable

TE
CH

The

59

HI - TECH COMPUTERS
Logical operators
An

operator

can

be

performing

different

there

various

are

defined

as

a

symbol

that

is

used for

operations
...


TE
CH

In this article, we are discussing the logical operators and
their types, along with an example of each
...


Some

irrespective

programming

of

a

languages

support limited operators, so some of the logical operators
that we are discussing may or may not be supported by the
programming language you are using
...
They return Boolean values
...


These

operators

-

make

allow

the

evaluation

and

manipulation of specific bits within the integer
...


||

This operator returns true if at least one of the

(Logical

relational statements combined with || is true,

OR)

else it returns false
...


HI

Operators

operator

returns

true

if

all

relational

NOT)

C - LANGUAGE

60

HI - TECH COMPUTERS

&& (Logical AND)
The logical && operator doesn't check the second condition if
first condition is false
...


The

evaluation

in

logical

&&

expression is left to right
...
This

TE
CH

operators,

operator accepts two operands
...
So, we can see the
possible values of A && B in the below table
...

Here, we are discussing the four cases to show the possible
conditions of && operator and also to check the result where
it returns false or where it returns true
...


the

Programmers

parentheses
often

use

as
it

it

is

not

to

make

technically

the

logic

of

expression very clear
...
10 > 20 && 20 > 5
Statement 1 in the above line is 10 > 20 and statement 2 is 20
> 5
...
AND operator will give
the true value only when both statements are true
...

Case 2:
1
...

Statement 1 in the above line is true, so in this case, the
operator

will

further

check

the

second

statement,

but

the

false as a result
...
10 > 20 && 20 < 5

TE
CH

second statement is false so, the operator will give us the

In this case, both statements are false
...
Although it is similar to the
first case, the operator will not check the second statement
because the first statement is not true
...
10 < 20 && 20 > 5

-

In this case, we can see both statements are true, so the

HI

operator will give us the true as a result
...
It works opposite to the logical AND operator,
as it only gives false when both statements of the expression
are not true
...

The logical || operator doesn't check the second condition if
first condition is true
...


TE
CH

The logical || operator accepts two operands
...
We can see the possible values of A ||
B in the following table
...

Here, we are discussing the four cases to show the possible
conditions of || operator and also to check the result where
it returns false or where it returns true
...
10 < 20 || 20 < 5
Statement 1 in the above line is 10 < 20 and statement 2 is 20
< 5
...
As stated earlier, OR operator will
give

the

true

value

when

one

of

the

statements

of

expression is true
...
10 > 20 || 20 > 5
In this case, operator will again return true as a result
...

Case 3:
1
...
The first statement is true, and
after

checking

it,

the operator will not check the second

statement and gives us the true as a result
...
10 < 20 || 20 < 5

Here, both of the statements are false
...


C - LANGUAGE

64

HI - TECH COMPUTERS
! (Logical NOT operator)
This logical operator is represented as an exclamation sign
(!)
...

Suppose we have an operand A
...

!A

True

False

False

TE
CH

A

True

If the value of argument is false, NOT operator will return
true, and vice versa
...
! (12 > 20)

In this case, the value of argument is false, but as we are
using the NOT operator, we will get the true as a result
...

Case 2:

1
...

Logical AND Operator
The logical AND operator is represented as the '&&' double
ampersand
operands

symbol
...
Else it returns false or 0
...
Otherwise, the logical AND (&&) operator returns
0 (false)
...

both

the

conditions are true, the logical AND operator

TE
CH

If

returns Boolean value 1 or true
...

Truth table of the Logical AND (&&) operator
A

B

A && B

1

1

1

1

0

0

0

1

0

0

0

0

-

Program to demonstrate the Logical AND Operator in C
#include ...
h>

HI

int main ()
{

// declare variable
int n = 20;

// use Logical AND (&&) operator to check the condition

printf (" %d \n", (n == 20 && n >= 8)); // condition is true,
therefore it returns 1
printf (" %d \n", (n >= 1 && n >= 20));
printf (" %d \n", (n == 10 && n >= 0));
printf (" %d \n", (n >= 20 && n <= 40));

C - LANGUAGE

66

HI - TECH COMPUTERS
return 0;
}
Output
1
1
0

Program

to

find

the

operator
#include ...
h>
int main ()
{

TE
CH

1
largest

number

using

the

Logical

// declare integer type variable
int x, y, z;

-

printf (" Enter the first number: ");
scanf ("%d", &x);

printf (" Enter the second number: ");
scanf ("%d", &y);

HI

printf (" Enter the third number: ");
scanf ("%d", &z);

// use logical AND operator to validate the condition
if ( x>= y && x >= z )
{

printf (" %d is the largest number of all
...
", y);
}
C - LANGUAGE

67

AND

HI - TECH COMPUTERS
else
{
printf( " %d is the largest number of all
...

#include ...
h>

-

int main ()
{

// declare variable

HI

int age;
printf (" Enter the age: ");
scanf (" %d", &age); // get age
//

use

logical

AND

operator

to

check

more

than

condition

if ( age>= 13 && age <= 19)
{
printf (" %d is a teenager age
...
", age);
}
return 0;
}
Output

17 is a teenager age
...

Program

to

validate

defined range or not
...
h>
{
int num;

-

int main ()

printf (" Enter a number between 1 to 50: ");

HI

scanf (" %d", &num); //get the number
// use logical AND operator to check condition
if ( (num> 0 ) && (num<= 50))

{

printf (" The entered number is in the range 0 and 50
...
");
}
else
C - LANGUAGE

69

HI - TECH COMPUTERS
{
printf (" Please enter the number is in the defined range
...

2nd Run

Enter a number between 1 to 50:
51

The entered number is in the range 50 and 100
...


HI

Program to validate the username and password entered by the
user

is

correct

or

not

using

the

predefined

username

and

password
...
h>
#include ...
");
}
else
{

printf( " \n The user's credentials are incorrect
...

2nd execution
Enter the username: system
Enter the password: admin@1234
The user's credentials are incorrect
...
When we perform the
bitwise

operations,

then

it

is

also

known

as

bit-level

programming
...
It is
mainly used in numerical computations to make the calculations

We

have

different

TE
CH

faster
...
The following is the list of the bitwise
operators:
Operator

Meaning of operator

Bitwise AND operator

|

Bitwise OR operator

^

Bitwise exclusive OR operator

~

One's complement operator (unary operator)

<<

Left shift operator

>>

Right shift operator

-

&

Let's look at the truth table of the bitwise operators
...
Two integer operands are written on both sides of the (&)
operator
...

C - LANGUAGE

72

is

1;

HI - TECH COMPUTERS
For example,
We have two variables a and b
...
e
...
If the bit of both the
variables is 1 then the output would be 1, otherwise 0
...

#include ...
e
...
The values of 'a' and 'b' are 6 and 14 respectively
...
When we apply the AND operator between these two
variables,
a AND b = 0110 && 1110 = 0110
Output
C - LANGUAGE

73

HI - TECH COMPUTERS
Bitwise OR operator
The bitwise OR operator is represented by a single vertical
sign (|)
...
If the bit value of any of the operand is 1,
then the output would be 1, otherwise 0
...
e
...


HI

Let's understand the bitwise OR operator through a program
...
h>
int main()
{

int a=23,b=10;

// variable declarations

printf("The output of the Bitwise OR operator a|b is %d",a|b);
return 0;
}
Output

C - LANGUAGE

74

HI - TECH COMPUTERS
Bitwise exclusive OR operator
Bitwise exclusive OR operator is denoted by (^) symbol
...
If the corresponding bit of any of the operand is 1
then the output would be 1, otherwise 0
...


-

value of any of the operand is 1, then the output would be 1

Let's understand the bitwise exclusive OR operator through a

HI

program
...
h>
int main()
{

int a=12,b=10;

// variable declarations

printf("The output of the Bitwise exclusive OR operator a^b is
%d",a^b);
return 0;
}
Output

C - LANGUAGE

75

HI - TECH COMPUTERS
Bitwise complement operator
The

bitwise

complement

operator

is

also

known

as

one's

complement operator
...


It

takes

complement
complement

only

one

operation

on

operation

on

operand
an
any

or

variable

operand
...


If we have a variable named 'a',
a = 8;
The

binary

representation

below:
a = 1000

of

the

above

variable

is

given

When we apply the bitwise complement operator to the operand,
then the output would be:
Result = 0111

As we can observe from the above result that if the bit is 1,

-

then it gets changed to 0 else 1
...

#include ...

The bitwise shift operators will shift the bits either on the
left-side

or

right-side
...


TE
CH

Right-shift operator

shifts

the

number

of

bits

to the

Syntax of the left-shift operator is given below:
Operand << n
Where,
is

an

integer

expression

on

which

we

apply

the

-

Operand

left-shift operation
...

In the case of Left-shift operator, 'n' bits will be shifted
on the left-side
...

For example,

Suppose we have a statement:
int a = 5;
The binary representation of 'a' is given below:
a = 0101

C - LANGUAGE

77

HI - TECH COMPUTERS
If we want to left-shift the above representation by 2, then
the statement would be:
a << 2;
0101<<2 = 00010100
Let's understand through a program
...
h>
{

TE
CH

int main()
int a=5; // variable initialization

printf("The value of a<<2 is : %d ", a<<2);
return 0;
}
Output
Right-shift operator

It is an operator that shifts the number of bits to the right
side
...

N is the number of bits to be shifted
...
The 'n' bits on the right-side will
be popped out, and 'n' bits on the left-side are filled with
0
...

#include ...
It is widely used for documenting code
...

Single Line Comments

Single Line Comments

TE
CH

Multi-Line Comments

Single line comments are represented by double slash \\
...

#include ...


For

HI

Even

example:

printf("Hello C");//printing information
Mult Line Comments
Multi-Line comments are represented by slash asterisk \*
...
It can occupy many lines of code, but it can't be nested
...

#include ...
The format string determines the format
of the input and output
...

The commonly used format specifiers in printf() function are:
Format

Description

%d or %i

It

is

used

TE
CH

specifier
to

print

the

signed

integer value

where signed integer means that the variable can
hold both positive and negative values
...

%o

It is used to print the octal unsigned integer
where octal integer value always starts with a 0

%x

-

value
...


In

this,

are

printed

in

small

letters such as a, b, c, etc
...

%f

It

is

used

for

printing

the

decimal

floating-point values
...


C - LANGUAGE

82

HI - TECH COMPUTERS
%e/%E

It is used for scientific notation
...


%g

It is used to print the decimal floating-point
values, and it uses the fixed precision, i
...
,
the value after the decimal in input would be
exactly the same as the value in the output
...


TE
CH

%p

%c

It is used to print the unsigned character
...


%ld

It

is

value
...


-

● %d
1
...
{

int b=6;

4
...


printf("Value of b is:%d", b);

6
...


7
...


return 0;

9
...

Output

C - LANGUAGE

83

HI - TECH COMPUTERS
● %u
1
...
{
3
...


int c= -10;

5
...


printf("\nValue of c is:%u",c);

8
...
}

TE
CH

7
...
e
...
The value of
b is positive, so %u specifier prints the exact value of b,
but

it

does

not

print

negative value
...
int main()
2
...


printf("Octal value of a is: %o", a);

5
...


6
...
}

In

the

above

code,

we

are

displaying

the octal value and

integer value of a
...
int main()
2
...


int y=0xA;

4
...


printf("\nHexadecimal value of y is: %X",y);

6
...


return 0;

8
...
We
display the hexadecimal value of y in two formats
...
e
...
int main()
2
...
e
...


3
...
4;

4
...


return 0;

6
...

Output
● %e

HI

1
...
{
3
...


printf("Exponential value of y is: %e", y);

5
...
}
Output
● %E
1
...
{
3
...


printf("Exponential value of y is: %E", y);

5
...
}
Output
● %g
1
...
{
float y=3
...


printf("Float value of y is: %g", y);

5
...
}

TE
CH

3
...
The %g specifier displays the output
same as the input with a same precision
...
int main()
2
...


int y=5;

4
...


return 0;

6
...
int main()
2
...


char a='c';

4
...


return 0;

6
...
int main()
2
...


printf("%s", "java");

4
...
}
Minimum Field Width Specifier
number

of

spaces

on

TE
CH

Suppose we want to display an output that occupies a minimum
the

screen
...

1
...
{
3
...


printf("%8d", x);

5
...


return 0;

-

7
...


HI

Output
Now we will see how to fill the empty spaces
...
int main()
2
...
int x=12;
4
...


return 0;

6
...

Output
C - LANGUAGE

87

HI - TECH COMPUTERS
Specifying Precision
We can specify the precision by using '
...

1
...
{
3
...
2;
4
...
2f", x);

5
...
}

HI

-

Output

C - LANGUAGE

88

HI - TECH COMPUTERS
Escape Sequence in C
An escape sequence in C language is a sequence of characters
that doesn't represent itself when used inside string literal
or character
...
For example: \n represents new line
...
h>
int main(){

int number=50;
printf("You\nare\nlearning\n\'c\'

language\n\"Do

you

know

language\"");
return 0;
}
Output:
C - LANGUAGE

89

C

HI - TECH COMPUTERS
You
are
learning
'c' language
"Do you know C language"

What is ASCII code?
The

full

form

information
used

for

of

ASCII

interchange
...


Each

character

or

a

special character is represented by some ASCII code, and each
ascii code occupies 7 bits in memory
...
The ascii value represents the character

-

variable in numbers, and each character variable is assigned
with some number range from 0 to 127
...


HI

In the above example, we assign 'A' to the character variable
whose ascii value is 65, so 65 will be stored in the character
variable rather than 'A'
...

Play Video

We will create a program which will display the ascii value of
the character variable
...
h>
int main()
C - LANGUAGE

90

HI - TECH COMPUTERS
{
char ch;

// variable declaration

printf("Enter a character");
scanf("%c",&ch);

// user input

printf("\n The ascii value of the ch variable is : %d", ch);
return 0;
}
In the above code, the first user will give the character

TE
CH

input, and the input will get stored in the 'ch' variable
...
e
...

Output
ASCII value in C

The above output shows that the user gave the input as 'A',
after

giving

input,

the

ascii

value

of

'A'

will

get

-

and

printed, i
...
, 65
...

#include ...
As we know that ascii value of all the characters
starts from 0 and ends at 255, so we iterate the for loop from
0 to 255
...
h>
int main()
{
int sum=0;

// variable initialization

char name[20];
int i=0;

TE
CH

of a string
...
After
taking user input, we execute the while loop which adds the
ascii value of all the characters of a string and stores it in
a 'sum' variable
...
e
...


type

that

contains

Basically,

the

two

bool

types of

type

value

represents two types of behavior, either true or false
...

In

C Boolean, '0' is stored as 0, and another integer is

stored as 1
...
e
...
h
...

Syntax
bool variable_name;

In the above syntax, bool is the data type of the variable,
and variable_name is the name of the variable
...


-

Play Video
#include ...
h>

HI

int main()
{

bool x=false; // variable initialization
...
h> header file so
that we can use the bool type variable in our program
...
Then, we add
the

conditional

statements,

i
...
,

if
...


The value of x is FALSE
Boolean Array
Now,

we

create

a

bool

TE
CH

Output

type

array
...


Let's understand this scenario through an example
...
h>
#include ...
e
...

Output
1,0,

C - LANGUAGE

94

HI - TECH COMPUTERS
typedef
There is another way of using Boolean value, i
...
, typedef
...

Let's see a simple example of typedef
...
h>
typedef enum{false,true} b;

TE
CH

int main()
{

b x=false; // variable initialization
if(x==true) // conditional statements
{

printf("The value of x is true");
}
else
{

printf("The value of x is false");
return 0;
}

-

}

In the above code, we use the Boolean values, i
...
, true and

HI

false, but we have not used the bool type
...
In order to
achieve this, the typedef keyword is used in the program
...
e
...
We
use the 'b' type in our program and create the 'x' variable of
type 'b'
...

There are three types of logical operators in the C language:
&&(AND

Operator):

It

is a logical operator that takes two

operands
...
If the value of both the operands is false, then it
returns false otherwise true
...

If the value of the operand is false, then it returns true,
and

if the value of the operand is true, then it returns

false
...


-

#include ...
h>
int main()
{

HI

bool x=false;
bool y=true;

printf("The value of x&&y is %d", x&&y);
printf("\nThe value of x||y is %d", x||y);
printf("\nThe value of !x is %d", !x);
}

Output
The value of x&&y is 0
The value of x||y is 1
The value of !x is 1

C - LANGUAGE

96

HI - TECH COMPUTERS

Static in C
Static is a keyword used in C programming language
...
e
...


An

ordinary

variable is limited to the scope in which it is defined, while

TE
CH

the scope of the static variable is throughout the program
...
It is declared at the
top

of

the

program,

and

program
...
Its lifetime is throughout the program
...
The memory of a static

HI

local variable is valid throughout the program, but the scope
of visibility of a variable is the same as the automatic local
variables
...


Static member variables
When the member variables are declared with a static keyword
in a class, then it is known as static member variables
...

Static method
C - LANGUAGE

97

HI - TECH COMPUTERS
The member function of a class declared with a static keyword
is

known as a static method
...

Let's understand through an example
...
h>
int main()
printf("%d",func());
printf("\n%d",func());
return 0;
}
int func()
{

TE
CH

{

int count=0; // variable initialization

count++; // incrementing counter variable
return count; }

-

In the above code, the func() function is called
...
As soon as the function completes
its

execution,

the

memory

of

the

count

variable

will

be

removed
...


HI

then

If we

declare the variable as static, then the variable will not be
removed from the memory even when the function completes its
execution
...

Syntax
The syntax of a static variable is given below:
static data_typevariable_name;

#include ...


int func()

-

{
static int count=0;
count++;

return count;

HI

}
In the above code, we have declared the count variable as
static
...
Therefore, we can say that the
value

of

the

static

variable

persists

within

the

function

call
...

In the above syntax, the expression1 is a Boolean condition
that can be either true or false value
...

The expression2 is said to be true only when it returns a
non-zero value
...


The expression3 is said to be false only when it returns zero
value
...

#include ...
After taking input, we have applied the condition by
using

a

checking

conditional

operator
...
If the age of the user is

age

of

In

this

condition,

we

are

greater than or equal to 18, then the statement1 will execute,
i
...
,

(printf("eligible

for

voting"))

otherwise,

statement2

will execute, i
...
, (printf("not eligible for voting"))
...

C - LANGUAGE

100

HI - TECH COMPUTERS
If we provide the age of user below 18, then the output would
be:
Conditional Operator in C
As

we can observe from the above two outputs that if the

condition is true, then the statement1 is executed; otherwise,

Till

now,

we

have

TE
CH

statement2 will be executed
...
Now, we will see how a conditional operator is
used to assign the value to a variable
...

#include ...
e
...
After the
declaration, we are assigning value to the 'b' variable by
using the conditional operator
...

Output

C - LANGUAGE

101

HI - TECH COMPUTERS
The above output shows that the value of 'b' variable is 3
because the value of 'a' variable is equal to 5
...
Let's
look at their differences
...


A conditional operator can also be used for assigning a value
to the variable, whereas the 'if-else' statement cannot be
used for the assignment purpose
...

The

nested

ternary

operator is more complex and cannot be

-

easily debugged, while the nested 'if-else' statement is easy

HI

to read and maintain
...
As we know that the 1s complement of a binary number is
created

by

transforming

bit

1

to

0

and

0

to

1;

the

2s

complement of a binary number is generated by adding one to
the 1s complement of a binary number
...


In the above figure, the binary number is equal to 00010100,
and its one's complement is calculated by transforming the bit
1 to 0 and 0 to 1 vice versa
...


calculate

the

After

two's

calculating

complement

by

one's

adding

complement,

1

to

the

we

one's

complement, and its result is 11101100
...

#include ...
The operations specified in
if block are executed if and only if the given condition is
true
...


The if statement is used to check some given condition and
perform some operations depending upon the correctness of that
condition
...

The syntax of the if statement is given below
...
if(expression)
2
...
//code to be executed

HI

4
...

1
...
h>
2
...
int number=0;
4
...
scanf("%d",&number);
6
...
printf("%d is even number",number);
8
...
return 0;
10
...

1
...
h>
3
...


int a, b, c;

5
...
int main()

printf("Enter three numbers?");

6
...


if(a>b && a>c)

8
...


printf("%d is largest",a);

10
...


if(b>a

12
...


&& b > c)

14
...


if(c>a && c>b)

16
...

18
...


if(a == b && a == c)

20
...


printf("All are equal");

22
...


}

}

Output
Enter three numbers?
12 23 34
34 is largest

C - LANGUAGE

107

HI - TECH COMPUTERS
If-else Statement
The if-else statement is used to perform two operations for a
single condition
...
e
...
Here, we must notice that the if and else block
cannot be executed simultaneously
...
The syntax of the if-else statement
is given below
...
if(expression){

2
...
}else{

4
...
}

Let's see the simple example to check whether a number is even

-

or odd using if-else statement in C language
...
#include ...
int main(){

3
...
printf("enter a number:");
5
...
if(number%2==0){
7
...
}

9
...


printf("%d is odd number",number);

11
...


return 0;

13
...

1
...
h>
2
...
{
int age;

5
...


scanf("%d",&age);

7
...


{

9
...
");

10
...


else

12
...


printf("Sorry
...

15
...


Enter your age?18

HI

You are eligible to vote
...
you can't vote
If else-if ladder Statement
The if-else-if ladder statement is an extension to the if-else
statement
...
In if-else-if
ladder statement, if a condition is true then the statements
defined in the if block will be executed, otherwise if some
other condition is true then the statements defined in the
else-if block will be executed, at the last if none of the
C - LANGUAGE

109

HI - TECH COMPUTERS
condition
block

is

will

true

be

then the statements defined in the else

executed
...
It is similar to the switch case statement where the
default is executed instead of else block if none of the cases
is matched
...
if(condition1){
2
...
}else if(condition2){
5
...
else if(condition3){

TE
CH

4
...
//code to be executed if condition3 is true
8
...

10
...


//code to be executed if all the conditions are false

12
...


1
...
h>
2
...
int number=0;
4
...
scanf("%d",&number);
6
...
printf("number is equals to 10");
8
...
else if(number==50){
10
...


}

12
...


printf("number is equal to 100");

14
...


else{

16
...


}

18
...


}

Output
enter a number:4
enter a number:50
number is equal to 50

TE
CH

number is not equal to 10, 50 or 100

Program to calculate the grade of the student according to the
specified marks
...
#include ...
int main()
3
...


printf("Enter your marks?");

6
...


if(marks > 85 && marks <= 100)

8
...


9
...


printf("Congrats ! you scored grade A
...


else if (marks > 60 && marks <= 85)

12
...


printf("You scored grade B +
...


}

15
...


{

17
...
");

18
...


else if (marks > 30 && marks <= 40)

20
...

C - LANGUAGE

printf("You scored grade C
...


}

23
...


{

25
...
");

26
...


}
}

Output
Sorry you are fail
...

Enter your marks?90

TE
CH

Enter your marks?10

HI

-

Congrats ! you scored grade A
...
Here, We can define various statements in the
multiple cases for the different values of a single variable
...
case value1:

TE
CH

1
...
//code to be executed;
4
...
case value2:

6
...
break;

//optional

8
...
default:
10
...


code to be executed if all cases are not matched;

Rules for switch statement in C language
1) The switch expression must be of an integer or character
type
...

3)

The

case

value

can

be

used

only

inside

the

switch

statement
...
It is

optional
...

It is known as fall through the state of C switch statement
...
We are assuming
that there are following variables
...
int x,y,z;
2
...
float f;
C - LANGUAGE

113

HI - TECH COMPUTERS
Invalid

Valid

Invalid

Switch

Case

Case

switch(x)

switch(f)

case 3;

case 2
...
5)

case 'a';

case x;

switch(a+b-2)

case 1+2;

case x+2;

switch(func(x,y))

case

case 1,2,3;

TE
CH

Valid Switch

'x'>'y';

Functioning of switch case statement
First,

the

integer

expression

specified

in

the

switch

statement is evaluated
...
If a
match is found, then all the statements specified in that case
are executed along with the all the cases present after that
case including the default statement
...


statement,

If

the

matched

-

similar

then

all

the

cases

case

present

contains
after

that

a

break

will

be

skipped, and the control comes out of the switch
...


HI

Let's see a simple example of c language switch statement
...
#include ...
int main(){

3
...
printf("enter a number:");
5
...
switch(number){
7
...
printf("number is equals to 10");
9
...


case 50:

C - LANGUAGE

114

HI - TECH COMPUTERS
printf("number is equal to 50");

12
...


case 100:

14
...


break;

16
...


printf("number is not equal to 10, 50 or 100");

18
...


return 0;

20
...


number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

-

Switch case example 2
1
...
h>
2
...
{
int x = 10, y = 5;

HI

4
...


switch(x>y && x+y>0)

6
...


case 1:

8
...


break;

10
...


printf("bye");

12
...


default:

14
...

C - LANGUAGE

}
115

HI - TECH COMPUTERS
16
...


}

Output
hi
C Switch statement is fall-through
In C language, the switch statement is fall through; it means
if you don't use a break statement in the switch case, all the
Let's

try

to

understand

TE
CH

cases after the matching case will be executed
...

1
...
h>
2
...
int number=0;
4
...
printf("enter a number:");
6
...
switch(number){

-

8
...
printf("number is equal to 10\n");
case 50:

11
...


case 100:

HI

10
...


printf("number is equal to 100\n");

14
...


printf("number is not equal to 10, 50 or 100");

16
...


return 0;

18
...
Such type of statements is called nested switch
case statements
...

1
...
h>
2
...

4
...


int j = 20;

6
...

8
...


case 10:

10
...


case 20:

12
...


case 20:

14
...

16
...

18
...


printf("Exact value of j is : %d\n", j );

20
...

22
...
There are
three types of loops used in the C language
...

Why use loops in C language?
looping

simplifies

the

complex

problems

into

the

TE
CH

The

easy

ones
...


For

example, if we need to print the first 10 natural numbers
then, instead of using the printf statement 10 times, we can
print inside a loop which runs up to 10 iterations
...


-

2) Using loops, we do not need to write the same code again
3) Using loops, we can traverse over the elements of data
structures (array or linked lists)
...
do while
2
...
for

do-while loop in C
The do-while loop continues until a given condition satisfies
...
It is used when it is
necessary

to

execute

the

loop

at

least

once

(mostly

driven programs)
...
do{
2
...
}while(condition);
while loop in C
The while loop in c is to be used in the scenario where we

TE
CH

don't know the number of iterations in advance
...
It is also called a
pre-tested loop
...
while(condition){

2
...
}

-

for loop in C
The for loop is used in the case where we need to execute some
part of the code until the given condition is satisfied
...
It is better to

HI

use for loop if the number of iteration is known in advance
...
for(initialization;condition;incr/decr){
2
...
}

C - LANGUAGE

120

HI - TECH COMPUTERS
do while loop in C
The do while loop is a post tested loop
...
The do-while loop is mainly used in the case where
we need to execute the loop at least once
...

do while loop syntax
1
...
//code to be executed
3
...
#include ...
#include ...
void main ()
4
...


int choice,dummy;

7
...


printf("\n1
...
Print Java\n3
...


scanf("%d",&choice);
switch(choice)

HI

10
...


11
...


case 1 :

13
...


break;

15
...


printf("Java");

17
...


case 3:

19
...


break;

21
...


printf("please enter valid choice");

23
...


printf("do you want to enter more?");

25
...


scanf("%c",&c);

27
...


}

Output

TE
CH

1
...
Print Java
3
...
Print Hello
2
...
Exit

do you want to enter more?

HI

n
do while example

There is given the simple program of c language do while loop
where we are printing the table of 1
...
#include ...
int main(){
3
...
do{
5
...
i++;
7
...
return 0;
9
...
#include ...
int main(){

-

3
...
printf("Enter a number: ");
5
...
do{

HI

7
...
i++;

9
...


return 0;

11
...


HI

1
...
//statement
3
...
In general, a
while loop allows a part of the code to be executed multiple
times

depending

upon

a given boolean condition
...
The while loop is mostly
used in the case where the number of iterations is not known
in advance
...
while(condition){

TE
CH

The syntax of while loop in c language is given below:
2
...
}

Example of the while loop in C language

Let's see the simple program of while loop that prints table
of 1
...
#include ...
int main(){

-

3
...
while(i<=10){

5
...
i++;

HI

7
...
return 0;
9
...
#include ...
int main(){
3
...
scanf("%d",&number);
6
...
printf("Enter a number: ");

7
...
i++;
9
...


return 0;

11
...

defined

inside

the

while

loop

will

repeatedly execute until the given condition fails
...
The condition
will be false if it returns any non-zero number
...

● Running a while loop without a body is possible
...


● If the loop body contains only one statement, then the
Example 1

-

braces are optional
...
#include ...
void main ()

HI

3
...


int j = 1;

5
...


{

7
...


}

9
...


}

Output
3 5 7 9 11

C - LANGUAGE

127

HI - TECH COMPUTERS
Example 2
1
...
h>
2
...
{
4
...


{

6
...
}
Output

TE
CH

7
...
#include ...
void main ()
3
...


while(x+y-1)

6
...

8
...


printf("%d %d",x--,y--);
}

9
...

1
...
//statement
3
...
It is frequently used
to

traverse

the

data structures like the array and linked

list
...
for(Expression 1; Expression 2; Expression 3){
3
...
//code to be executed

Let's see the simple program of for loop that prints table of
1
...
#include ...
int main(){
3
...
for(i=1;i<=10;i++){
6
...
return 0;

-

5
...
}

HI

Output
1
2
3
4
5
6
7
8
9
10

C - LANGUAGE

129

HI - TECH COMPUTERS
C Program: Print table for the given number using C for loop
1
...
h>
2
...
int i=1,number=0;
4
...
scanf("%d",&number);
6
...
}
9
...


}

Output
Enter a number: 2
2
4
6
8

TE
CH

7
...

● We can initialize more than one variable in Expression 1
...

● In C, we can not declare the variables in Expression 1
...
#include ...
int main()
3
...


4
...


for(a=0,b=12,c=23;a<2;a++)

6
...


printf("%d ",a+b+c);
}

9
...


HI

35 36
Example 2

1
...
h>
2
...
{
4
...


for(;i<5;i++)

6
...

8
...
}

C - LANGUAGE

131

HI - TECH COMPUTERS
Output
1 2 3 4
Properties of Expression 2
● Expression 2 is a conditional expression
...
If it is not, the
loop is terminated
...
However,

TE
CH

the loop will iterate until the last condition becomes
false
...

● Expression 2 is optional
...
That is, we can initialize the variable as
well as update the loop variable in expression 2 itself
...


However, in C, any non-zero value is true, and zero is
false by default
...
#include ...
int main()
3
...


for(i=0;i<=4;i++)

6
...


7
...


printf("%d ",i);

}

9
...
#include ...
int main()

C - LANGUAGE

132

HI - TECH COMPUTERS
3
...


int i,j,k;

5
...


{

7
...


j+=2;

9
...

}

TE
CH

11
...
#include ...
int main()
3
...


for(i=0;;i++)

6
...


7
...


printf("%d",i);

}

9
...

● We can update more than one variable at the same time
...


C - LANGUAGE

133

HI - TECH COMPUTERS
Example 1
1
...
h>
2
...
{
4
...


for(i = 0;i<5;i++,j=j+2)

6
...


printf("%d %d\n",i,j);
}

TE
CH

8
...
}
Output
0 2
1 4
2 6
3 8
4 10

Loop body
braces

However,

if

{}

are

used

to

define

the

scope

of

the loop
...
A loop without a body is possible
...
e
...
Consider the following example
...
#include ...
void main ()
3
...


int i;

5
...


{

7
...


printf("%d ",i);

9
...


}
}

C - LANGUAGE

134

HI - TECH COMPUTERS
Output
20 20 20 20 20 20 20 20 20 20
Infinitive for loop in C
To make a for loop infinite, we need not give any expression
in

the

syntax
...
This will
1
...
h>
2
...
{
4
...


{

6
...


TE
CH

work as an infinite for loop
...
}

If you run this program, you will see above statement infinite

HI

-

times
...
Nesting of loops is the
feature

in

C

that allows the looping of statements inside

another loop
...

Any number of loops can be defined inside another loop, i
...
,
there is no restriction for defining any number of loops
...
You can define any
type of loop inside another loop; for example, you can define
Syntax of Nested loop
1
...
{
3
...


Inner_loop
{

5
...


TE
CH

'while' loop inside a 'for' loop
...

}

7
...


8
...

Nested for loop

HI

The nested for loop means any type of loop which is defined
inside the 'for' loop
...
for (initialization; condition; update)
2
...


for(initialization; condition; update)

4
...


// inner loop statements
...


}

7
...


8
...
#include ...
int main()
3
...


int n;// variable declaration

5
...


// Displaying the n tables
...


for(int i=1;i<=n;i++)

8
...


// outer loop

for(int j=1;j<=10;j++)

10
...


printf("%d\t",(i*j)); // printing the value
...


}

13
...


// inner loop

}

Explanation of the above code
● First,

the

'i'

variable is initialized to 1 and then

-

program control passes to the i<=n
...


● If the condition is true, then the program control passes

HI

to the inner loop
...


● After the execution of the inner loop, the control moves
back to the update of the outer loop, i
...
, i++
...
e
...

● If the condition is true, then the inner loop will be
executed again
...


C - LANGUAGE

137

HI - TECH COMPUTERS

Nested while loop

TE
CH

Output:

The nested while loop means any type of loop which is defined
inside the 'while' loop
...
while(condition)
2
...


while(condition)

4
...


// inner loop statements
...


}

7
...


-

8
...
#include ...
int main()

HI

3
...


int rows;

5
...


int k=1; // variable initialization

7
...

8
...


printf("\nEnter the number of columns :"); // input
the number of columns
...


scanf("%d",&columns);

11
...


int i=1;

13
...


{

15
...


while(j<=columns)

17
...


printf("%d\t",k);

// printing the value of

k
...


j++;

// increment counter

TE
CH

19
...


}

22
...


printf("\n");

24
...


}
}

Explanation of the above code
...
e
...

● The program initializes the 'i' variable by 1
...

● After the execution of the inner loop, the control moves

HI

to the update of the outer loop, i
...
, i++
...


● If the condition is true, the control then again moves to
the inner loop
...


C - LANGUAGE

139

HI - TECH COMPUTERS

Nested do
...
while loop means any type of loop which is
defined inside the 'do
...

1
...
{
3
...


do
{

6
...


-

5
...
// outer loop statements
...
}while(condition);

HI

Example of nested do
...

1
...
h>
2
...
{
4
...


********

6
...


********

8
...
int i=1;
10
...


{

C - LANGUAGE

// outer loop

140

HI - TECH COMPUTERS
12
...


do

14
...


printf("*");

16
...


printf("\n");

19
...

21
...


Explanation of the above code
...
e
...


HI

● As we know that the do
...


● After the execution of the inner loop, the control moves
to the update of the i++
...
If the condition in the outer loop is true,
then the inner loop is executed
...


C - LANGUAGE

141

HI - TECH COMPUTERS
Infinite Loop in C
An

infinite

loop

is

a

looping

construct

that

does

not

terminate the loop and executes the loop forever
...


It

either

produces a continuous output or no output
...
In the following
situations, this type of loop can be used:

● All the operating systems run in an infinite loop as it
does not exist after performing some task
...


● All the servers run in an infinite loop as the server
responds to all the client requests
...


-

● All the games also run in an infinite loop
...

We

can

create
The

infinite

following

HI

structures
...


The

following

is

the

definition for the infinite for loop:
1
...
{
3
...


4
...

Let's understand through an example
...
#include ...
int main()
3
...


for(;;)

5
...


printf("Hello java");
}

8
...


9
...


C - LANGUAGE

143

HI - TECH COMPUTERS
while loop
Now, we will see how to create an infinite loop using a while
loop
...
while(1)
2
...


// body of the loop
...
}
As

we

know

that

any

TE
CH

In the above while loop, we put '1' inside the loop condition
...

Let's look at a simple example
...
#include ...
int main()
3
...


int i=0;

5
...


{
i++;

8
...

}

10
...


}

HI

9
...
The value
of 'i' will be updated an infinite number of times
...
while loop
The do
...


The

following

is

the

syntax

to

create

the

infinite

do
...

1
...
{
3
...


4
...
while loop represents the infinite condition as
we provide the '1' value inside the loop condition
...


C - LANGUAGE

145

true

HI - TECH COMPUTERS
C break statement
The break is a keyword in C which is used to bring the program
control out of the loop
...
The break statement breaks the loop
one by one, i
...
, in the case of nested loops, it breaks the
inner loop first and then proceeds to outer loops
...
With loop
Syntax:

TE
CH

1
...
//loop or switch case
2
...
#include ...
#include ...
void main ()
4
...


for(i = 0; i<10; i++)

7
...


printf("%d ",i);

9
...

10
...


}

12
...

14
...

1
...
h>
2
...
int i=1,j=1;//initializing a local variable
5
...
for(i=1;i<=3;i++){
6
...
if(i==2 && j==2){

8
...
}
10
...


return 0;

12
...
But 3 1, 3 2 and 3 3 are printed because the break
statement is used to break the inner loop only
...


C - LANGUAGE

147

HI - TECH COMPUTERS
1
...
h>
2
...
{
int i = 0;

5
...


{

7
...


i++;

9
...


",i);

break;

11
...


printf("came out of while loop");

13
...


1

2

3

4

5

6

7

8

9

came out of while loop

break statement with do-while loop

Consider the following example to use the break statement with

-

a do-while loop
...
#include ...
void main ()

HI

3
...


int n=2,i,choice;

5
...


{

7
...


while(i<=10)

9
...


printf("%d X %d = %d\n",n,i,n*i);

11
...

13
...
",n+1);
C - LANGUAGE

148

HI - TECH COMPUTERS
14
...


if(choice == 0)

16
...


break;

18
...


n++;

20
...


}while(1);
}

TE
CH

Output
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
do

you

want

-

2 X 10 = 20
to

continue with the table of 3 , enter any

non-zero value to continue
...
0

C - LANGUAGE

149

HI - TECH COMPUTERS
C continue statement
The continue statement in C language is used to bring the
program control to the beginning of the loop
...
It is mainly used for a
condition

so

that

we

can

skip

some

code for a particular

condition
...
//loop statements
2
...
//some lines of the code which is to be skipped
Continue statement example 1
1
...
h>
2
...
{
4
...


while(i!=10)

6
...


continue;

9
...


10
...


}

Output

infinite loop

Continue statement example 2
1
...
h>
2
...
int i=1;//initializing a local variable
4
...
for(i=1;i<=10;i++){
6
...
continue;
8
...
printf("%d \n",i);
10
...


return 0;

12
...

C continue statement with inner loop
In such case, C continue statement continues only inner loop,

HI

but not outer loop
...
#include ...
int main(){

3
...
for(i=1;i<=3;i++){
5
...
if(i==2 && j==2){
7
...
}
9
...


}

11
...


return 0;

13
...


C - LANGUAGE

152

HI - TECH COMPUTERS
C goto statement
The goto statement is known as jump statement in C
...
The goto statment can be used to repeat
some part of the code for a particular condition
...
However, using goto is avoided
these

days

since

it

makes

program

less

readable

TE
CH

complecated
...
label:

and

2
...
goto label;
goto example
Let's

see

a

simple

example

language
...
#include ...
int main()

-

3
...


int num,i=1;

5
...

7
...


printf("%d x %d = %d\n",num,i,num*i);

9
...


if(i<=10)

11
...


}

Output:
Enter the number whose table you want to print?10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
C - LANGUAGE

153

to

HI - TECH COMPUTERS
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90

When should we use goto?

TE
CH

10 x 10 = 100

The only condition in which using goto is preferable is when
we need to break the multiple loops using a single statement
at the same time
...

1
...
h>
2
...
{
4
...


for(i=0;i<10;i++)

6
...


{

9
...


for(k=0;k<3;k++)

10
...

12
...


{

14
...


}

16
...


}

18
...


out:

20
...


}

0 0 0
C - LANGUAGE

154

HI - TECH COMPUTERS
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 3 0

HI

-

came out of the loop

TE
CH

0 2 2

C - LANGUAGE

155

HI - TECH COMPUTERS
Type Casting in C
Typecasting allows us to convert one data type into other
...

Syntax:
1
...
int f= 9/4;

TE
CH

higher for avoiding data loss
...
printf("f : %d\n", f );//Output: 2
With Type Casting:
1
...
printf("f : %f\n", f );//Output: 2
...


-

1
...
h>
2
...
float f= (float)9/4;

4
...
return 0;
6
...
250000

C - LANGUAGE

156

HI - TECH COMPUTERS
C Functions
In c, we can divide a large program into the basic building
blocks known as function
...


A

function

can

be

called multiple times to provide reusability and modularity to
the C program
...
The function is also known as
procedureor subroutinein other programming languages
...

● By

using

functions,

we

can

avoid

rewriting

same

logic/code again and again in a program
...


● We can track a large C program easily when it is divided
into multiple functions
...

program
...

● Function declaration A function must be declared globally

HI

in a c program to tell the compiler about the function
name, function parameters, and return type
...
The parameter list must not differ in function
calling and function declaration
...



Function

definition

It

contains

the

actual

statements

which are to be executed
...

Here, we must notice that only one value can be returned
from the function
...
return_type function_name(data_type parameter
...
//code to be executed
3
...
Library Functions: are the functions which are declared
in the C header files such as scanf(), printf(), gets(),
puts(), ceil(), floor() etc
...
User-defined

functions:

are

the

functions

which

are

created by the C programmer, so that he/she can use it
many times
...


C - LANGUAGE

158

HI - TECH COMPUTERS
Return Value
A C function may or may not return a value from the function
...

Let's see a simple example of C function that doesn't return
any value from the function
...
printf("hello c");
3
...
void hello(){

If you want to return any value from the function, you need to
use any data type such as int, long, char, etc
...

Let's see a simple example of C function that returns int
value from the function
...
int get(){
2
...
}
In the above example, we have to return 10 as a value, so the
return type is int
...
g
...
2, 3
...
5, etc), you need to use float as the

HI

return type of the method
...
float get(){
2
...
2;
3
...


Different aspects of function calling
A function may or may not accept any argument
...
Based on these facts, There are four

C - LANGUAGE

159

HI - TECH COMPUTERS
different aspects of function calls
...
void printName();
3
...
{

TE
CH

1
...
h>

5
...


printName();

7
...
void printName()
9
...

}

Output
Hello Java

-

11
...
#include ...
void sum();
3
...
{
5
...


sum();

7
...
void sum()
9
...


int a,b;

11
...


scanf("%d %d",&a,&b);

13
...


}

Output
Going to calculate the sum of two numbers:
Enter two numbers 10
The sum is 34

TE
CH

24

Example for Function without argument and with return value
Example 1
1
...
h>
2
...
void main()
4
...


int result;

6
...


result = sum();

8
...
}
int sum()

HI

10
...


{

12
...


printf("\nEnter two numbers");

14
...


return a+b;

16
...
#include ...
int sum();
3
...
{
printf("Going
square\n");

to

calculate

the

area

6
...


printf("The area of the square: %f\n",area);

8
...
int square()
10
...


11
...


printf("Enter the length of the side in meters: ");

13
...


return side * side;
}

-

15
...
000000
Example for Function with argument and without return value
Example 1

1
...
h>
2
...
void main()
4
...


int a,b,result;

6
...


printf("\nEnter two numbers:");

C - LANGUAGE

162

two

HI - TECH COMPUTERS
8
...


sum(a,b);

10
...


void sum(int a, int b)

12
...

14
...

1
...
h>

2
...
void main()
4
...

6
...


scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);

HI

7
...


average(a,b,c,d,e);

10
...


void average(int a, int b, int c, int d, int e)

12
...


float avg;

14
...


printf("The

average

of

given

five

numbers

%f",avg);
16
...
000000
Example for Function with argument and with return value
Example 1
1
...
h>
2
...
void main()
4
...


int a,b,result;

6
...


scanf("%d %d",&a,&b);

9
...


10
...


printf("\nThe sum is : %d",result);

12
...


{

14
...


return a+b;

}

Output

Going to calculate the sum of two numbers:
Enter two numbers:10
20
The sum is : 30

C - LANGUAGE

164

two

HI - TECH COMPUTERS
Example 2: Program to check whether a number is even or odd
1
...
h>
2
...
void main()
4
...
int n,flag=0;
6
...
scanf("%d",&n);
9
...


if(flag == 0)

11
...


TE
CH

7
...


}

14
...


{

16
...

18
...


int even_odd(int n)

20
...

22
...


return 1;

24
...


else

26
...


return 0;

28
...


}
}

Output
Going to check whether a number is even or odd
Enter the number: 100
C - LANGUAGE

165

HI - TECH COMPUTERS
The number is even
C Library Functions
Library

functions

are

the

inbuilt

function

in

C

that

are

grouped and placed at a common place called the library
...
For
example, printf is a library function used to print on the
console
...
All C standard library functions are defined inside
the different header files saved with the extension
...
We
need to include these header files in our program to make use
of the library functions defined in such header files
...
h in our program which is a header file
that

contains

all

the

input/output
...

Header

N

file

1

stdio
...


HI

It

contains

all

the

library

functions

regarding standard input/output
...
h

This is a console input/output header file
...
h

It

contains

all

string

related

library

functions like gets(), puts(),etc
...
h

This

header

library

file

functions

contains all the general
like

malloc(),

calloc(),

exit(), etc
...
h

This

header

operations

file

contains

related

functions

all

the

like

math

sqrt(),

pow(), etc
...
h

This

header

file

contains

all

the

time-related functions
...
h

This

header

file

contains

all

character

8

stdarg
...

Variable argument functions are defined in
this header file
...
h

All the signal handling functions are defined
in this header file
...
h

This file contains all the jump functions
...
h

This file contains locale functions
...
h

This file contains error handling functions
...
h

-

10

HI

This file contains diagnostics functions
...
e
...


Let's understand call by value and call by reference in c
language one by one
...
In other
words, we can say that the value of the variable is used
in the function call in the call by value method
...

● In

call

actual

by

and

value,

formal

different

memory

is

parameters

since

the

allocated

for

value

the

of

actual parameter is copied into the formal parameter
...


C - LANGUAGE

168

HI - TECH COMPUTERS
Let's try to understand the concept of call by value in c
language by the example given below:
1
...
h>
2
...


printf("Before adding value inside function num=%d
\n",num);

4
...
}
7
...


8
...


printf("Before function call x=%d \n", x);

10
...


printf("After function call x=%d \n", x);

12
...


}

Output

-

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200

HI

After function call x=100
Call

by

Value

Example:

Swapping

the

values

of

the

two

variables

1
...
h>
2
...
int main()
4
...


int a = 10;

6
...


printf("Before swapping the values in main a = %d, b
= %d\n",a,b); // printing the value of a and b in main

8
...


printf("After swapping values in main a = %d, b =
%d\n",a,b);
change

by

//

The

value

changing

the

of

actual

formal

parameters

parameters

do

in

call

not
by

value, a = 10, b = 20
10
...


void swap (int a, int b)

12
...


temp = a;

15
...


b=temp;

17
...


= %d\n",a,b); // Formal parameters, a = 20, b = 10
18
...


HI

● The value of the actual parameters can be modified by
changing the formal parameters since the address of the
actual parameters is passed
...
All the
operations in the function are performed on the value
stored at the address of the actual parameters, and the
modified value gets stored at the same address
...

1
...
h>
2
...


printf("Before adding value inside function num=%d
\n",*num);

4
...


printf("After adding value inside function num=%d
\n", *num);

TE
CH

6
...
int main() {
8
...


printf("Before function call x=%d \n", x);

10
...


printf("After function call x=%d \n", x);

12
...


}

Output

Before function call x=100

-

Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
by

reference

Example: Swapping the values of the two

HI

Call

variables

1
...
h>
2
...
int main()
4
...


int a = 10;

6
...


printf("Before swapping the values in main a = %d, b
= %d\n",a,b); // printing the value of a and b in main

8
...


printf("After swapping values in main a = %d, b =
%d\n",a,b); // The values of actual parameters do change
in call by reference, a = 10, b = 20

10
...


void swap (int *a, int *b)

12
...


temp = *a;

15
...


*b=temp;

17
...


= %d\n",*a,*b); // Formal parameters, a = 20, b = 10
18
...

Any function which calls itself is called recursive function,
and such function calls are called recursive calls
...
However, it is
important

to

impose

a

termination

condition

of

recursion
...


TE
CH

Recursion code is shorter than iterative code however it is
Recursion cannot be applied to all the problem, but it is more
useful for the tasks that can be defined in terms of similar
subtasks
...

Generally,

iterative

solutions

are

more

efficient

than

recursion since function call is always overhead
...


solved

However,

recursively,

some

problems

can

are

also

best

be

solved

suited

to

be

-

solved by the recursion, for example, factorial finding, etc
...


1
...
h>

HI

2
...
int main()
4
...


int n,f;

6
...


scanf("%d",&n);

8
...


printf("factorial = %d",f);

10
...


int fact(int n)

12
...


if (n==0)

14
...


return 0;

16
...


else if ( n == 1)

18
...


}

21
...


{

23
...

25
...


}
}

Output

Enter the number whose factorial you want to calculate?5

HI

-

factorial = 120

C - LANGUAGE

174

HI - TECH COMPUTERS
Storage Classes in C
Storage

classes

in

C

are

used

to

determine

the

lifetime,

visibility, memory location, and initial value of a variable
...

● The visibility of the automatic variables is limited to
the block in which they are defined
...
The automatic variables
are initialized to garbage by default
...

● The

keyword

used

for

defining

automatic

variables

is

auto
...
#include ...
int main()
3
...
int a; //auto
5
...
float c;
7
...


%f",a,b,c);

//

printing

initial

default

8
...
}
Output:

-

value of automatic variables a, b, and c
...


● Static local variables are visible only to the function
or the block in which they are defined
...

● Default initial value of the static integral variable is
0 otherwise null
...

● The keyword used to define static variable is static
...
#include ...
static char c;
4
...
static char s[100];
6
...
{

TE
CH

3
...
printf("%d %d %f %s",c,i,f); // the initial default value
of c, i, and f will be printed
...
}
Output:
0 0 0
...


HI

● We can not dereference the register variables, i
...
, we
can not use &operator for the register variable
...


● The initial default value of the register local variables
is 0
...
However, it is

compiler?s choice whether or not; the variables can be
stored in the register
...
e
...

● Static

variables

can

not

be

stored

into

the

register

since we can not use more than one storage specifier for
the same variable
...
int main()
3
...
#include ...
register int a; // variable a is allocated memory in the
CPU register
...

5
...
}
Output:
0
External

-

● The external storage class is used to tell the compiler
that the variable defined as extern is declared with an
external linkage elsewhere in the program
...
It is only declaration and intended to specify
that the variable is declared elsewhere in the program
...


● We

can

only

initialize

the

extern

variable

globally,

i
...
, we can not initialize the external variable within
any block or method
...

● If a variable is declared as external then the compiler
searches for that variable to be initialized somewhere in

C - LANGUAGE

178

HI - TECH COMPUTERS
the program which may be extern or static
...

Example 1
1
...
h>
2
...
{
4
...
printf("%d",a);
6
...
int a = 20;
Output

HI

-

20

TE
CH

a defined and initialized somewhere in the pogram or not
...
Arrays are the
derived data type in C programming language which can store
the primitive type of data such as int, char, double, float,
etc
...


The

array is the simplest data structure where each data element

TE
CH

can be randomly accessed by using its index number
...

For example, if we want to store the marks of a student in 6
subjects, then we don't need to define different variables for
the marks in the different subject
...


By using the array, we can access the elements easily
...


-

Properties of Array
The array contains the following properties
...
e
...


HI

● Elements of the array are stored at contiguous memory
locations

where

the

first

element

is

stored

at

the

smallest memory location
...


Advantage of C Array
1) Code Optimization: Less code to the access the data
...


C - LANGUAGE

180

HI - TECH COMPUTERS
3) Ease of sorting: To sort the elements of the array, we need
a few lines of code only
...

Disadvantage of C Array
1)

Fixed

Size:

Whatever

size,

we

define

at

the

time

of

declaration of the array, we can't exceed the limit
...

Declaration of C Array

TE
CH

doesn't grow the size dynamically like LinkedList which we

We can declare an array in the c language in the following
way
...
data_type array_name[array_size];

Now, let us see the example to declare the array
...
int marks[5];

Here, int is the data_type, marks are the array_name, and 5 is
the array_size
...
We can initialize each element of the array
by using the index
...

1
...
marks[1]=60;
3
...
marks[3]=85;
5
...
#include ...
int main(){
3
...
int marks[5];//declaration of array
5
...
marks[1]=60;
8
...
marks[4]=75;

TE
CH

7
...


//traversal of array

11
...


printf("%d \n",marks[i]);

13
...


return 0;

15
...

Let's see the code
...
int marks[5]={20,30,40,50,60};

In such case, there is no requirement to define the size
...

1
...

1
...
h>
2
...
int i=0;
4
...
for(i=0;i<5;i++){

TE
CH

5
...
printf("%d \n",marks[i]);
8
...
return 0;
10
...

1
...
h>
2
...
{
4
...


int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};

6
...


{

8
...


{

10
...


{

C - LANGUAGE

183

HI - TECH COMPUTERS
12
...


a[i] = a[j];

14
...


}

16
...


printf("Printing Sorted Element List
...


for(i = 0; i<10; i++)

20
...


printf("%d\n",a[i]);

22
...


TE
CH

17
...
The 2D array is organized as matrices which can be
represented as the collection of rows and columns
...
It provides ease of holding the bulk
of data at once which can be passed to any number of functions
wherever required
...

1
...

1
...

Initialization of 2D Array in C

In the 1D array, we don't need to specify the size of the
array if the declaration and initialization are being done
will

have

to

-

simultaneously
...
We
define

at

least

the

second dimension of the

array
...


HI

1
...
#include ...
int main(){

3
...
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
5
...
for(i=0;i<4;i++){
7
...


printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);

9
...


}//end of i

C - LANGUAGE

185

HI - TECH COMPUTERS
11
...


}

Output
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3

TE
CH

arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6

C 2D array example: Storing elements in a matrix and printing
it
...
#include ...
void main ()
3
...


for (i=0;i<3;i++)

HI

4
...


{

7
...


{

9
...

11
...


}

13
...
\n");

14
...


{

16
...


for (j=0;j<3;j++)

18
...


printf("%d\t",arr[i][j]);

20
...

22
...


C - LANGUAGE

187

HI - TECH COMPUTERS
Return an Array in C
An array is a type of data structure that stores a fixed-size
of a homogeneous collection of data
...

For example, if we want to declare 'n' number of variables,
n1, n2
...
, if we create all these variables individually,
then it becomes a very tedious task
...
Each element of an

TE
CH

array can be accessed using an index of the element
...


Passing array to a function
1
...
h>

2
...
{

printf("Elements of array are : ");

5
...


{

-

4
...


printf("%d ", arr[i]);

8
...
}
int main()

HI

10
...


{

12
...


getarray(arr);

14
...


}

In the above program, we have first created the array arr[]
and then we pass this array to the function getarray()
...


C - LANGUAGE

188

array

HI - TECH COMPUTERS

TE
CH

Output

Passing array to a function as a pointer

Now, we will see how to pass an array to a function as a
pointer
...
#include ...
void printarray(char *arr)
3
...


for(int i=0;i<5;i++)

6
...


-

4
...


}

9
...

11
...


char arr[5]={'A','B','C','D','E'};

13
...


return 0;

15
...
The function printarray() prints the elements of an
array
...


How to return an array from a function

Returning pointer pointing to the array
1
...
h>
2
...
{
int arr[5];

5
...


for(int i=0;i<5;i++)

7
...


8
...


10
...


}

12
...


{

14
...


n=getarray();

16
...


for(int i=0;i<5;i++)

18
...

20
...

22
...


It

returns

a

local

variable,

but

it is an illegal

memory location to be returned, which is allocated within a
function in the stack
...
Therefore, we can say that this program is returning

TE
CH

memory location, which is already de-allocated, so the output
of the program is a segmentation fault
...

For example, consider a function which sorts the 10 elements
in ascending order
...
Here,
instead of declaring 10 different numbers and then passing
into the function, we can declare and initialize an array and
that

into

the

function
...


As we know that the array_name contains the address of the
first element
...
The array defined as the formal parameter
will automatically refer to the array specified by the array
name defined as an actual parameter
...
#include ...
int minarray(int arr[],int size){
3
...
int i=0;
5
...
if(min>arr[i]){
7
...
}

9
...


return min;

11
...

13
...


int i=0,min=0;

15
...

17
...


printf("minimum number is %d \n",min);

19
...


}

Output

HI

-

TE
CH

minimum number is 3

C - LANGUAGE

193

HI - TECH COMPUTERS
C Pointers
The

pointer

in

C

language

is

a

variable which stores the

address of another variable
...
The size of the
pointer

depends

on

the

architecture
...

Consider

the

following

example

to

define

a

pointer

which

1
...

2
...

Declaring a pointer

The pointer in c language can be declared using * (asterisk
symbol)
...


as

indirection

pointer

used

to

1
...
char *c;//pointer to char
Pointer Example
given below
...
e
...


The

value

of

HI

number variable is 50
...


By the help of * (indirection operator), we can print the
value of pointer variable p
...


C - LANGUAGE

194

above

HI - TECH COMPUTERS
1
...
h>
2
...
int number=50;
4
...
p=&number;//stores the address of number variable
6
...


TE
CH

7
...

8
...
}
Output

Address of number variable is fff4
Address of p variable is fff4

-

Value of p variable is 50
Pointer to array

1
...
int

*p[10]=&arr;

//

Variable

p

of

type

pointer

is

HI

pointing to the address of an integer array arr
...
void show (int);
2
...


and

used

with

arrays, structures, and functions
...

C - LANGUAGE

195

HI - TECH COMPUTERS
3) It makes you able to access any memory location in the
computer's memory
...

1) Dynamic memory allocation
In

c

language,

we

can

dynamically

allocate

memory

using

malloc() and calloc() functions where the pointer is used
...


It

TE
CH

Pointers in c language are widely used in arrays, functions,
reduces

performance
...

But, we need to use %u to display the address of a variable
...
#include ...
int main(){
3
...
printf("value

%u",number,&number);
5
...
}

HI

Output
value of number is 50, address of number is fff4
NULL Pointer

A pointer that is not assigned any value but NULL is known as
the

NULL

pointer
...
It will provide a better approach
...

Pointer

Program

to

swap two numbers without using the 3rd

variable
...
#include ...
int main(){
3
...

5
...
*p1=*p1+*p2;
7
...
*p1=*p1-*p2;
10
...


return 0;

12
...
printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);

Before swap: *p1=10 *p2=20

HI

-

After swap: *p1=20 *p2=10

C - LANGUAGE

197

HI - TECH COMPUTERS
C Double Pointer (Pointer to Pointer)
As we know that, a pointer is used to store the address of a
variable in C
...

However,

In C, we can also define a pointer to store the

address of another pointer
...
The first pointer is used to
store the address of a variable whereas the second pointer is
used

to

store

the

address

of

the

first

pointer
...


The syntax of declaring a double pointer is given below
...
int **p; // pointer to a pointer which is pointing to an
integer
...


-

1
...
h>
2
...
{

int a = 10;

5
...

6
...


p = &a; // pointer p is pointing to the address of a

8
...


printf("address of a: %x\n",p); // Address of a will
be printed

10
...


printf("value stored at p: %d\n",*p); // value

stoted at the address contained by p i
...
10 will be
printed
C - LANGUAGE

198

HI - TECH COMPUTERS
12
...


}

Output
address of a: d26a8734
address of p: d26a8738
value stored at pp: 10
C double pointer example

TE
CH

value stored at p: 10

Let's see an example where one pointer points to the address

-

of another pointer
...


1
...
h>
2
...
int number=50;
4
...
int **p2;//pointer to pointer
6
...
p2=&p;
8
...
printf("Address of p variable is %x \n",p);

C - LANGUAGE

199

HI - TECH COMPUTERS
10
...


printf("Address of p2 variable is %x \n",p2);

12
...


return 0;

14
...
However, as we know that pointer
contains the address, the result of an arithmetic operation
performed on the pointer will also be a pointer if the other
operand

is

of

type

integer
...
Following
arithmetic

operations

are

● Increment
● Decrement
● Addition
● Subtraction
Incrementing Pointer in C
If

we

increment

pointing

to

the

a

on

the

pointer

in

C

TE
CH

language:

possible

pointer

immediate

by

next

1,

the

pointer

location
...

We can traverse an array by using the increment operation on a
pointer

which

will

keep

pointing

to

every

element

of

the

HI

array, perform some operation on that, and update itself in a
loop
...
new_address= current_address + i * size_of(data type)

Where i is the number by which the pointer get increased
...

64-bit
For 64-bit int variable, it will be incremented by 4 bytes
...

C - LANGUAGE

201

on

HI - TECH COMPUTERS
1
...
h>
2
...
int number=50;
4
...
p=&number;//stores the address of number variable
6
...
p=p+1;
\n",p);

//

in

increment:
our

bytes
...
return 0;
10
...
printf("After

Address of p variable is 3214864300

After increment: Address of p variable is 3214864304
Decrementing Pointer in C

Like increment, we can decrement a pointer variable
...
The formula of decrementing the pointer is given
below:

1
...

64-bit

For 64-bit int variable, it will be decremented by 4 bytes
...


1
...
h>
2
...
int number=50;
4
...
p=&number;//stores the address of number variable
C - LANGUAGE

202

on

HI - TECH COMPUTERS
6
...
p=p-1;
8
...

9
...
The formula of
adding value to pointer is given below:
1
...

64-bit

-

For 64-bit int variable, it will add 4 * number
...


HI

1
...
h>
2
...
int number=50;
4
...
p=&number;//stores the address of number variable
6
...
p=p+3;

//adding 3 to pointer variable

8
...
return 0;
10
...
But after
adding

3

with

increment
...
e
...
But if we were using 32-bit architecture, it
incrementing

to

6 only, i
...
, 2*3=6
...

C Pointer Subtraction
Like

pointer

addition,

we

can

subtract

a

value

from

the

pointer variable
...
The formula of subtracting value from the
pointer variable is given below:
1
...

64-bit

For 64-bit int variable, it will subtract 4 * number
...

1
...
h>
2
...
int number=50;
4
...
p=&number;//stores the address of number variable
6
...
p=p-3; //subtracting 3 from pointer variable
8
...
return 0;
10
...


TE
CH

However, instead of subtracting a number, we can also subtract
an address from another address (pointer)
...
It will not be a simple arithmetic operation, but it
will follow the following rule
...
For example, if we declare the int pointer, then
this int pointer cannot point to the float variable or some

-

other type of variable, i
...
, it can point to only int type
variable
...

A pointer to void means a generic pointer that can point to
any data type
...


Syntax of void pointer
1
...
According to C perception, the
representation of a pointer to void is the same as the pointer

C - LANGUAGE

205

HI - TECH COMPUTERS
of character type
...

Let's look at the below example:
1
...
h>
2
...
{
4
...


int *p

6
...


float *fp = NULL;//float pointer

8
...


printf("size of void pointer = %d\n\n",sizeof(ptr));

TE
CH

10
...


printf("size

%d\n\n",sizeof(p));
12
...


printf("size

of

-

%d\n\n",sizeof(cp));
14
...


printf("size

of

float

pointer

%d\n\n",sizeof(fp));
return 0;

HI

16
...


}

Output

C - LANGUAGE

206

=

HI - TECH COMPUTERS
What is a Null Pointer?
A Null Pointer is a pointer that does not point to any memory
location
...
The null
pointer basically stores the Null value while void is the type
of the pointer
...
Here, Null means that the pointer is
referring to the 0th memory location
...
When a NULL value
is assigned to the pointer, then it is considered as a Null
pointer
...


● It is used to perform error handling with pointers before

-

dereferencing the pointers
...

Let's look at the situations where we need to use the null
pointer
...


1
...
h>
2
...
{
4
...


printf("Address: %d", ptr); // printing the value of
ptr
...


printf("Value:

%d",

*ptr);

//

dereferencing

the

illegal pointer
7
...
}
In the above code, we declare the pointer variable *ptr, but
it

does

not

contain

the

address

of

any

variable
...


TE
CH

According to the stack memory concept, the local variables of
a function are stored in the stack, and if the variable does
not contain any value, then it shows the garbage value
...


Therefore,

we

can

say

that

keeping

an

uninitialized pointer in a program can cause serious harm to
the computer
...
A

-

null pointer is a pointer pointing to the 0th memory location,
which is a reserved memory and cannot be dereferenced
...
#include ...
int main()

HI

3
...


int *ptr=NULL;

5
...


{

7
...


}

9
...


{

11
...


}

13
...


return 0;
}

C - LANGUAGE

208

HI - TECH COMPUTERS
In the above code, we create a pointer *ptr and assigns a NULL
value to the pointer, which means that it does not point any
variable
...


C - LANGUAGE

209

HI - TECH COMPUTERS
C Strings
The

string

can

be defined as the one-dimensional array of

characters terminated by a null ('\0')
...
Each character in the array occupies one byte of
memory,

and

the

last

character

must

always

be

0
...
When we
a

string

as

char

s[10],

the

character

s[10]

TE
CH

define

is

implicitly initialized with the null in the memory
...

1
...
By string literal

Let's see the example of declaring string by char array in C
language
...
char ch[10]={'j', 'a', 'v', 'a', '\0'};
As

we

know,

array

index

starts

from

0,

so

it

will

be

-

represented as in the figure given below
...
So we can write
the above code as given below:
1
...
For example:
1
...


Difference between char array and string literal
There are two main differences between char array and literal
...


C - LANGUAGE

210

HI - TECH COMPUTERS
● The string literal cannot be reassigned to another set of
characters whereas, we can reassign the characters of the
array
...
The '%s' is used as a format specifier for the

1
...
h>
2
...
h>
3
...

5
...


char ch[11]={'j', 'a', 'v', 'a', '\0'};
char ch2[11]="java";

6
...


printf("Char Array Value is: %s\n", ch);

8
...
return 0;
10
...
h
...

C gets() function
The gets() function enables the user to enter some characters
followed by the enter key
...
The null character is
user

to

enter

the

TE
CH

added to the array to make it a string
...


It

returns

the

string entered by the user
...
char[] gets(char[]);

Reading string using gets()
1
...
h>
2
...
{
char s[30];

5
...


gets(s);

7
...


8
...
It suffers from buffer
overflow, which can be avoided by using fgets()
...
Consider the following example
...
#include ...
void main()
3
...


char str[20];

5
...


fgets(str, 20, stdin);

7
...
}

TE
CH

Output

Enter the string? java is the best
java is the best
C puts() function

The puts() function is very much similar to printf() function
...

The puts() function returns an integer value representing the
number of characters being printed on the console
...


HI

Declaration
1
...

1
...
h>
2
...
h>
3
...
char name[50];
5
...
gets(name); //reads string from user
7
...
puts(name);
C - LANGUAGE

//displays string
213

HI - TECH COMPUTERS
9
...


}

Output:
Enter your name: Ram

HI

-

TE
CH

Your name is: Ram

C - LANGUAGE

214

HI - TECH COMPUTERS
C String Functions
There

are

many

important

string

functions

defined

"string
...

No

Function

Description

strlen(string_name)

returns


...

copies

the

contents

of

source

string to destination string
...
The result
of the string is stored in first
string
...
If both strings

-

are same, it returns 0
...


6)

strlwr(string)

returns

HI

5)

7)

strupr(string)

C - LANGUAGE

string

characters

in

characters

in

lowercase
...


215

in

HI - TECH COMPUTERS
C String Length: strlen() function
The strlen() function returns the length of the given string
...

1
...
h>
2
...
h>
3
...
char ch[20]={'j', 'a', 'v', 'a', '\0'};
printf("Length of string is: %d",strlen(ch));

6
...
}
Output:
Length of string is: 4
C Copy String: strcpy()
The

strcpy(destination,

string in destination
...
#include ...


source)

function

copies

the

source

2
...
h>

-

3
...
char ch[20]={'j', 'a', 'v', 'a', '\0'};
char ch2[20];

6
...


printf("Value of second string is: %s",ch2);

HI

5
...
return 0;
9
...

1
...
h>
2
...
h>
3
...


char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};

5
...


strcat(ch,ch2);

7
...
return 0;
9
...


Here, we are using gets() function which reads string from the
console
...
h>
#include ...
Let's see a simple example of strrev() function
...
#include ...
#include ...
int main(){
char str[20];

5
...


gets(str);//reads string from console

7
...


printf("\nReverse String is: %s",strrev(str));

9
...


}

Output:
Enter string: java
String is: java
Reverse String is: avaj

TE
CH

4
...
Let's see a simple example of strlwr() function
...
#include ...
#include ...
int main(){
4
...


printf("Enter string: ");

6
...


printf("String is: %s",str);

8
...
return 0;
10
...
Let's see a simple example of strupr() function
...
#include ...
#include ...
int main(){
char str[20];

5
...


gets(str);//reads string from console

7
...


printf("\nUpper String is: %s",strupr(str));

9
...


}

Output:
Enter string: java
String is: java
Upper String is: JAVA

TE
CH

4
...
It is used to

return substring from first match till the last character
...
char *strstr(const char *string, const char *match)

String strstr() parameters
string: It represents the full string from where substring
will be searched
...

1
...
h>
2
...
h>
3
...


char str[100]="this is java with c and java";

5
...


sub=strstr(str,"java");

7
...
return 0;
9
...
h> header file
...
h> header file contains various methods for performing
mathematical operations such as sqrt(), pow(), ceil(), floor()
etc
...
h header file
...

1)

TE
CH

used functions of math
...


greater

than

given

or

number
...

2)

floor(number)

rounds
returns

down
the

the

given

integer

number
...


sqrt(number)

returns

the

square

root

of

given

HI

number
...


exponent)

5)

abs(number)

C - LANGUAGE

returns the absolute value of given
number
...
h
header file
...
#include ...
#include ...
int main(){
4
...
6));
5
...
3));

TE
CH

6
...
6));
7
...
2));
8
...
printf("\n%f",sqrt(7));
10
...


printf("\n%f",pow(3,3));

12
...

14
...
000000
4
...
000000

HI

3
...
000000
2
...
000000
27
...
Each element of
a structure is called a member
...
Let's see

TE
CH

the syntax to define the structure in c
...
struct structure_name
2
...


data_type member1;

4
...



...



...


data_type memeberN;

8
...

1
...


char name[20];

4
...
{

5
...
There are two ways
to declare structure variable:
1
...
By

declaring

a

variable

at

the

time

of

defining

structure
...


It

should

be

declared

within

the

main

function
...
struct employee
2
...


char name[50];

4
...
};

TE
CH

Now write given code inside the main() function
...
struct employee e1, e2;
The

variables e1 and e2 can be used to access the values

stored in the structure
...

2nd way:
Let's

see

another

way

defining the structure
...
struct employee
int id;

3
...


float salary;

5
...
{

to

Which approach is good

HI

If number of variables are not fixed, use the 1st approach
...


If no
...
It saves your
code to declare a variable in main() function
...
By
...
By -> (structure pointer operator)
Let's see the code to access the id member of p1 variable by
...

1
...
id
C - LANGUAGE

224

HI - TECH COMPUTERS
C Structure example
Let's see a simple example of structure in C language
...
#include ...
#include ...
struct employee
4
...


char name[50];
//declaring e1 variable for structure

7
...
{
9
...
}e1;

//store first employee information

10
...
id=101;

11
...
name, "Ram");//copying string into char

array
12
...


printf( "employee 1 id : %d\n", e1
...


printf( "employee 1 name : %s\n", e1
...


}

Output:

-

15
...

1
...
h>
2
...
h>
3
...
{

int id;

5
...


float salary;
//declaring e1 and e2 variables for structure

8
...
{

TE
CH

7
...


//store first employee information

11
...
id=101;

12
...
name, "Ram");//copying string into char

array
13
...
salary=56000;

14
...


//store second employee information
e2
...


strcpy(e2
...


e2
...


19
...

21
...
id);

22
...
name);

23
...
salary);

24
...


//printing second employee information

26
...
id);

27
...
name);

28
...
salary);

29
...


}

Output:
C - LANGUAGE

226

HI - TECH COMPUTERS
employee 1 id : 101
employee 1 name : Ram
employee 1 salary : 56000
...
000000

C - LANGUAGE

227

HI - TECH COMPUTERS
typedef in C
The typedef is a keyword used in C programming to provide some
meaningful names to the already existing variable in the C
program
...
In short, we can say that this keyword is used to
redefine the name of an already existing variable
...
typedef

In the above syntax, 'existing_name' is the name of an already
existing variable while 'alias name' is another name given to
the existing variable
...


To

overcome

the

problem, we use a typedef keyword
...
typedef unsigned int unit;

-

In the above statements, we have declared the unit variable of
type unsigned int by using a typedef keyword
...
unit a, b;
instead of writing:
1
...
This keyword is useful when we are dealing
with the long data type especially, structure declarations
...

1
...
h>
2
...
{
4
...
unit i,j;
6
...
j=20;

TE
CH

8
...
printf("\nValue of j is :%d",j);
10
...


}

Output
Value of i is :10
Value of j is :20

Using typedef with structures

Consider the below structure declaration:

-

1
...
{

3
...
int age;

HI

5
...
struct student s1;

In

the

above

structure

declaration,

we

have

created

the

variable of student type by writing the following statement:
1
...
e
...


To

avoid

such

a big

statement, we use the typedef keyword to create the variable
of type student
...
struct student
2
...
char name[20];
4
...
};
6
...
stud s1, s2;

TE
CH

In the above statement, we have declared the variable stud of
type struct student
...

The above typedef can be written as:
1
...
{
3
...
int age;
5
...
stud s1,s2;

-

From the above declarations, we conclude that typedef keyword
reduces the length of the code and complexity of data types
...

Let's

see

another

example

where

we

typedef

the

structure

HI

declaration
...
#include ...
typedef struct student
3
...
char name[20];
5
...
}stud;
7
...
{
9
...


printf("Enter the details of student s1: ");

11
...


scanf("%s",&s1
...


printf("\nEnter the age of student:");

14
...
age);

15
...
name);

16
...
age);

17
...


}

Output

TE
CH

Enter the details of student s1:

Enter the name of the student: Peter
Enter the age of student: 28

Name of the student is : Peter

HI

-

Age of the student is : 28

C - LANGUAGE

231

HI - TECH COMPUTERS
C Array of Structures
Consider

a

case,

where

we

need

to

store

the

data

of

5

students
...

1
...
h>
2
...


char name[20];

5
...


float marks;

7
...
void main()
9
...
{

10
...


int dummy;

12
...
name,&s1
...
marks);

14
...


-

13
...
name,&s2
...
marks);

17
...

18
...


scanf("%s %d %f",s3
...
id,&s3
...


scanf("%c",&dummy);

21
...
\n");

22
...
name,s1
...
marks);

23
...
name,s2
...
marks);

24
...
name,s3
...
marks);

25
...

James 90 90
...
000000
Nick 90 90
...


be

increased if there are 20 students
...


This

will

always

be

tough

since

we

will

have to

declare a variable every time we add a student
...

However, c enables us to declare an array of structures by
using which, we can avoid declaring the different structure
variables; instead we can make a collection containing all the
structures that store the information of different entities
...
The array of structures

HI

in C are used to store information about multiple entities of
different data types
...


C - LANGUAGE

233

HI - TECH COMPUTERS
Let's see an example of an array of structures that stores
information of 5 students and prints it
...
#include ...
#include ...
struct student{
4
...
char name[10];
7
...
int i;

TE
CH

6
...
struct student st[5];

printf("Enter Records of 5 students");

11
...


printf("\nEnter Rollno:");

13
...
rollno);

14
...


scanf("%s",&st[i]
...


}

17
...


for(i=0;i<5;i++){

19
...
rollno,st[i]
...


}

-

10
...

22
...
For example, we may need to store the address of an
entity employee in a structure
...

Hence, to store the address of the employee, we need to store
the address of the employee into a separate structure and nest
the following program
...
#include ...
struct address
3
...


char city[20];

5
...


char phone[14];

TE
CH

the structure address into the structure employee
...
};

-

8
...
{
10
...


struct address add;
};

HI

12
...


void main ()

14
...


struct employee emp;

16
...


scanf("%s

%s

%d

%s",emp
...
add
...
add
...
add
...


printf("Printing the employee information
...


printf("name: %s\nCity: %s\nPincode: %d\nPhone:

%s",emp
...
add
...
add
...
add
...


}

C - LANGUAGE

236

HI - TECH COMPUTERS
Output
Enter employee information?
Arun
Delhi
110001
1234567890
Printing the employee information
...
The union
statement defines a new data type with more than one member
for your program
...

member definition;

TE
CH

member definition;

} [one or more union variables];

The union tag is optional and each member definition is a
normal variable definition, such as int i; or float f; or any
other valid variable definition
...
Here is the way you
f, and str −
union Data {

-

would define a union type named Data having three members i,

int i;

HI

float f;
char str[20];

} data;
Now,

a

variable

of

Data

type

can

store

an

integer,

a

floating-point number, or a string of characters
...
e
...
You can use any built-in or user
defined data types inside a union based on your requirement
...
For example, in the above
example,

Data

C - LANGUAGE

type

will

occupy

20

bytes

of

memory
238

space

HI - TECH COMPUTERS
because this is the maximum space which can be occupied by a
character

string
...
h>
#include ...


Sometimes,

it

is

not

enough to only display the data on the console
...
However, if we need to do so,

TE
CH

we may store it onto the local file system which is volatile
and can be accessed every time
...


File handling in C enables us to create, update, read, and
delete the files stored on the local file system through our C
program
...

● Creation of the new file
● Opening an existing file
● Reading from the file
● Writing to the file

-

● Deleting the file

Functions for file handling
There

are

many

functions

in

the

C

library to open, read,

HI

write, search and close the file
...


C - LANGUAGE

241

HI - TECH COMPUTERS
We can use one of the following modes in the fopen() function
...
It sends formatted output to a stream
...
int fprintf(FILE *stream, const char *format [, argument,

...
#include ...
main(){

TE
CH

Example:

3
...


fp = fopen("file
...


fprintf(fp, "Hello file by fprintf
...


fclose(fp);//closing file

7
...
It reads a word from the file and returns EOF at the end
of file
...
int fscanf(FILE *stream, const char *format [, argument,

...
#include ...
main(){
3
...


FILE *fp;

char buff[255];//creating char array to store data of
file

5
...
txt", "r");

6
...


printf("%s ", buff );

8
...

10
...

C File Example: Storing employee information
Let's

see

a

file

handling

example

to

store

employee

information as entered by user from console
...
#include ...
void main()
3
...


4
...


int id;

6
...


float salary;

8
...
txt", "w+");/*

9
...


open for writing */

11
...


return;

}

14
...

15
...


fprintf(fptr, "Id= %d\n", id);

17
...


scanf("%s", name);

19
...


printf("Enter the salary\n");

21
...


fprintf(fptr, "Salary= %
...


fclose(fptr);

24
...
For windows operating

TE
CH

system, go to TC\bin directory, you will see emp
...
It
will have following information
...
txt
Id= 1
Name= sonoo

HI

-

Salary= 120000

C - LANGUAGE

245

HI - TECH COMPUTERS
C fputc() and fgetc()
Writing File : fputc() function
The fputc() function is used to write a single character into
file
...

Syntax:
1
...
main(){

TE
CH

1
...
h>
3
...


fp = fopen("file1
...


fputc('a',fp);//writing single character into file

6
...
}
file1
...

It gets a character from the stream
...

Syntax:

HI

1
...
#include ...
#include ...
void main(){
4
...
char c;
6
...
fp=fopen("myfile
...

9
...


printf("%c",c);

C - LANGUAGE

246

HI - TECH COMPUTERS
11
...


fclose(fp);

13
...


}

myfile
...
Let's see examples of writing and
reading file using fgets() and fgets() functions
...
It
outputs string to a stream
...
int fputs(const char *s, FILE *stream)
Example:

-

1
...
h>
2
...
h>
3
...
FILE *fp;

HI

5
...


7
...
txt","w");
8
...


10
...


getch();

12
...
txt
hello c programming

C - LANGUAGE

247

HI - TECH COMPUTERS
Reading File : fgets() function
The fgets() function reads a line of characters from file
...

Syntax:
1
...
#include ...
void main(){
4
...
char text[300];
6
...


TE
CH

2
...
h>

8
...
txt","r");

9
...

fclose(fp);

12
...


}

Output:

-

11
...

No

Macro

Description

_DATE_

represents


...

represents current time in "HH:MM:SS"
format
...


4

_LINE_

represents current line number
...
c
1
...
h>
2
...


printf("Date :%s\n", __DATE__ );

5
...


printf("Line :%d\n", __LINE__ );

7
...


8
...
c

Date :Dec 6 2015
Time :12:28:46
Line :6

C - LANGUAGE

249

HI - TECH COMPUTERS
C Expressions
An expression is a formula in which operands are linked to
each other by the use of operators to compute a value
...

Let's see an example:
1
...


There are four types of expressions exist in C:
● Arithmetic expressions
● Relational expressions
● Logical expressions

● Conditional expressions

Each type of expression takes certain types of operands and
uses a specific set of operators
...

For example:

-

1
...
The

HI

portion after the equal is an expression
...
An arithmetic expression

computes a value of type int, float or double
...


TE
CH

contains both integral and real operands, it is known as mixed

public class ArithmeticExceptionTest {

public static void main(String[] args) {
int a = 0, b = 10;
int c = b/a;

System
...
println("Value of c is : "+ c);
}
}

-

Relational Expressions
● A relational expression is an expression used to compare
two operands
...

● In

relational

expressions,

a

numeric

value

cannot

be

compared with the string value
...


Here,

the

zero

value

is

equivalent to a false and non-zero value is equivalent to
true
...


The

relational

expression results in value 1 if x is an even
number otherwise results in value 0
...
This relational expression results in 1 if
a is not equal to b otherwise 0
...


a>=9

It is used to check whether the value of a is

HI

-

greater than or equal to 9
...
#include ...
int main()
3
...

5
...


if(x%2==0)

7
...


}

TE
CH

8
...


else

11
...


return 0;

13
...

● It is a complex test condition to take a decision
...

Logical

Description

Expressions
It is a test condition to check whether the

( x < 6 )

x is greater than 4 and x is less than 6
...

x > 10 || y

It is a test condition used to check whether

<11

x is greater than 10 or y is less than 11
...


It is a test condition used to check whether

&& ( y = = 2

x is not greater than 10 and y is equal to

)

-

! ( x > 10 )

2
...


C - LANGUAGE

254

HI - TECH COMPUTERS
Let's see a simple program of "&&" operator
...
#include ...
int main()
3
...


int x = 4;

5
...


if ( (x <10) && (y>5))

7
...


}

TE
CH

8
...


else

11
...


return 0;

13
...
#include ...
int main()
3
...


int x = 4;

5
...


if ( (x <6) || (y>10))

7
...


}

TE
CH

8
...


else

11
...


return 0;

13
...

● A

conditional

operator

is

also

known

as

a

ternary

operator
...

exp1 ? exp2 : exp3
above

expression

is

a

conditional

expression

which

TE
CH

The

is

evaluated on the basis of the value of the exp1 expression
...


Let's understand through a simple example
...
#include ...
#include ...
int main()
4
...


char status;

7
...


if(status == 'M')

9
...


10
...


printf("Unmarried");

12
...


}

Output

C - LANGUAGE

257

HI - TECH COMPUTERS
Factorial Program in C
Factorial Program in C: Factorial of n is the product of all
positive descending integers
...

For example:
1
...
3! = 3*2*1 = 6
Here, 5! is pronounced as "5 factorial", it is also called "5
bang" or "5 shriek"
...

There

are

many

ways

to

write

the

factorial

program

in

c

language
...

Exception Handling in Java - Java
● Factorial Program using loop

● Factorial Program using recursion
Factorial Program using loop

Let's see the factorial Program using loop
...
#include ...
int main()
3
...
int i,fact=1,number;

5
...

7
...

9
...


printf("Factorial of %d is: %d",number,fact);

11
...


}

Output:
Enter a number: 5
Factorial of 5 is: 120

C - LANGUAGE

258

HI - TECH COMPUTERS
Factorial Program using recursion in C
Let's see the factorial program in c using recursion
...
#include ...

3
...


if (n == 0)

6
...


else

8
...
{

return(n * factorial(n-1));

9
...

11
...


{

13
...


long fact;

15
...


scanf("%d", &number);

-

17
...


fact = factorial(number);

19
...


return 0;
}

HI

21
...
There
are two common ways to swap two numbers without using third
variable:
1
...
By * and /
Program 1: Using + and third variable
...
#include ...
int main()
3
...
int a=10, b=20;

TE
CH

Let's see a simple c example to swap two numbers without using

5
...
a=a+b;//a=30 (10+20)
7
...
a=a-b;//a=20 (30-10)

-

9
...


return 0;

11
...

1
...
h>
2
...
h>
3
...
{
5
...
printf("Before swap a=%d b=%d",a,b);
7
...
b=a/b;//b=10 (200/20)
9
...


system("cls");

11
...


return 0;

13
...
In this program, we are getting number as input
from the user and reversing that number
...

1
...
h>
2
...
{

TE
CH

4
...
printf("Enter a number: ");
6
...


while(n!=0)

8
...


rem=n%10;

10
...


n/=10;

12
...


printf("Reversed Number: %d",reverse);
return 0;

15
...


Enter a number: 123

HI

Reversed Number: 321

C - LANGUAGE

262

HI - TECH COMPUTERS
Prime number
Prime number in C: Prime number is a number that is greater
than

1 and divided by 1 or itself
...

For example 2, 3, 5, 7, 11, 13, 17, 19, 23
...

#include ...
h>

printf("Enter the Number: ");
scanf("%d",&n);

HI

-

if(n==0 || n==1)
{
printf("it is not Prime Number
...
For example 121, 34543, 343, 131, 48984
are the palindrome numbers
...
In this c program, we
will get an input from the user and check whether number is
palindrome or not
...
#include ...
int main()
3
...
int n,r,sum=0,temp;

-

5
...
scanf("%d",&n);
7
...
while(n>0)

HI

9
...


r=n%10;

11
...


n=n/10;

13
...


if(temp==sum)

15
...


else

17
...


return 0;

19
...

Armstrong number is a number that is equal to the sum of cubes
of its digits
...

Let's try to understand why 153 is an Armstrong number
...
where:
3
...
(5*5*5)=125
5
...
So:
7
...
153 = (1*1*1)+(5*5*5)+(3*3*3)

Let's try to understand why 371 is an Armstrong number
...
371 = (3*3*3)+(7*7*7)+(1*1*1)

-

2
...
(3*3*3)=27

4
...
(1*1*1)=1

HI

6
...
27+343+1=371

Let's see the c program to check Armstrong Number in C
...
#include ...
int main()
3
...
int n,r,sum=0,temp;
5
...
scanf("%d",&n);
7
...
while(n>0)
9
...


r=n%10;

11
...


n=n/10;

13
...


if(temp==sum)

15
...


else

17
...


return 0;

19
...
In this program, we are taking input from the user and
iterating this number until it is 0
...

1
...
h>
2
...
h>
3
...
long int n,sum=0,r;
5
...


6
...
scanf("%ld",&n);
8
...
{
r=n%10;

11
...


n=n/10;

13
...


n=sum;

15
...


16
...


r=n%10;

18
...


{

20
...


printf("one ");

22
...


case 2:

24
...


break;

26
...


break;

29
...


printf("four ");

31
...


case 5:

33
...


break;

35
...


printf("six ");

37
...


case 7:

39
...


break;

41
...


printf("eight ");

43
...


case 9:

45
...


break;

47
...


printf("zero ");

49
...


TE
CH

HI - TECH COMPUTERS

50
...


printf("tttt");

52
...


}

54
...


}

56
...


}

Output:
enter the number=4321
four three two one
C - LANGUAGE

269

HI - TECH COMPUTERS
HR Interview Questions | Common Interview Questions
A list of frequently asked HR interview questions or Common
interview questions or Job interview questions and answers are
given below
...
This
puts

all

the

pressure

on

the

candidate,

and

TE
CH

question

interviewer relax
...
You should
start with an easy and confident tone and answer in a proper
manner
...
Always remember, you are
not giving the interview to a robot so your articulation, your
pronunciation of each word should be clear and confident
...


-

First of all, thank you for giving me this opportunity to
introduce myself
...


As far as my education qualification is concerned, I have done
with

finance

HI

MBA

stream

from

Srivenkateswara university in

Emerald's P
...
College, Tirupathi, in the year of 2014
...
tech from N
...
T Jaipur in 2012
...
I
...
Allahabad
...


My

father

is

a

Businessman,

and

my

Mother

is

a

homemaker
...

I am good in programming languages C, C++, and Java and very
much interested in HTML, CSS, ASP
...

My strength is self-confidence, positive attitude, hard work
...


C - LANGUAGE

270

HI - TECH COMPUTERS
My hobbies are: Watching news channels, Playing volleyball,
Listening to music
...
I am Anshika Bansal
...
I
have

done

my

B
...

coming

including

to

my

family

members,

there

are

4

members

TE
CH

While

me
...
My younger sister will appear her 12th CBSE board
exam this year
...


I

am

a

cool

hearted

person,

so usually see every

difficulty with a positive side and keep myself always smiling
which makes me stronger even more
...

My

hobbies

are

dancing,

Internet

surfing,

playing

Chess,

-

listening to music, watching the news channel
...


HI

Thank you for giving this opportunity to introduce myself
...
I am Anshika Bansal
...
I
have

done

my

B
...

I

am

carrying

5

years

of

experience

top

Street

Companies
...


I

can

survive

fast-paced environment
...

2) Why are you applying for this job? (or)
Why this role attract you?
By this question, the interviewer wants to know that:
● If you fully understand what the job entails

TE
CH

● How well you might match their requirement
● What appeals to you most about this job

Before answering this question, take your own time an answer
in

the

way

that

convinces

the

interviewer
...

Possible Answer 1

I have applied for this vacancy because it is an excellent
match for my skills and experience
...


-

Possible Answer 2

Sir, it's a great privilege to work in a reputed company like
yours
...
Through this role, I can show

HI

my technical skills to contribute to the company growth
...
Don't
tell a lie or compromise to get the job only
...
It an extra effort means I am doing something for the
company, I'll be happy to do it
...
The work should always be more
important

than

the

money
...

Possible Answer 1
"Money is always important, but the work is most important for
me
...
If we work and

TE
CH

"I

achieve Company goals then obviously money would follow
...
"
Possible Answer 3

"Work is more important for me
...
My work
makes me stay productive, and money would naturally come along
well
...
It's vital and necessary for us to live and prosper
the

same

time,

it's

not

my single most important

driving force
...
"

HI

5) What do you know about this organization?
You

should

going

for

fully
an

aware of that organization where you are

interview,

so

check

the

history,

present

structure and working style of that organization
...

Possible Answer 1
We

all

know

that

it

is

one

of

the

fastest

growing

infrastructure company in India
...
People feel proud to be the part of your
company

as

the

company

provides

full

support

to

their

employees in professional front
...
It has crossed the branches in the
world also
...

Possible Answer 2
We all know that this company is India's no
...
I was delighted to see on your company website
that your employees are talking about how great it is to work
for your company
...
It's

TE
CH

great to see that your employees are proud to talk about how
much they love their company and jobs
...
Avoid trashing
other

employers

and

making

a

statement

like

"I

need

more

money"
...
I have learned a lot from my last
job, but now I am looking for new challenges to broaden my

-

horizons and to gain a new skill-set
...


HI

Possible Answer 1
"I believe that everyone starts with a beginning, I need a
platform

to

prove

my

abilities

and

skills
...
I need to
be a part of your growth
...
"
Possible Answer 2

"As a fresher, I need a platform to prove my ability
...
None is born with experience,
and if you hire me, I will get professional experience through
your company
...
I am ensuring you that I will put all my
efforts

for

the

good

progress

of

the

organization
...


Being

the

working

punctual

and

TE
CH

regular, I can finish the work giving to me on time and try my
best to fulfill all the needs of the company
...
"

8) What are your salary expectations?

Don't ask your salary in exact numbers, instead of this show

-

your commitment to the job itself
...
As you also know that the high cost of living here
in Delhi
...
Learning and

gaining experience is my major priority
...

Possible Answer 3
As of now, I haven't thought much about it
...


C - LANGUAGE

275

I

am

HI - TECH COMPUTERS
9) Assume you are hired, then how long would you expect to
work for us?
Possible Answer 1
"I will do my best for the growth of your company as long as I
have

the

career

growth,

job

satisfaction,

respect

and

a

healthy environment, then I don't need to change my company
...
"
Possible Answer 3

"Everyone looks for a bright future, healthy work environment,
good salary, job satisfaction and I am pretty sure that your
company

gives

such

things,

company
...
So your company gains

-

good results, and I can be in a good position to improve my
skills
...
That thought will create an interest in learning the
things
...


Possible Answer 2
I will answer this question based on some parameters
...
When it comes
to creativity, I would like to rate myself as 9
...
To talk about patience, I will tag myself with
6 because I am an entry-level professional
...


That

is

probably

why

in

most

companies,

senior

management looks more patient than entry level or even middle
level
...


HI

-

*****

C - LANGUAGE

277


Title: Full C language
Description: All detailed notes of C language with programs and examples