Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

Introduction of “C”:- C is a structured programming language which has been developed b y Dennis Ritchie in

1972 at AT & T(American Telephone & Telegraph) Bell labs. It has been developed by Microsoft Corporation.
It is a structured programming language because it follows the features by structured programming
technique.
It is also called as middle level language because it provides the facility to develop system software and
application software both by using English language (High level language). We can develop the application software
by using assembly language or machine language. We can develop the system software. Thus we can say that the “C”
editor support the high level as well as low level programming both that’s why it is called as a middle level language.
SOFTWARE SPECIFICATION FOR “C” :-
“Turbo C” is the software which provides the editor for “C” language. Actually it has three versions nowadays:-
(i) TURBO C2
(ii) TURBO C3
(iii) TURBO C 4.5
TURBO C 2 is the software for “C”. TURBO C 3 and 4.5 are the software for “C++”. Since C++ is the extension of “C”
that’s why the “C” program can be done in TURBO C3 and 4.5 also but the C++ program can’t be done in TURBO C2.
TURBO C2 is complete dos based software so it does not support the activity of mouse. But the TURBO C3
and 4.5 are the window supported dos based software that’s why it supports the activity of mouse also. The TURBO C
4.5 is complete windows based software.
TURBO C3 – IDE (Integrated development Environment)
FEATURES OF “C”
(i) “C” is a case sensitive language so all the predefined words(keywords) must be written in lower case
only.
(ii) “C” has own editor to write and execute its program.
(iii) It is a compiler based language. So its program is translated and executed by the compiler..
(iv) Every statement in “C” program must be terminated by semicolon(;) so semicolon is called as statement
terminator.
(v) “C” is a plateform independent bt architectural dependent programming language.
Platform independent means if its program has been written on windows XP operating system then it can
be executed on any version of windows and vice versa.
Architectural dependent means if the “C” program has been written on IBM based computer then it
can’t5 be executed on apple Macintosh and Intel based computer system.
Saving, Compiling and Running “C” program
 Saving:- The “C” program file can be saved by any name with extension “.c”. This file is called as source file.
 Compiling:- After writing the “C” program, either by pressing “Alt+F9” button or by selecting “Compile” option
from “Compile” menu.
During compilation phase the compiler checks the all syntactical and linking error within program. If the program has
any error then all the error are displayed at a time with their line numbers and messages. We must have to solve all
the errors within our program because if the program has been one error then it can’t be executed.
If the program has no any error that is the compilation is successful then the compiler translate whose source code
into object code and two types of files are automatically created by the compiler whose primary name is same as the
source file but the extension are as follows:-
(i) .obj:- It is called as object file and it holds the link up code of our source code and predefined
library code. The object file also hold the code in machine language but it is not a complete
machine code that’s why it can’t be executed. The size of object file is more than the size of
source file.
(ii) .exe:- It is called as an executable file. It is created automatically by the compiler according to the
object file. It holds the complete machine code of the object code, which represent to the
compiler that now our program is ready fro execution.
 Running:- After creating and executable file(.exe file), we can execute our program either by pressing Cotro+F9 or
by selecting “Run” option from “Run” menu.
NOTE:- The program may have warning also. The warning can be ignored. It means the program can be executed if it
has warning. The warning can effect on output at run time. But for a good program the program should not have
warning.
Program Structure of “C”
Every language has a definite program structure which must be followed to write and execute the program
successfully in that language. The “C” has the following program structure:-
(i) Documentation Section Optional
(ii) Link Section Essential
(iii) Definition Section Optional
(iv) Global Declaration Section Optional
(v) main() program Section Essential
{
Declaration part
Executable part

}
(vi) User Define function Section Optional
f1() f2()
{ {
………………. ……………….
………………. ……………….
} }

(i) Documentation Section:- In this section we can write the extra statement a part from the program code.
The extra data statements may have the date of creation, purpose of program, working of each module
etc.
The extra statements must be enclosed either by using following two symbols.
 //
 /* ……………..*/
All the statements enclosed within above two symbols are called as comments.
 // :- Used for single line.
Ex:- // ……………..
//……………..
 /*….*/ :- Used for multiple line.
Ex:- /* ……………..
…………….. */
The commented statements are ignored by the compilers so there is no any effect of commented
statements at run time. They can be seen only in the source code.
(ii) Link Section:- In this section we use the header files within program.
Header file is a collection of several predefined functions having extension “.h”. So we can say that
header files is a container of containing the predefined functions.
If we want to use any predefined function within program then we must have use the
corresponding header file also.

Syn:- # include<header file name>


o ( ) – Parenthesis
o { } – Curly brace
o [ ] – Square bracket
o < > - Angle bracket
o # - Preprocessor directive
The preprocessor directive (#) opens the “C” library at first and then the “include” keyword search
the specify directory in the library and add it with own program it found otherwise raise an message.
Ex:- #include<stdio.h> //Standard I/o
#include<conio.h> //console I/o
#include<math.h>
#include<stdlib.h> etc.
(iii) Definition section:- It is an optional part and it is use to create a macro by using “#define” statement.
Syn :- #define macroname value
The macroname may be any user defined name and it can hold any types of value. When the
macroname is used within program then its value is automatically submitted at that place.
Ex:- #define a 10
#define s “RAM”
#define ram Sita
(iv) Global declaration section:- It is also an optional part and it is used to create the global variables.
Ex:- #include<stdio.h>
#define O 10
int x;
float y;
main()
{
……
}
The global variables can be used from anywhere within the program.
(v) Main program section:- Every “C” program must have one and only one main() function because the
execution of the program starts from the main() function only.
A program which has no main function can’t be executed
Syn:- main()
{
Declaration part
Executable part

}
 Declaration part:- In this part we can declare the variables. These variables are called as local
variables. The scope of local variables is limited within the module only i.e. the local variables can be
used within that module only under which they have been declared.
 Executable part:- In this part we can write the executable statements.
Note:- the declaration part must appear before executable part in “C”.
Ex:- main()
{
//declaration part
int a; //local variable
float b; //local variable
char c; //local variable
// executable part
c=a+b;
printf(……………..);
}
Ex:- main()
{
int a;
float b;
c=a+b;
printf(…………….);
char m; //error
int k; //error
}
Ex:- int n; //global variable
main()
{
int x; //local variable
x=f;
n=10;
}
Ex:- f1()
{
x=10; // ×
n=10; // 
}

(vii) User Define function section:- It is an optional part which is used to create any number of user
define function. This section is used to implement the structured programming technique.
Variable
Those quantity which can store any types of data and can be changed any time are called as variables. Variable are
used to hold the constant values(data) in memory for further processing because the memory space is always
allocated by the variable name. So the constant values can’t be stored in memory directly.
Follow the following rules for the naming of variable:-
 Variable name may 32 characters lon.
 Variable name may be iehter alphabets only or alphanumeric but must always be started with
alphabets only.
 Only numerics can’t be used as variable name.
 Space is not allowed in variable name.
 Special characters like underscore can be used in variable name.
Ex:- a, aaa, abcxyz456, def, ram_kumar etc are valid.
Ex:- 45, 4abc, ram kumar etc are not valid.
Constant
Those quantities whose value can’t be changed within program are called as constant. There are basically three types
of data:-
(i) Numeric constant:-
Integer constant :- without decimal value. Ex:- +-280, +-1254 etc.
Float constant:- with decimal value.
Ex:- +-2.45, +-12.45 etc.
(ii) Character constant:- Any letter, any digit and any symbol are called as characters.
Ex:- Letter A to Z or a to z
Digit :- 0 to 9
Symbol:- +,-,%,& etc.
(iii) String constant:- Combination of more than one characters.
Ex:- Ram, Ramesh, abc489, 650, Ram Kumar%abc$462 etc.

Data type
Data type is a predefined keyword which is used to declare the type of variable so that compiler may understand
which variable will store, what types of constant data. Data type is broadly classified as follows:
Table:-……
. …..
…..
The following table shows the list of all standard data types, their keywords, size and control string:-
Data Type Keyword Size (in byte) Range Control String
Integer Int 2 -32768 to +32768 %d
long int 4 -2147483648 to %ld
+2147483647
Float Float 4 ………….. %f
Double 8 ………….. %lf
Character Char 1 -128 to +127 %c
String Char Not fixed ……………………………. %s

Variable declaration:-
Syn:- datatype variablename;

Ex:- int a; // 2 byte


int x,y ; //2 byte x and 2 byte y
float m,n,p; // 4 byte m, 4 byte n and 4 byte p
Variable declaration with assignment

Syn:- datatype variablename=value;


Ex:- int a=10;
int x=10, y=30;
float m=4.56, n=15.445;
Constant declaration

Syn:- const datatype variablename=value;

The constant must be assigned a value at the time of declaration and it can’t be change any where else within the
program. It can be used only.
Ex:- const int x=10;
X=20; // ×
Y=x+4; // 

Input-Output statement/Function
Input function:- Input function is used to input the values within program at run time. “scanf()” function is used as
input function in “C”.
Syn:- scanf(“Control String”, list of variables);

NOTE:- Use Ampersand(&) with numerical variable at the time of input.


Ex:- int a;
float b;
scanf(“%d”, &a);
scanf(“%f”, &b);
or
scanf(“%d%f”, &a,&b);
Output function :- Output function is used to print any message or value on the screen at run time. “printf()” function is used as
output function.
Syn:- printf(“message”);
printf(“Control String”, variable name);
Ex:- int a=10;
printf(“%d”, a); //10
printf(“a=%d”, a); // a=10

NOTE:- (i) printf and scanf function reside within “stdio.h” header file.
(i) Use “\n” (new line character) to break the line in output.
Ex:- printf(“Hy \n By”); //Hy
By
(iv) Use “\t” (tab) to take horizontal line space in output.
Ex:- printf(“Hy \t By”); // Hy By
Ex:- printf(“Hy \n \t By”); //Hy
By
(v) Use “\a” to take CPU alarm in output.
Ex:- printf(“Hy \a\a\a By”); // Hy By
(vi) Use “\b” to take one backspace.
Ex:- printf(“Hello”); //Hello_
Ex:- printf(“Hello \b”); //Hello
Ex:- printf(“Hello \b\b”); //Hell
Q1. Write a program to print a message on the screen .
Q2. Write a program to input two numbers and print their sum.
Q3. Write a program to input a number and calculate its square.
Q4. Write a program to input principal rate and time and calculate the simple interest.
Q5. Write a program to input two numbers and swap them with using third variable and without using third variables.
Input-Output statement/Function
Operator is an instruction which is used to perform the operation on operand(DATA). “C” supports total eight types
of operators:-
(i) Arithmetical operator
(ii) Relational/comparison operators
(iii) Logical operators
(iv) Assignment operators
(v) Increment/Decrement operators
(vi) Conditional operators
(vii) Bitwise operators.
(viii) Special operators.

(i) Arithmetical operators:- Those operators which are used to perform arithmetical operations are called as
arithmetical operators.
Operators Name Symbol
Addition +
Subtraction —
Multiplication *
Division /
Modulus %
(ii) Relational/comparison operators:- Those operators which are used to compare two operands together
are called as relational operators.
Operators Name Symbol
Greater than >
Greater than equal to >=
Less than <
Less than equal to <=
Equal to ==
Not equal to !=
(iii) Logical operators:- Those operators are used to compare two or more condition together.
Operators Name Symbol
AND &&
OR ||
Not !
(iv) Assignment operators:- Equal to (=) sign is called as assignment operators and it is use to assign some
value or expression into a variable. The assignment always take place from right to left.
Syn:- Variable = value
Variable=expression
Ex:- c=10 //
10=c // ×
a=b // 
b=a //

(v) Increment/Decrement operators:- “++” is called as increment operators and “- - “ is called as decrement
operator. It is use to increase or decrease the value of a variable by one.
Ex:- int a=5, b=4;
a++  a=a+1  a=6
There are two types of increment/decrement operators:-
(a) Prefix I/D operator:- When operator comes before on operand then it is called as prefix I/D operator.
Ex:- ++a  a=a+1
- -b  b=b-1
When it is used with assignment operator in an expression then the increment/decrement operation is
performed at 1st , after that the assignment operation will take place.
Ex:- int a=5, b=4, c;
c=++a  a=6
c=6
c= - - b  b=3
c=3
(b) Postfix I/D operator:- When operator comes after an operand then it is called as postfix I/D operator.
Ex:- a ++  a=a+1
b--  b=b-1
When it is used with assignment operator in an expression then the assignment is performed at first and
after that the I/D operation will performed.
Ex:- int a=5, b=4, C;
C=a++  a=6, c=5
C=b - -  b=c, c=4
Ex:- int a=5, b=4, C=6, d=2;
C=a++  a=6, c=5
b=- - c  b=4, c=4
d= b - -  d=4, b=3
a= ++d  a=5, d=5
finally, a=5, b=3, c=4, d=5
(vi) Conditional operator:- It is used to assign some value into a variable on some condition “?” and “: “ is
simultaneously called as conditional operator.
Syn:- V=(condition? Expr1:expr2) // expr1 is true part and expr2 is false part
Ex:- a=5, b=10, c;
C=(a<b?40:50); // c=40
C=(a>b?a+b:a-b); // c=-5
(vii) Bitwise operators:- these operators are used to perform the operation on bit values(0 and 1). It means
to perform an operation on binary data, we use to the bitwise operators.
Bitwise locical operator
Bitwise AND &
Bitwise or !
Bitwise nor ^
Bitwise complement operator
Bitwise not ~
Ex:- int a=100, b=1100, c;
C=a&b; //c=1000
(viii) Special operators:- Comma (,) and “sizeof()” operator is called as special operator. Comma(,):- It is used
to separate two variables or two expressions together.
Ex:- int a,b,c;
Sizeof() :- It accepts the name of variable, datatype and constant and returns the memory space occupied
by it.

eg:- int n,a=10;

n=sizeof(float);

printf("%d", n); //4

n=sizeof('A');
printf("%d", n); //1

Gogoxy() function
It is a performed function which accepts the column and row position of the screen and set the cursor at that
position.
Syn:- gogoxy(c,r); //c for column and r for row

The screen resolution is divided into 80×25 in dos environment.


i.e. 80 column(0 to 79)
25 row (0 to 24)
Eg:- gotoxy(25,10);
Printf(“Hello”);

Conditional statements
These statements are used to performed conditional work within program. It can execute one or more statements on
some condition. These are three types of conditional statement supported by “C”:-
(i) If - Statement:- It has one body which is called as true part.

eg:- if(condition)
{
..............; // true part
}
If the given condition remains true then the body of “if” will be executed otherwise whole body
will be skipped.
eg:- int a=5,b=10,c=2;
if(a>b)
{
c=a+b;
printf("\n %d", c); //2
}
(ii) If-else statement:- It has two body one is called as true part and other is called as false part.
Syn:- if(condition)
{
...........// true part
}
else
{
............// false part
}

If the condition is true then the true part executed otherwise false part is executed.
Nesting of if/if else
The process of using one “if” within another “if” is called nesting of “if/if-else.
syn:- if(condition)
{
..............
}
if(condition)
{
.............
}
Syn:- if(condition)
{
.............
}
if(condition)
{
...........
}
if(condition)
{
...........
}
else
{
............
}
else
{
............
}

Ladder of if/if - else


When we associate condition with else part then the chain of “if – else if – else if ………..”. This chain is called as
ladder of if or if – else.
Syn:- if(condition)
{
..........
}
else if(condition)
{
...........
}
else if(condition)
{
..........
}
Q6. Write a program to input a number and check whether it is even or odd.
Q7. Write a program to input two numbers and check which is greater.
Q8. Write a program to input a number and print its square if it is even otherwise print its next number.
Q9. Write a program to input two numbers and print their sum if 1st is greater than 2nd otherwise swap them and
then print their subtraction.

(iii) Switch statement:- It is a multiple conditional checking statement which is generally used in menu driven
programs where we have to perform only one task at a time out of several tasks.

syn:- switch(variable)
{
case value 1:
{
............
break;
}
case value 2:
{
............
break;
}
default: //optional
{
.......
}
}
The number of “case” depends upon the number of options available in the menu. One case should
perform only one task.
The “break” statement is used within every case body to terminate the switch body after execution
of any one case body.
The “default” is optional part and it is executed automatically whenever user inputs wrong choice.
So, it is generally used to display the error messages.
Goto label statement
It is used to jump the cursor within program from one part to another part. It must be used on some condition within
program because the wrong use of this statement may cause for the infinite execution of the program.
The goto statement change the normal flow of execution. Since it is used to jump the cursor from one part to
another part that’s why it is also known as jumping statements.
Since it may cause for the infinite execution of the program that’s why it is also called as unstructured looping
statement.
Syn:- goto labelname;
The label name may be any user defined name and it should not be declared as variable. The label must be indicated
at that point where we want to send the pointer as follows.
Syn:- labelname:
Ex:- main()
{
........
goto A;
.........
A:
.........
}
Ex:- main()
{
A:
Q10. Write a program to input 10 numbers and print their sum.
..........
Q11. Write a program to input a number and print its table.
goto A:
Looping .......... statements
Looping } statements are used to repeat a certain task on some
condition Ex:- main() for a certain time. The given condition must be of
Boolean { type expression. The looping statements repeat its work
unless or B: until the given condition remains true.
“C” .......... supports three types of looping statements:-
if(condition) (i) for loop:-
{ (ii) while loop
goto A; (iii) do-while loop
}
else (i) for loop:-
{
goto B;
}
..........
A:
..........
}
Syn:- for (initialization ; condition; increment/decrement)
{
…………………
}
The for loop has three parts:-
 Initialization:- In this part we assigned the initial value in looping variable which tells to the compiler
that from which value the loop will be started. This part executes only once and first time only.
 Condition:- It tells to the compiler that how many times the loop will be repeated. This part works
from first time till last execution.
 Increment/decrement:- This part specifies that by which value, the value of looping variable will be
increase or decrease. This part does not execute first time but after each repeatation of body, the
closing brace of looping sends the pointer at the increment/decrement part.

Ex:- for (a=1;a<=5;a++)


{
printf(“\n %d”, a);
}
Output
1
2
3
4
5
Ex:- for (a=5;a<=1;a--)
{
printf(“\n %d”, a);
}
Output
5
4
3
2
1

Ex:- for (a=5;a<=1;a--)


{
printf(“\n %d”, a);
}
Here, Loop does not execute

Ex:- for (a=1;a<=10;a+=2)


{
printf(“\n %d”, a);
}
Output
1
3
5
7
9
Ex:- a=1;
for ( ; a<=5 ; )
{
printf(“\n %d”, a);
a++
}
Output
1
2
3
4
5
Q 12. Write a program to print the natural numbers from one to hundred in the interval of 10.
Q.13. Write a program to input a number and print its table.
Q14. Write a program to input a number and find its factorial.
Q15. Write a program to print all the even and odd numbers separately between one and twenty
in form of EVEN ODD
………. …….
And also count total numbers of even and total numbers of odds and also print the sum of all even and
odd numbers.
Q16. Write a program to print number and its square from 1 to 10 as follows:-
Number Square
1 1
2 4
3 9
… …
10 100
(ii) while loop:-
Syn:- Initialization;
while(condition)
{
………………
Increment/decrement;
}
Ex:- a=1;
while(a<=5)
{
printf(“\n %d”, a);
a++;
}
output
1
2
3
4
5
Ex:- a=10;
while(a>=1)
{
printf(“\n %d”, a);
a-=3;
}
output
10
6
2
(iii) do-while loop:-
Syn:- initializationdo
{
…………
increment/decrement;
}while(condition);
The body of do-while must be executed at least once either the given condition remains true or false. The further
repetition depends upon the condition
Ex:- a=1;
do
{
printf(“\n %d”, a);
a++;
}while(a<=5)
output
1
2
3
4
5
Ex:- a=1;
do
{
printf(“\n %d”, a);
a++;
}while(a>=5)
output
1

Q17. Write a program to input a number and count the number of digits and also print the sum of all digits.
Q18. Write a program to input a number and check whether it is palindrome or not.
Q19. Write a program to input a number and check whether it is prime or not.
Q20. Write a program to input a number and check whether it is Armstrong or not.(153=13+53+33)
Q21. Write a program to print the fabonacii series numbers below the inputted number.
0 1 1 2 3 5 8 13 21 … … … … … … ….
Nesting of Loop:-
The process of using one loop within another looping body is called as nesting of loop

Ex:- for(……………..) Ex:- for(……………..)


{ {
for(…………..) while(condition)
{ {
……….. d0
} {
} ………………
}while(condition);
}
}
Ex:- do Ex:- while(condition
{ {
do while(condition)
{ {
……….. ………..
}while(condition); }
}while(condition); }

In case of nesting the inner loop is executed from start to end each time for each iteration of outer loop.
Ex:- for(a=1;a<=3;a++) Output
{ a=1
printf(“\n a=%d”, a); b=1
b=2
for(b=1;b<=3;b++) b=3
{ a=2
printf(“\n b=%d”,b); b=1
} b=2
} b=3
a=3
b=1
b=2
b=3
Ex:- for(a=1;a<=2;a++) Output
{ a=1
printf("\n a=%d", a); b=1
b=a++; b=3
while(b<=3) c=5
{
printf("\n b=%d", b);
b+=2;
}
c=b;
do
{
printf("\n c=%d", c);
c++;
}while(c<=5);
}

Q22. Write a program to print the series of prime numbers between 1 and 50.

Q23. Write a program to print the multiplication table.

Break and continue statement


► break:- It can be used within looping body to terminate the looping statements. It can be used either
conditionally or unconditionally within looping body. This type of termination of loop is called as premature
termination.
Syn:- for(……………..) Syn:- do
{ {
…………………. ………………….
break; break;
………………… …………………
} }while(condition);
Syn:- while(condition) Syn:- for(…………………)
{ {
…………………. ………………….
if(condition) if(condition)
{ {
break; break;
} }
……………….. ………………..
} }

Ex:- for(a=1;a<=5;a++) Output Ex:- a=1; Output


{ 1 while(a<=5) 1
printf(“\n a=%d”, a); { 2
break; printf(“\n a=%d”,a); 3
} if(a==3)
{
break;
}
a++;
}
Ex:- a=1; Output
while(a<=5) 1
{ 2
printf(“\n a=%d”,a);
a++;
if(a==3)
{
break;
}
}

In case of nesting, the “break” statement works for only that loop under which it has been used.

Ex:- for(a=1;a<=3;a++) Output


{ a=1
printf(‘\n a=%d”,a); b=1
for(b=1;b<=3;b++) a=2
{ b=1
printf(“\n b=%d”,b); a=3
break; b=1
}
}

► continue:- It can be used within looping body only to continue the loop so it is same as the closing brace of loop.
It sends the cursor at the top of the loop for further continuation.
So, all the statements written below the “continue” statement are ignored by the compiler. It can be used
other conditionally or unconditionally within looping body.

Syn:- for(……………..) Syn:- while(condition)


{ {
…………………. ………………….
continue; if(condition)
………………… {
} continue;
}
………………..
}

Ex:- for(a=1;a<=3;a++) Output Ex:- for(a=1;a<=5;a++) Output


{ 1 { a=1
printf(“\n a=%d”, a); 2 printf(“\n a=%d”, a); hy
continue; 3 if(a>=3) a=2
printf(“\n hy”); { hy
} continue; a=3
} a=4
printf(“\n hy”); a=5
} by
printf(“by”);
In case of nesting it works for only that Output Ex:- a=1; Output
loop under which it has been used a=1 while(a<=2) a=1
Ex:- for(a=1;a<=3;a++) b=1 { b=1
{ b=3 printf(“\n a=%d”, a); c=2
printf(“\n a=%d”, a); b=3 b=a; Hy
for(b=1;b<=3;b++) By d0 c=3
{ a=2 { c=4
printf(“\n b=%d”, b); b=1 printf(“\n b=%d”, b); Hy
continue; b=2 b++;
printf(“hy”); b=3 if(b>1)
} By {
printf(“\n By”); a=3 break;
} b=1 }
b=2 }while(b<=4);
b=3 for(c=b;c<=4;c++)
By {
printf(“\n c=%d”, c);
if(c==3)
{
continue;
}
printf(“\n Hy”);
}
break;
}

Q26. Write a program to print the character and its ASCII value.
Q27. Write a control page break program for ascii.
ARRAY
Array is also a variable which can store more than one value of same data type. It means an array is used to store the
homogeneous types of data.
Syn:- datatype variable[size];
Where size represents the number of values to be stored
Ex:- int n[5];
float x[10];
char s[30];

The memory space is always allocated by the array variable name and then the whole space is divided into several
blocks according to the given size. Each block is numbered from zero(0) for its identification, called as index number
or block number or pocket number. Each block can store one value and each block has equal size.
Ex:- int n[5];
n

0 1 3 4 5 Total space for n=5*2=10 bytes.

An array follows the contiguous memory allocation techniques. It means all the blocks must be adjacent to each
other. An array follows the sequential data storing and accessing technique. It means the data can be stored or
retrieved to/from only one block at a time.
Storing: n
n=10 -> Error
n[1]= 10 -> True
0 1 2 3 4

Retrieving:
printf(“%d”, n); -> Error
printf(“%d”, n[1]); -> 10

Q28. Write a program to input 10 numbers in an array and print them.


Q29. Write a program to input 10 numbers in an array and print the sum of those numbers only which are even.
Q30. Write a program to input 10 numbers in an array and print the reverse of each numbers as follows:-
Number Reverse
12 21
152 251
…………………………………………..
Q31. Write a program to input 10 numbers in an array and print only those numbers which are prime.
Types of Array
There are mainly two types of array:-
(i) Single Dimensional Array
(ii) Multi Dimensional Array
(a) Double D.A.
(b) Triple D.A.
(c) Fourth D.A.
………………………
Upto Sixty D.A.
(i) Single Dimensional Array:- When one dimension is specified with an array variable then it is called as single
dimensional array.
Syn:- data type v[size];
Ex:- int x[5]; char s[40];
float n[10];

Double dimensional Array


When two dimensions are specified with an array variable then it is called as double dimensional array.

Syn:- datatype variable[size][size];


Where the first size represents the number of rows and the second size represents the numbers of columns.
The memory space is divided into several rows and several columns according to the given dimensions. Each row and
each column is numbered from zero called as index number/pocket number/block number.
The combination of row and column creates several blocks or cells. Each cell is identified by row number and
column number both. Each cell can store one value and each cell has equal size.

Ex:- int n[2][3];


n
0,0 0,1 0,2
1,0 1,1 1,2
0 1 2 Total space for n=6*2=12 bytes.

*Storing:-
N=10; error
N[1]=10; error
N[1][2]=10; goes on (1,2) block
The double dimensional array is also called as matrix representation.
(35) Write a program to create 3 by 3 (3×3) D.D.A., input numbers and print them.

String Handling
String s a combination of more than one character. ‘C’ does not support a separate data type to store and manipulate
string data. The ‘char’ keyword is used to store the string data in ‘c’. But the simple ‘char’ variable can store one
character only at a time. So we need to create an array of ‘char’ datatype to store the string data.
Syn:- char v[size];
Thus the single dimensional character array is called as the simple string variable. Where size represents the no. of
characters to be stored.
Ex:- char s[20];
Here ‘s’ is a string variable which can store one string value of maximum 20 characters long.
The memory space is allocated by ‘char’ variable name and the whole space is divided into several blocks according
to the given size. Each block can store one character value and each block has equal size.
Ex:- char s[20];
S
0 1 2 3 ……………………………………………………………………………..19
Total space for s=20*1=20 bytes.
When we input string value in string variable then a null character (\0) is automatically added after the last character.
So one block is always reserved for null (\0) character.
Thus we can store total (n-1) characters out of n characters long.
The null character represents to the compiler that there is not any characters after it at the time of reading.
Ex:- char s[10];
S
R A M \0
0 1 2 …………………………………………………………………………………………………………….9

S
C O M P U T E R \0
0 1 2 …………………………………………………………………………………………………………….9

S
U N I V E R S I T \0
0 1 2 …………………………………………………………………………………………………………….9

If we have to store the ‘university’ then last character ‘y’ not be stored.
The control string (%s) is used to input and output the string value.

Ex:- char s[10];

S
R A M \0
0 1 2 …………………………………………………………………………………………………………….9

printf(“%s”, s); // RAM


printf(“%c”, s); // ×
printf(“%c”, s[1]); //A

Q. Write a program to input a person name and print it.


Ans:-

#include<stdio.h>
#include<conio.h>
void main()
{
char pname[20];
clrscr();
printf(“\n Enter person name:-“);
scanf(“%s”, pname);
printf(“\n Person Name=%s”, pname);
getch();

String Array
When we want to store more than one string values in one string variable then we need to create the string Array.;
Syn:- char v[size][size];
Thus the double dimensional character array is called as single dimensional string array. The first size represents the
no. of strings and the second size represents the no. of length of each string.
Ex:- char s[3][10];
c o m p u t e r \0
s y s t e m \0
P A N D E Y \0
0 1…………..………………………………………………………………………… 9

Storing:-
S[0] – computer
S[1] – system
S[2] – PANDEY
Retrieving:-
printf(“%s”, s); //x
printf(“%s”, s[1]); //system
printf(“%c”, s[1]); //x
printf(“%c”, s[1][3]); //t

Q. Write a program to input 10 person name and print them.

String handling function


Since string data can not be manipulated directly by using arithmetical and relational operators. So ‘c’ supports some
predefined library functions to perform the operations on string data. All the string handling functions reside within
“string.h” headerfile . The functions are as follows:-
(1) strcpy():-
It is use to copy one string into another string.
Syn:- strcpy(s1,s2);

Ex:- char s1[15]=”computer”, s2[15];


strcpy(s2,s1); // s2=s1
printf”%s”, s1); //computer
printf(“%s”, s2); //computer

(2) strncpy():-
It is use to copy the specified no. of characters form one string to another string.
Syn:- strncpy(s1,s2,n);

Ex:- s1[15]=”computer”, s2[15];


strncpy(s2,s1,3);
printf(“%s”, s1); //computer
printf(“%s”, s2); //com
(3) strcat():- It is use to concat two string together.
Syn:- strcat(s1,s2);

Ex:- char s1[25]=”computer”, s2[25]=”system”;

strcat(s1,s2);
printf(“%s”, s1); //computersystem
printf(“%s”, s2); //system
(4) strncat():- It is use to concat the specified no. of characters from one string to another string.
Syn:- strncat(s1,s2,n);

Ex:- char s1[25]=”computer”, s2[25]=”system”;

strncat(s1,s2,3);
printf(“%s”, s1); //computersys
printf(“%s”, s2); //system
(5) strlen():- It returns the length of the string.
Syn:- int v=strlen(string);

Ex:- char s1[25]=”computer”;


int n;
n=strlen(s1);
printf(“%d”, n); // 8
(6) strrev():- It is used to reverse the string.
Syn:- strrev(string);

Ex:- char s1[25]=”ATAT”;


strrev(s1);
printf(“%s”, s1); //TATA

(7) strlwr():- it is used to convert the upper case string into lower case string.
Syn:- strlwr(string);

Ex:- char s1[25]=”ATAT”;


strlwr(s1);
printf(“%s”, s1); //atat

(8) strupr():- it is used to convert the lower case string into upper case string.
Syn:- strlwr(string);

Ex:- char s1[25]=”tata”;


strlwr(s1);
printf(“%s”, s1); //TATA

(9) strcmp():- It is used to compare two strings together. It performs the binary comparison on string i.e. performs
the comparison on ASCII value. So the upper case and lower case is different in this case.
Syn:- int v=strcmp(s1,s2);

It returns either 0, 1 or -1 after comparison.


If s1=s2 the returns 0
s1>s2 then returns 1
s1<s2 then returns -1

Ex:- char s1[10]=”RAM” s2[10]=”ram”;

if(strcmp(s1,s2)==0)
{
printf(“\n Equal”);
}
else
{
printf(“\n Not equal);
}

(10) stricmp():- It is also used to compare two string but it performs the text comparision on string. i.e. the upper case
and lower case is same in this function.

Ex:- char s1[10]=”RAM”, s2[10]=”ram”;

if(stricmp(s1,s2)==0)
{
printf(“\n Equal”);
}
else
{
printf(“\n not equal”);
}

Function
It is a subprogram or module which is created to perform a certain task. Every function has a unique name and every
function performs only one task. The concept of function is most important and useful because it implements the
concept of structured programming technique.
As we know that structured programming technique gives the facility to divide a whole program into different
modules, each module is called as a function. Each module performs only one work and each module has a unique
name for its identification.
A function is a dependent program because it performs its task only when it is being called from any where. A
function can be called from any other function.
There are two types of functions:-
(i) Predefine function:- Those functions which are already created by the developers are called as predefine
functions. All those predefine functions are further categorized within the header files and all the header files have
been kept in the “C” library.
Ex:- clrscr(), getch(), strcpy(), strcmp(), sqrt(). Etc
(ii) Userdefine function:- Those functions which are created by the user within program are called as user define
functions. Follow the following syntax to create user define function.
Syn:- returntype functionname([argument, if any])
{
……………………………
…………………………..
[return(value/variable/expression);]
}
 returntype:- A function may or may not return a single value. The return type tells to the compiler that the
function will return a value or not. We specify the name of datatype in this place as we want to take return of value
from function. For example if we want to take return of integer type then we will specify the int datatype, if we want
to return float value then we will specify the float datatype and so on.
If we don’t want to take return of value from function then we specify the “void” in place of returntype. Thus “void”
is also datatype which tells to the compiler that function couldnot return any value.
 function name:- Every function should have a unique name because a function is identified and called by its name
only.
 argument, if any:- A function has nature that it can receive the value and can’t. It tells to the compiler that the
function would accept the values or not. We specify the variables with datatype as arguments. These arguments are
called as formal arguments.
 return() function:- It is a predefine function, which is used to take return of values from function. It can be used
within function body only when the function has nature to return value.
Syn:- return(value/variable/exp.);
Ex:- return(10), return(c), return(a+b).

Calling and called function:-


A function which calls another function within its body is called as calling function. A function which is being called by
another function is known as called function.

Ex:- void main() // calling


{
…………………..
f1(); // called
……………………….
}
Ex:- void f1() // calling
{
…………………..
f2(); // called
……………………….
}
void f2()
{
………………..
}
Types of function:-
There are four possible ways of creating a function:-
(i) No returntype and no formal argument
(ii) No ruturntype but formal argument
(iii) returntype but no formal argument
(iv) returntype with formal argument

(i) No returntype and no formal argument:- In this case the input process and output are performed by the UDF.
Ex:- void f1()
{
I calling
O
P
}
void main()
{
f1();
}

(ii) No returntype but formal argument:- In this case the process and output operation is perfomed by the UDF. But
the input operation is performed by the calling function.

Ex:- void f1(int x)


{
calling
O
P
}
void main()
{
Input
f1(value);
}

(iii) returntype but no formal argument:- In this case the input and process operation is performed by the user
define function and the output operation is performed by its calling function.

Ex:- void f1()


{
calling
I
P
}
void main()
{
int x;
x=f1();
Output
}

(iv) returntype and formal argument:- In this case the process operation is performed by UDF and the input/output
operation is performed by its calling function.

Ex:- void f1(int x)


{
calling
Process

}
void main()
{
int x,y;
Input
y=f1(x);
Output
}

Types of calling a function:-


There are two ways of calling a function:-
(i) call by value (ii) call by reference

(i) Call by value:- In this case the duplicate values of actual arguments are transferred within the formal arguments.
When simply the variable names or constant values are specified as actual arguments then it is said as the function
has been called through call by value method.

Ex:- void f1(int x, int y)


{
…………………………
}
void main()
{
Int m=10, n=20;
f1(m,n);
or call by value method
f1(30,40);
}

Since in this case the xerox values of actual arguments are transferred within formal arguments. So, there is no any
relation between actual and formal arguments.
Thus we do any operation on formal arguments within function then it will not effect the values of actual arguments.

Q. WAP to demonstrate the call by value method.


#include<stdio.h>
#include<conio.h>
void change(int x);
void main()
{
int n=10;
clrscr();
printf(“\n Value of n before calling=%d”, n);
change(n);
printf(“\n value of n after calling=%d”, n);
getch();
}
Void change(int x)
{
x=x+5;
printf(“\n x=%d”, x);
}

(i) Call by reference:- When the address of the actual arguments are transferred within the formal arguments then it
is called as “call by reference method”.
In this case the formal arguments must be declared as pointer type variable having same datatype.
Ex:-
void f1(int *p)
{
…………………….
}
void main()
{
int n=10;
f1(&n); // call by reference
}

In this case the address of actual arguments are transferred within the formal arguments. So a linking is established
between formal and actual arguments. Thus if we do any operation on formal arguments then it will directly affect
the value of actual arguments also.

Q. WAP to demonstrate the call by reference method.


# include<stdio.h>
#include<conio.h>
void change(int *x);
void main()
{
int n=10;
clrscr();
printf(“\n value of n before calling =%d”, n);
change(&n);
printf(“\n value of n after calling=%d”, n);
getch();
}
Void change(int *x)
{
*x=*x+5;
printf(“\n x=%d”, *x);
}
POINTER
It is also a variable which can store the address of another simple variable having same datatype.
It is the unique feature of “C” through which we can work on the address directly. The advantage of using
pointer is as follows:-
(i) It increases the processing speed of the program. Because it works on the address directly.
(ii) With the help of pointer we can do the hardware programming also.
(iii) It supports the run time memory allocation feature through which the memory can be allocated and dellocated at
run time. This features is called as DMA. So it saves the memory space.
(iv) It is more efficient to use for the character or string data.
Thus due to the above features we can say that pointer is more powerful feature of “C”. There are two
symbols used in case of pointer:-
(i) *(asterisk):- used to declare the pointer variable.
(ii) &(Ampersand):- It is used to retrieve the address of any variable. So it is also called as an address pointer.
Syn:- datatype *variable;
Ex:- int *p; //integer pointer
float *f; //float pointer
char *s; //character pointer
The integer pointer can store the address of simple integer variable only, float pointer can store the address of simple
float variable and so on.
-> Linking between pointer and simple variable:- Once the pointer variable is created, it must be linked with simple
variable at first. For this, we have to simply assign the address of simple variable into the pointer as follows:-
Syn:- pointervariable=&variable;
Ex:- int *p, a=10;
p=&a; //linking
Once the address of simple variable is assigned into the pointer then we can access the address as well as variable
both of simple variable with the help of pointer.
In case of pointer, the variable “p” is called as pointer variable and it refers to the address of simple variable
and the variable “*p” is called as indirectional pointer variable and it refers t o the value of simple variable.
Ex:- p 1000 a 10

1004 1000
p - 1000 , &a- 1000 , a- 10 , *P– 10, &p- 1004
Operational performed on pointer :- Following operations can be performed on pointer variable:-
(i) Two pointer variables can be subtracted together.
Ex:- int *p1, *p2;
p1-p2;
p2-p1
It returns the numbers of elements reside within these two address.
(ii) Constant values can be added or subtracted on pointer variable.
Ex:- p1=p1+5;
p1=p1-2;
Following operations can not be performed on pointer variable:-
(i) Two pointer variables can’t be added, multiplied and divided together.
Ex:- p1+p2; ×
p1*p2; ×
p1/p2; ×
(ii) Constant values can’t be multiplied and divided with pointer variable.
Ex:- p1=p1*5; ×
p1=p/10; ×
Following operations can be performed on indirectional pointer variable:-
(i) Two indirectional pointer variables can be added, sub, multiplied & divided together.
Ex:- *p1 + *p2; *p1 - *p2; *p1 * *p2; *p1 / *p2;
(ii) Constant values can also be added, subt, multi, divided with indirectional pointer.
Ex:- *p1 + 5; *p1 - 2; *p1 * 8; *p1 / 10;
STRUCTURE & UNION
Structure:- It is a user defined datatype which provides the facility to declare more than one variables of different
datatypes together. In another word we can also say that structure is used to store the heterogeneous types of data.
Syn:- struct structurename
{
datatype variables;
datatype variables;
…………………………
}
All the variables declared within structures are called as elements of structure.

Ex:- struct A
{
int x;
float y,z;
char m;
};
The elements of structure are loaded in the memory only when the structure type variable is created. So, we must
have to create the structure type variable or structural variable. There are two ways of creating structural variable:-
(i) At the time of structure creation
Ex:- struct A
{
……………….
………………..
}V1,V2,V3; //structural variable
(ii) Within any function
Syn:- structurename variablename;

Ex:- A S1,S2,S3; //structural variables.


The memory space is always allocated by the structural variable name and then all the elements of structure are
copied into the structural variable memory area. We can create any no. of structural variable and all the structural
variable share the same properties of structure. It means all the elements of structure are copied into the each
structural variable area separately.
Thus the memory space is not allocated by the structure name. Structure name is simply used as a datatype
within program and since this name is given by the user that’s why structure is called as user defined datatype.
Ex:- struct A
{
int x;
float y;
char z;
}S1,S2;
Once the elements of structure are copied into the structural variable area then we can access the elements of
structure within program as follows:-
Structuralvariable.element=value;
Where “.” Is called as member differenting operator.
Ex:- S1.x=10;
S2.x=20;
S1.y=4.5;
S2.y=8.6;

Union:- Union is also a user define datatype which provides a unit or block in which different variables of different
datatype can be kept together. It means union is also used to store the heterogeneous types of data.
Syn:- union unionname
{
datatype variables;
datatype variables;
…………………………
}

Ex:- union A
{
int x;
float y,z;
char m;
};
Linke structure the elements of union also reside within union type variable area. So the memory space is always
allocated by the union type variable and then all the elements of union are loaded within union type variable area.
Like structure there are two ways of creating union type variable.
(i) At the time of structure creation
Ex:- union A
{
……………….
………………..
}U1,U2,U3; //union type variable
(ii) Within any function
Syn:- unionname variablename;

Ex:- A U1,U2,U3; //union type variables.


Once the elements of union are loaded within union type variable area, then the element of union can be accessed as
follows:-
Syn:- unionvariale.elementname;

Ex:- U1.x=10;
U1.y=4.5;
Thus in all respect the union is same as structure but the basic difference between structure and union is that the
elements of structure takes separate space within structural variable area but the space for union variable is allotted
according to the highest size element and then all the elements of union share the same space. So only one elements
of “union” store its current value in memory. So, the elements of “union” occupy less space than structure.
The elements of structure store separate values at a time in memory. Because the elements of structure
occupy separate space within structural variable area. So, the size of structural variable is equal to the sum of size of
all elements.
Hence we can say that all the elements of structure can be accessed at a time within program but only one
union element can be accessed at a time.
Thus it is clear that union is beneficial to use in that situation we need to access one member oly at a time
due to which the memory space can be saved.

You might also like