You are on page 1of 35

PROGRAM:

Program is a set of instructions are written in a programming language to perform a specific task
or to solve a particular problem. ‘C’ is a middle level language .C language was developed by
Denish Riche in 1978

EX.
/* to display your name */
#include<stdio.h>
#include<conio.h>
void main
{
clrscr();
printf(“ MY NAME IS DIPTI RANJAN SAHU “);
getch();
}

Press alt f 9 to compile and check error of your program , when error is not present in your
program then press ctrl f9 to run your program.

EX :
/* Addition of two number */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
printf(“ enter two number ”);
scanf( “ %d%d” , &a,&b);
sum=a+b;
printf( “ addition of two number is %d “, sum);
getch();
}

A “C” program what ever its size, consists of function and variables. A function contains
statements that specify the computing operations to be done, and variables are store values used
during the computation.
#include<stdio.h>
It tells the compiler to include information about the standard input/output library function. This
line appears at the beginning of many “c” source files. It use any input/output function.
#(heash) : ( preprocessor directory )
It is known as precompiler are nothing but the execution of program or process before
complication the time is taken.
Preprocessor :
The process which is excuted before the comparision is known as preprocessor.
Include :
It is a library function which includes the header file (.h) to our source program.
Header file:
A header file is contain all the similar type of library function and its extension is .h , different type
of header files are :
<stdio.h>
<conio.h>
<ctype.h>
<math.h>
<srtring.h>

<> : A header file is declared in to <> symbol. File or folder which is include greater then and
smaller than symbol and search the current directory.
“ “:
It searches a current drive.
#include:
The #include is known as preprocess directive. The work of preprocess directive put include the
header file to the source program.
<stdio.h>:
Standard input/output function
.<conio.>
It is used for taste graphics.
<math.h>:
It is used for the mathematical function.
Printf(“ ” ):
Printf( ) function is only executable statement of the program. Printf ( ) is a predefined standard
“C “function for printing output.
Scanf(“ “ ):
Scanf( ) is input library function which avilble in the <stdio.h> header file.
Predefined( ):
 Predefined means that it is a function that has already been written and compiled and
linked together without program at the time linking.
 The printf( ) function causes everything between there starting and ending question
mark to printout .
 Printf lines always ends with a semicolon mark( ; )

Clrscr( ) :
It is used to the clear screen.
Getch( ):
It is a input function. Getch fuction don’t ____________ the character on the screen, press alt f5.

main( ):
 main is a function. Every program must have a main function some where. Program begins
executing all the beginning of main.
 The main ( ) is a special function used by the system to tell the compiler where the program
start.
 Every program have exactly one main function.
 If we use more than one main function the compiler can’t tell which one marks the beginning of
the program.
main( ):
The empty pair of the parenthesis immediately following main indicate that the function main has
no argument or parameter or no return information to the operating system
.main( void):
It means no any returning.
int main( void):
When int is specified the last statement in the program must b return.

{ or opening brace:
It marks the beginning of the function.

} or closing brace :
It marks the end of the function and it is also indicate the end of the program or function part.

Syntax of { }
{
--------------
---------------
----------------
-----------------
}
All the statement between two braces is called as function body. Braces { & } are used to group
declarations and statement together into a compound statement or a block.

Comment line / *-------------------------------------------------*/


//---------------------------------------------------- :
 It is known as comment line. These are used in a program to enhance its readuable
and understanding.
 Comment line are not excutable statements and therefore anything between /*…………
*/ is grouped by the compiler.
 It is used whenever blank space can access at the beginning middle or line, but never
used the middle of a word.
 We can’t used a comments inside the comment.
 // It is also comment line to use any line restrict.

“C “ language use only lowercase letter, and also use uppercase in output string like” printf(“MY
NAME IS Chandra sekhar mohanty“);

COMPILER :
It is a translator to translate to high-level language to machine level language. The compiler read
all the lines of the program & short out the error. If exit & display. If there is no error then it
converts the high level language to machine language.

Interpreter :’
The interpreter read line by line & checks error. If the error occure in any line then it stop reading
process on that line & display the order.
DATA TYPES :

ANSI C supports three type of data type


1. Primary data type( integer(int), charcter(char), flaot, longinteger(long int), double, void
etc.
2. Derived data type( array, function, structure, pointer)
3. User defined datatype.

All “ C” compiler support five fundamental data types, namely


1. integer (int)
2. character (char)
3. single floting point( float)
4. double precision floating point (double)
5. void

Int:
An integer, typically reflecting the natural size of integer an host machine(ex; :1,2,-123,456)
.intger contain 16 bit , it represent or it declare in symbol %d and its range -32768 to 32767.
long int is represent %ld contain 32 bit data. We use long and unsigned integer to increase the
range of values.

Char:
A single byte, capable of holding one character in the local character set.
Ex: any single letter a—z) char contain 8 bit , it represent or it declare in symbol %c and its
range -128 to 127

Float:
Single precision floating point( ex : 43.67,5688.8) float contain 32 bit , it represent or it declare in
symbol %l and its range -3.4e-38 to 3.4e+e38

Double:
Double precision floating point( ex : 43.67,512233.898) double contain 64 bit , it represent or it
declare in symbol %lf and its range -1.7e-308 to 1.7e+308.

void
void has no value.
VARIABLE :
A variable is a data name that may be used to store a data value. A variable may take different
value at different times during execution. A variable name can be choose by the programmer in a
meaningful way, so as to reflect its function or nature in the program.

Ex : average
Height
Total
Variable names may consists of letters, digits and the underscore ( _ ) character, subject to the
following condition.
1) They must begin with a letter. Some systems permit underscore as the first character.
2) Variable length is between 1—31 character, length should not be normally more than
eight characters, since only the first eight characters are treated as significant by many
compiler.
3) Uppercase and lowercase are significant. That is the variable “ Total” is not same as
“total” or “TOTAL”
4) White space is not allowed.
5) It should not be a keyword (auto, char, int, float)
Valid variables are john, delhi, mark, t_rase
Invalid variables are 123, (area), 25th , %d

If only the 1st 8 characters are recognized by the compiler, then the two names average_ height
average_weight
avg_height and avg_weight

CONSTANT :
Constant means fixed values that don’t change during the execution of a program; “C” support
several types of constant.
• Integer constant : there are three types of integer constant
i. Decimal integer. Ex 123,-321,0
ii. Octal integer . Ex 037
iii. Hexadecimal integer. Ex A353

• Single charcter constant : A single character constant contains a single enclosed within a
pair of single quote marks, Ex—‘x’ , ‘.’, ‘s’. A character constant ‘5’ is not same as the no.
5.
• String Constant : A string constant is a sequence of characters enclosed in double
quotes. The characters may be letters, numbers, special character and blank space.Ex
—“hello”, “x”. Hence “x”=! ‘x
• Backslash character constants
Constant Meaning
‘\a’ addable alert
‘\b’ back space
‘\f’ form feed
‘\n’ new line
‘\r’ carriage return
‘\t’ horizontal tab
‘\v’ vertical tab
‘\”’ doubles quote
‘\?’ Question mark
‘\0’ null

Declaration of variables
Data type v1, v2, v3;

Ex—int a,b,sum;
A variable may also initialized in the declaration

Ex—int i=0, float b=8;


Or
int i=0;
float b=8;

OPERATOR:

An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators are used in programs to manipulate data and variables. They usually
form a part of the mathematical or logical expression.

TYPES OF OPERATOR: -

1) Arithmetic operator ( +, -,*,%,/ )


2) Relational operator ( <, <=, >, >=, = = )
We use relational operator, when compare the age of two person or price of two items.The value
of a relational expression is either one (true expression), zero (false exp)
***
EXPRESSION: -
An expression is a sequence of operand and operators that reduces to a single value.

Example: - 25+10=35 Here 35 is the expression


3. LOGICAL OPERATOR: -

&&logical AND
||  Logical OR
!  logical NOT
The logical operator && and || are used when we want to test more than one condition and make
decision..
1. a>b && x = =10
2. if(age> 55 && salary< 1000)
3. if (number < 0 || number >100)

4 ) ASSIGNMENT OPERATOR : -
Assignment operators are used to assign the result of an expression to a variable.
Example: -
a+b = c (let) Here = symbol is assignment operator or shorthand assignment operator.
Ex x + = y+1;
It means x =x+(y+1);
Here “ + = “the assignment operator

a=a+1 a+=1
a=a-1 a-=1
a= a*(n+1) a*=(n+1)
a=a/(n+1) a/=n+1
a=a%b a%=b

5) INCREMENT & DECREMENT OPERATOR :( ++ ,, -- )


The operator ++ is an increment operator, it adds 1 to the operand while -- is decrement operator,
it subtract 1. both are in another format
Example: -
++m or m++
--n or n—
++m or m=m+1 or m+=1

We use the increment operator and decrement operator in for and while loop extensively.

Example: -
m = 5;
y = ++6;
In this case y & m value would be 6.
Example: -
m=5
y=m++;
In this case y would be 5 and m is 6.

A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left.

A postfix operator first assigns the vale to the variable on the left and then increment or
decrement the operand

6)CONDITIONAL OPERATOR: -

A ternary operator pair “?” is available in ‘C’ to construct conditional expression of the form.
exp1? exp2: exp3
Where exp1, exp2, exp3 are expression.

Exp1 mean is evaluated first; if it is nonzero (true) then the expression exp2 is
evaluated and becomes the value of the expression.

If exp1 is false, exp3 is evaluated and its value becomes the value of the
expression. Only one of the expression is evaluated.

Example: - a = 10;
b= 15;
x= (a>b)? a: b;

OR, in if else statement if (a>b)


x=a;
else
x=b;

7) BITWISE OPERATOR: -
‘C’ provides some operator for bit manipulation of data at bit level. These operators are used for
testing the bits, or swiping them right or left. These operators are not applied in float & double.
& Bitwise AND
|| Bitwise OR
^ Bitwise EXCLUSIVE OR
<< Shift Left
>> Shift Right
- One’s Complement

8) COMMA OPERATOR: -
The comma operator can be used to link the related expression together. A comma linked list of
expressions is evaluated left to right and the value right most expression is the value of the
combined expression.

Example: -
Value = (x=10, y=5, x+y);
Here, 1st assign x=10
2nd assign y=5
3rd assign 10+5=15

EXPRESSION: -
An expression is a sequence of operand & operators that reduces to a single value.

Example: - 10+5 = 25
is an expression whose value is 25.

1. ARITHMATIC EXPRESSION: -
An arithmetic expression is a combination of variables, constants, operators arranged as per the
syntax of language.
Algebra Expression ‘C’ Expression

A*B-C A*B -C
(M+N)(X+Y) (M+N)*(X+Y)
(AB/C) A*B/C

1.) When a=9, b=12, c=3


X=a-b/3+c*2-1
=9-b/3+3*2-1
=9-12/3+3*2-1
=9-4+6-1
=5+5
=10

Simple program(Q):
1. Display your name.
2. Addition, Substraction, multiplication, division of two number.
3. WAP to addition five floating number and calculate its average.
4. WAP to enter four number and display addition, multiplication, and division of four numbers at
time.
5. WAP to addition 300 number.
6. WAP to convert Celsius to Fahrenheit and vice versa.
7. WAP to calculation simple interest.
8. WAP to find area of circle.
9. WAP to find perimeter of the circle.
10. WAP to find area of triangle.
11. WAP to find area of triangle.
12. WAP to find absolute of a number.
13. WAP to change the sign of a number.
14. WAP to find sqrt of a number .
15. WAP to solve this problem
16. WAP to solve this problem
17. WAP to find the sign, cos, tan, sec, cosec, cot value of a number .
18. WAP to find power of a number.
19. WAP to convert no. of days in to month in days.
20. WAP of multiplication table as number five.
CONTROL FLOW / THE DECISION CONTROL STRUCTURE/
DECISION MAKING & BRANCHING:

‘C’ language processes such decision making capabilities by supporting the following statement
or decision
.
1. IF STATEMENT: -
The if statement is a powerful decision making statement and is used to control the flow of
execution of statement. It is basically a two way decision statement and is used in conjunction
with an expression.
The keyword if tells the compiler that what follow, is a decision control instruction.
The keyword if is always enclosed within a pair of parentheses”( )”.
If the condition is true then the statement is executed. If the condition is false then
the statement is not executed.

Syntax: - if( condition)


printf(“ statement”);

Example: - / * check enter no. is present in between 10 */


#include<stdio.h>
#include<conio.h>
void main( )
{
int num;
clrscr( );
printf(“enter a number less than 10 :”);
scanf(“%d”,&num);
if(num<=10)
printf(“this number is in between 10”);
getch( );
}

DEMERITS OF IF STATEMENT: -
The statement block may be a single statement or a group of statement if the test expression is
true, the statement block will be executed; otherwise the program is not executed.

2. IF-ELSE STATEMENT: -
In if statement by if else will executes a single statement, or a group of statement – when the
condition is true. It doesn’t nothing when the condition is false.

Can we execute one group of statement if the condition is true or false?


Yes, this is the purpose of the else statement.

The if else statement is used to express decision or it is extension of the simple statement.

Syntax: -
if(test expression) True block statement
{ }
else }
{ Statement x
False block statement

Formally the syntax, where the else part is optional. The expression is evaluated, if it is true
statement 1 is executed. If it is false and if there is an else part, statement 2 is executed instead.
Example: - / * check enter no. is present in between 10 or not*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int num;
clrscr( );
printf(“enter a number less than 10 :”);
scanf(“%d”,&num);
{
if(num<=10)
printf(“this number is in between 10”);
else
printf(“ this number is not present in side 10”)
}
getch( );
}

Example2: /* check the no. is divisible by 5 or not */


#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
clrscr ( );
printf(“enter a number :”);
scanf(“%d”,&a);
if(a%5= = 0)
printf(“this number is divisible by 5”);
else
printf(“this number is not divisible by 5”);
getch( );
}

3. NESTED IF-ELSE STATEMENT: -


When a series of decisions are involved, we may have to use more than one if-else statement in
nested form.

Syntax: -
if(test condition1)
{
Printf(“ 1st true statement1 “);
Else if ( condition 2)
Printf( “ 2nd true sataement2”);
Else if ( condition 3)
Printf( “ 3rd true sataement3”);
Else
Printf(“ false statement”);
}
If the condition 1 or 2 or 3(any one of these) is true this condition statement is executed;
otherwise execute to false statement.
Example 3 : / * largest in between three number * /
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr ( );
printf(“enter thtree number :”);
scanf(“%d%d%d”,&a,&b,&c);
if(a > b && b > c)
printf(“ %d is the largest number\n “ ,a );
else if( b>c && c >a)
printf(“ %d is the largest number\n “ ,b );
else if (c > a && c > b)
printf(“ %d is the largest number\n “ ,c );
else
printf(“ all are equal );
getch( );
}
QUESTIONS:
1. Check the year is leap or not .
2. Check the number is odd or
even.
3. Find the largest and smallest of two given number, & also show both are equal.
4. Check the letter is vowel or
consonant.
5. The mark obtained by a student in 5 different subjects is input through the key board. If
percentage > = 60, 1st division, per >=50 , 2nd div, per >=40 , 3rd div other wise fail.
6. An electric power distribution company charges in domestic consumers as
Unit Rate of charge
0--- 200 Rs 0 --- 50 paise per unit
201—400 Rs 1 per unit
401--- 600 Rs 1.75 per unit
600—onwards Rs 2.50 per unit

IF – ELSE:
1. WAP to check enter no is present or not inside 10.
2. WAP to check given year is leap year or not.
3. WAP to check given number is divide by 5 or not.
4. WAP to check the number is odd or even.
5. WAP to find smallwest and largest of three number.
6. WAP to check the letter is vowel or not.
7. WAP to calculate , in a company an employee is paid as : if basic salary < rs 7600, hra10%,
da= 90%,salary >= 7600, hra 15% da=98%.
8. WAP to display result, the mark obtained by a student in 5 different subjects are input through
the keyboard. Percent > =60,1st class, per >=50, 2nd class, per> =40, 3rd class, other wise fail.
/ **************end **************/

SWITCH STATEMENT:
The switch statement is a multi way decision that tests whether an expression matches one of no.
of construct integer values & branches according.

The statement test the vales of a given variable or expression against a list of case values and
when a match is found, block statements associated with that case is executed.

Syntax
switch ( expression)
{
case constant 1:
statement;
break;
case constant 2:
statement;
break;
------------------------
--- - - -- - - -- - -- -
-- - --- --- - -- - -- -- --
default :
Statement;
}

The break statement causes an immediate exit from the switch.

Ex: (without use break)


#include<stdio.h>
#include<conio.h>
void main( )
{
int i=2;
clrscr ( );
switch( i)
{
case 1:
printf( “ i am in case 1\n”);
case 2:
printf(“ i am in case 2 \n”);
case 3:
printf(“ i am in case 3 \n”);
default:
printf(“ i am in default case \n”);
}
getch( );
}
Output
I am in case 2
I am in case 3

Ex: (use break)

#include<stdio.h>
#include<conio.h>
void main( )
{
int i=2;
clrscr ( );
switch( i)
{
case 1:
printf( “ i am in case 1\n”);
break;
case 2:
printf(“ i am in case 2 \n”);
break;
case 3:
printf(“ i am in case 3 \n”);
break;
default:
printf(“ i am in default case \n”);
}
getch( );
}
Output
I am in case 2

QUESTIONS
SWITCH CASE :
1. WAP to check given letter is vowel or not.
2. WAP to display day of week.
3. WAP to display 0 --- 10.
4. WAP to month of an year.
5. WAP to find largest and smallest.
6. WAP to find odd & even number.
7. WAP to display calculator operation.

/ **************end **************/
LOOP
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This
involves repeating some specified no. of times or until a particular condition is being satisfied.
This repetitive operation is done through a loop control structure.
In looping, sequences of statements are executed until some conditions for termination of the
loop are satisfied.

A program loop consists of two segments


• Body of the loop
• Control statement
The control statement tests the conditions in program & then direct the repeated execution of the
statements contained in the body of the loop.

Steps of looping process


 Setting and initialization of a condition variable.
 Execution of the statements in the loop.
 Test for a specified value of the condition variable for the execution of the loop.
 Incrementing or updating the condition variable.

“C” language provides three constructs for performing loop operations, they are
 While loop
 Do loop
 For loop

While loop:
It is often the case in programming that you want to do something a fixed number of times.
Example:
1. you have to calculate gross salary of different person
2. you want to convert temperature from centigrade to Fahrenheit for different cities.

Syntax:
While ( test condition)
{
Body of the loop
}

The test condition is evaluated and if the condition is true, then the body of the loop is executed.
After execution of the body the test condition is once again evaluated & if it is true, the body is
executed once again. This process is repeated execution of the body continues until the test
condition finally becomes false & the control is transferred out of the loop.
The body of the loop may have one or more statements. The braces are needed only of the body
contains two or more statement.

Example 1 : /* print 1 to 10 */

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1; //initialization//
clrscr();
while(i<=10) // test condition //
{
printf("%d\n",i); // display output
i=i+1; // increment
}
getch();
}

Example 2 : /* print 10 to 1 */

#include<stdio.h>
#include<conio.h>
void main()
{
int i=10; //initialization//
clrscr();
while(i>=1) // test condition //
{
printf("%d\n",i); // display output
i=i-1; // increment
}
getch();
}

LOOP: ( WHILE LOOP)( q)


1. WAP to print 1-10
2. WAP to display 1-10 & sum it.
3. WAP to sum any 10 number
4. WAP to sum of digit of any number.
5. WAP to find factorial of any number.
6. WAP to find reverse of a number.
7. WAP to find reverse of string.
8. WAP to find perfect number.
9. WAP to find palindrome number.
10. WAP to find sum and avg of even number in a range.
11. WAP to calculate simple interest.
12. WAP to find Armstrong number.
13. WAP to find fibonic series
14. WAP to display number is prime or not.

DO—LOOP OR DO—WHILE LOOP:


In case of while loop test the condition before executed at all of the condition is not satisfied at the
very first attempt. On some occasion it might be necessary to execute the body of the loop before
the test is performed. In this situation we can take the help of the do loop.

In case do-while tests the condition after having executed the statements within the loop.
This means that do while would execute its statement at least once, even if the condition fails for
the first time itself.
Syntax:
Do
{
Body of the loop
}
While ( test condition);
In do loops the program proceeds to evaluate the body of the loop first. At the end of the loop, the
test condition in the while statement is evaluated.

 If the condition is true, the program continues to evaluate the body of loop once again. This
process as long as the condition is true
 when the condition is false the loop will be terminated and the control goes to the loops that
appears immediately after the while statement of the do-while loops.

Ex:
i=1; //initialization
sum=0;
do
{
sum=sum+i;
i=i+2; //incrementing
}
while(sum <=40 || i< 10);
printf(“ %d%d\n”, i, sum); //testing
}

Example 3: print the number from 1 to 10


#include<stdio.h>
#include<conio.h>
void main( )
{
int n=1;
do
{
printf(“ %d\n”, n);
n=n+1;
}
while( n < 11);
getch( );
}
DO--- WHILE LOOP (Q):
1. WAP to print the number from 1 to 10.
2. WAP to print sum of digit of any number.
3. WAP to find factorial of a number.
4. WAP to create multiplication table.

For loop
For loop has three expression, semicolon is necessary for separating these expression.
1. Setting a loop counter to an initial value.
2. Testing the loop counter to determine whether its value has reached the no. of repetitions
desired.
3. Incrementing the value of loop counter each time the program segment within the loop
has been executed.

Syntax:
For (initialization; test condition; increment or decrement)
{
Body of the loop
}

In for loop
1. Initialization of the control variable is done first.
2. The value of the control variable is tested using the test condition. The test condition is
relational expression such as i<10
3. When the body of the loop is executed, the control is transferred back to the for loop after
evaluating the last loop. Now the control variable is increment or decrement using
assignment operator.

/* print 1 to 10 */
#incude< stdio.h>
#incude< conio.h>
void main( )
{
int i;
clrscr( );
for ( i=1; i<=11 or i<=10 ; i ++) // use for loop
printf( “ %d\n”, i);
getch( );
}
Nesting for loop
Nesting for loop means one for statement within another for statement is allowed in ‘c’
Syntax :
For (i=1; i<10 ; i++)
---------- - -- --- - ------
------ -- - --- -- -- - -- --
For(j=1;j!=5;j++)// inner loop
{
---------- - -- - - -- -
--- - - - -- - - -- - - -
}
Example display the following output
1
12
123
1234
12345
#incude< stdio.h>
#incude< conio.h>
void main( )
{
int i,j;
clrscr( );
for( i=0; i<=5 ; i++)
{
for( j=1; j<=i; j++)
{
printf( “%d\t”, j);
}
printf( “ \n”);
}
getch( );
}

FOR LOOP (Q)


1. WAP to print the number from 1—10 .
2. WAP to enter any number & it display the sum of no & add each no.( 5= 1+2+3+4+5)
3. WAP to enter any number display odd & even.
4. WAP to find how many numbers are present in range 1-100. Display sum of each number.
5. WAP to display prime or not.
6. WAP to add even number from 0 ---100
7. WAP to find factorial number.
8. WAP to find factorial number & display as like 3!= 3*2*1=6
9. WAP to display fibonic series.
10. WAP to display four different playing cards.
11. WAP to calculation simple interest for three (3) set.
12. WAP to find perfect number.
13. WAP to display these out put
A)5
5 4
5 4 3
5 4 3 2
5 4 3 21

B) 1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
C) 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
D) *
* *
* * *
* * * *
* * * * *

Break statement:
When the keyword back is encountered inside any ‘c’ loop control automatically passes to the first
statement after the loop.
When a break statement is encountered inside a loop, the loop is immediately exited.
 Break statement is used in while loop, do-while loop, for loop & switch case.

Continue statement
In some cases we want to take the control to the beginning of loop by passing the statements
inside of the loop which have not yet been executed. In this case we use continue statement.
When the keywords continue is encountered inside any ‘c’ loop, control automatically passes to
the beginning of the loop.
 A continues is usually associated with an if.
 The continue statement is used for doing next iteration of the loop. The loop dose not
terminate when this statement occur, but it skips the statements, which are after this
statement in the block.

Example to display 1 to9 but not display 3


#incude< stdio.h> {
#incude< conio.h> n=n+1;
void main( ) continue;
{ }
int n=1; printf( “ no. is %d”, n);
clrscr( ); n=n+1;
while (n< 10) }
{ getch( );
if(n==3) }

Example I known continue statement use

#incude< stdio.h> printf( “ here use continue statement”);


#incude< conio.h> n=n+1;
void main( ) continue;
{ }
int n=1; printf( “ no. is %d”, n);
clrscr( ); n=n+1;
while (n< 10) }
{ getch( );
if(n==3) }
{

FUNCTION
A function is a self contained block of code or statements that perform a particular task of some
kind. Function break large computing tasks into smaller computing task.
 Any ‘C’ programs contain at least one function ie main ( ).
 In a program if there are more than one functions are present then one of these
functions must be main ( ) because program execution always begins with main ( ).
 There is no limit on the number of functions that might be present in a ‘c’ program.
 Each function in a program is called in the sequence specified by the function calls in
main ( ).

 After each function has done its thing control returns to main ( ). When main ( ) runs
out of function calls, the program end.

Every program mainly contain three program


• Main ( )  library function

• Printf ( ) & scanf ( ) - user defined function

Q  why we use user defined function.?


The program may become too large and complex and as a result the task of debugging testing
and maintaining becomes difficult.

Why we use function ?


 There are times when certain type of operations or calculations is repeated at many
point through out a program. Exam—factorial no.
 When we are writing functions avoid rewriting the same code over and over.
 Using functions it becomes earlier to write programs and keep track of what they are
doing.
 Any function can call any other function. In fact it can call itself such a process is
called recursion.

Features:
 A called function can also call another function. A function can be called
more than once, but a function can’t be defined in another function.
 temA called function can be placed either before or after the calling function.
 A function gets called when the function name is followed by a semicolon.

main( )
{
argument ( );
}
 A function is defined when function name is followed by a pair of brasses in which one or
more statements are be present.
Argentina ( )
{
Statement 1;
Staent 2;
Statement 3;
}
 Any function can called from any other function. Even main ( ) can be called from other
function.
main ( )
{
message 1 ( );
}
message 2( );
{
printf(“ \n can’t image life without ‘c’ “);
main ( );
}

Element of user defined function , there are three element


1. function definition
2. function call
3. function declaration

1 ) Function definition :
Function definition is an independent program module that is specify written to implement he
requirement of the function.
 A function definition also known as function implementation element it contain
i. function name
ii. function type
iii. list of parameters
iv. local variable declaration
v. function statements
vi. a return statement.

Function Header:
The function header consists of three parts
i. function name.
ii. function type
iii. list of parameter.

Function Body :
The function body consist also three parts
i. local variable declaration
ii. function statements
iii. a return statement

A general format of function definition


function_type function_name ( parameter list) // function header
{
local variable declaration;
executable statement 1;
executable statement 2;
- - - - - -- - -- ----------
- - - - ----------- --- - --
return statement;
}

In between { & } braces constitute the function body contain local variable
Function statement;
Return statement ;
 A semicolon is not used at the end of the function header.
 Function type specifies the type of value like float or that is the function is excepted to
double return to the program calling the function.
 If the return type is not explicity specified c will assume that it as intger type.
 If the function is not returning anything that we need to specify type as void.
 Void is a fundamental data type in ‘C’.

FUNCTION NAME:
 The function name should be appropriate to the task performed by the function.
 Parameter list declares the variables that will receive the data sent by the calling
program.
 The parameter list contains declaration of variables separated by commas and
surrounded by parentheses.
 Exam--- int sum( int a, int b ) //int a,b (wrong declaration)
{
- -- - - - -- - -- - -- - - --
- -- - -- -- - - - - - - - -- - - - ---
- }
There is no semicolon after the closing parentsis.

RETURN VALUES:

A function may or may not send back any value to the calling function. If YES it is done through
the return statement.
Syntax:
return ( ); // does n’t return any value
or
return ( expression ); // here return with an expression return the value of the
expression.

Example:
int mul ( int x,int y)
{
int p;
p=x*y; // here the value of p which is product of the values of x & y.
return p;
}

- It is also written one line return ( x*y);

A function may have more than one statement

if ( x <= 0)
return (0);
else
return ( 1);

FUNCTION CALL:
A function can be called by simply using the function name by list of argument.
Example ---

main ( )
{
int y;
y=mul ( 5,10) // function call
printf(“ %d\n” ,y);
}

Example—

main ( )
{
int y;
y= mul( 10,5) // function call
}
int mul( int x, int y)
{
int p; //* local variable
p=x *y; // x=10,y=5;
return ( p);

The function call sends two integer values 10 & 5 to the function. Int mul (int x,int y ) assigned to x
& y respectable, the function computes the product x and y assigned the result to the local
variable p, and then returns the value 25 to the main where it is assigned to y again
FUNCTION DECLRATION:

Function declaration is also known as function prototype, it consists four (4) parts
i. Function type (return type)
ii. Function name
iii. Parameter list
iv. Terminating semicolon

Syntax: ( 1)

function_type function name(parameter list);


example—
int mul( int x, int y); // function declaration or function prototype

Syntax ( 2):
int mul ( int , int);
mul (int a, int b);
mul (int, int );

When a function doesn’t take any parameters and does not return any value, its prototype is
written as void display ( void);.
Prototype declaration may be placed in two places in a program.

RECURSION
A function may call itself directly or indirectly called recursion.
main ( )
{
printf( “this is an example of recursion” );
main ( );
}

FUNCTION(Q)

1. WAP to find sum of two numbers.


2. WAP to find even or odd.
3. WAP to find prime or not.
4. WAP to find sum of n numbers.
5. WAP to find factorial number.

RECURSION (Q)
1 ) WAP to find factorial number.
ARRAY
An array is a fixed size sequenced collection of element of the homogeneous data type.
Or
An array is a sequenced collection of related data items that share a common data name.
Or
An array is a collection of similar elements. The 1st element in the array is numbered zero, so the
last element is “1” less than the size of the array. An array is also known as subscripted variable.
Before using array its type and dimension must be declared. However big an array its elements
are always stored in contiguous memory locations.

Array symbol is [ ]  dimension


Array is always count start from zero.

Why use array ?


void main ( )
{
int x;
x=5;
x=10;
printf( “ \n x = %d “,x);
}

Out put 10 why ?

Because when a value 10 is assigned to x, the earlier value of x =5 is lost. So ordinary variable
can store only one value at a time in this example.

So we want to store more than one value at a time in a single variable.


Example

 Arrange to percentage marks obtained by 100 students in ascending order.


 List of employee of an organization( age salary, emp-id)
 List of tempreture recorded in every hour.

In this particular example see which is better ?


i. Construct 100 variables to store percentage marks obtained by 100 differenet
students marks
ii. Construct one variable capable to storing or handling all the hundreds value

The 2nd point is better

int mark [15];

Here int is data type


Mark is variable

[ 15 ] is the how many elements of type int will be an array. The no.
is often called the dimension of an array.

[ ] compiler known here use array

Array initialization:

Int num[6]= {2,4,6,12,9,5,19}


Int n[]= {2,4,5,45,5}
a) Till the array elements are not given any specific values, they are supposed to contain
garbage values.
b) If the array is initialized where it is declared mentioning the dimensions array.

An array element on memory ----:


int arr[ 8 ];

In this case memory reserved 16 byte because integer contains 2 byte. Here array is not
initialized all 8 values present in it would be garbage values.
2 14 16 8 10 9 7 70

1000 1002 1004 1006 1008 1010 1012 1014


Here 2 is an integer value & 1000 is the base of 1st array.

Types of an array: -
1) One dimensional array:

A list of items can be given one variable name using only one subscripts and such a variable
is called one dimensional array.
Declaration:
General form type variable name [ size];
If we want to represent a set of number say ( 20,35,45,50,60 ) by an variable number type
int, it declare:

int number [5];


number [0] = 20;
number [1] = 35;
number [2] = 45;
number [3] = 50;
number [4] = 60;

TWO DIMENSIONAL ARRAYS: -

We draw row and column in a table so we general form of two dimensional arrays as
type array-name [size1] [size2];
Example: -
int mat [3] [3];
Initialization values
int table [2] [3] = {0, 0, 0, 1, 1, 1}
The elements of row to 0 and second row to 1.
It is also equal to
int table [2] [3] = {{0, 0, 0}, {1, 1, 1}};
It is also written in
int table [2] [3] = {
{0, 0, 0},
{1, 1, 1}
};

MULTI-DIMENSIONAL ARRAY:-

‘C’ allows two or more dimensions.


General form is
type array-name [s1] [s2] [s3]………[sm];
Where s1 is the size of ith dimension
Int survey [3] [5] [12];

ARRAY:
1. WAP to accept 10 numbers & print them.
2. WAP to sum of 10 numbers.
3. WAP to accept 10 numbers & print the number is odd or even.
4. WAP to accept 10 characters & print them.
5. WAP to add ‘ n’ numbers .
6. WAP to accept any string & display that
7. WAP to count length of string.
8. WAP to check strings are contain same character or not.( using function strcmp)
9. WAP to check strings are contain same character or not.( without using strcmp function)
10. WAP to use strcpy( ) for copying of one string to another string.
11. WAP to check given number is palindrome or not.
12. WAP to check given word is palindrome or not.
13. WAP to convert lowercase character string to upper case.
14. WAP to enter your name & display your name.
15. WAP to enter name display 10 times & count 1 – 10.
16. v display reverse of a string.
17. WAP to find biggest & smallest number in an array.
18. WAP to find fibonic series.
19. WAP to for addition of matrix.
20. WAP to find swapping of two numbers.

POINTER
A pointer is a variable that contain & the address of a variable pointer can be used to access and
manipulate data stored in the memory.

BENEFITS:-
• Pointers are more efficient in handling arrays & data.
• It can be used to return multiple values from a function via function arguments.
• Pointer reduces length & complexity of program.
• It increases the execution speed & thus reduces the program execution time.
The addresses are number starting from 0. The last address depends on the memory size.
It symbolized with * (asterisk symbol)
int quantity = 179
here data type int,
integer variable is quantity, and its value is 179.
Let it address is 5002
Since memory addresses are simply numbers, they can be assign to some variable, which can be
stored in memory. So these variables are called pointer variable.
A pointer variable that contains an address, which is a location of another variable in memory. A
pointer is a variable, its value is stored in the memory is another location.

Example:-
Variable Value Address
quantity 179 5002
p 5002 5048
Let we assign the address of quantity to a variable p. Link in between p and quantity is
(here address of p is 5048).
Value of p is (5002) the variable is the address of variable quantity. The value of quantity (179) by
using the value of p (5002) and therefore, the variable of p point to the variable of quantity. So “p”
gets the name pointer.
Quantity 179 5000
P 5000 5048
int quantity = 179 int p = 5000

p = &quantity
5000 = 179
‘&’: - It is the operation in the scanf function the operator and immediately preceding a variable
returns the address of the variable associated with it.
So p = &quantity
5000 is the address location of quantity and to the variable of p and operator can be remembered
as “address of”.

DECLARATION OF POINTER VARIABLES: -

Data type *pt-name;


In this case the compilers tell three things about the variable pt name.
1. The asterisk * tells that the variable pt-name is a pointer variable.
2. pt-name needs a memory location.
3. pt-name points to variable of type data type.

Example: - int *p; /* integer pointer*/


Float *p; /* floating pointer*/
int quantity;
Int *p; //declaration
P = &quantity; // initialization
It also written in
int *p = &quantity;

POINTER:
1. WAP to addition of two using pointer.
2. WAP to find greatest number.
3. WAP to display 10 number using pointer.
4. WAP to swapping of two no using third variable.
5. WAP to swapping of two no without using third variable.
6. WAP to display your name.
7. WAP to reserve of a string using pointer.
8. WAP to find biggest & smallest no. using pointer.
9. WAP to find odd & even number.
10. WAP to accept 10 numbers & print them.
11. WAP to accept 10 numbers & sort them.

Structures & Union

A structure is a collection of one or more variables, possible of different types grouped


together under a single name for convenient handling.
We know array is the collection of same type of elements but it may be possible to take a
different type of elements together. It is useful to grouped the different of data which has some
meaningful information.
If we want to create of any person which has contains he name, age, weight and address
of that persons then we make the structure of the records as

Record

Name Age Weight Address

A structure can be declared by the key ward structure.


We can define the structure
struct tag <name> (An optional name called a structure tag)
{
member 1;
member 2;
…………
…………
member 3;
…………
…………
member m;
};
member 1, …… member ‘m’ are member of the structure.
If declare anther way
structure
{
member 1;
member 2;
…………
…………
member m;
}var;
Here var is the structure variable.
Ex:-
structure
{
char name [20];
int age;
char address[20]
float weight;
}rec;
Here name, age, weight and address is the member of this structure. rec is the structure variable.
For accessing any no of the structure we use dot (.) operation.
Ex:-
rec.name points to the member name, age, & address of any persons & display.

#include<stdio.h>
void main( )
{
struct
{
char name[20];

int age;
char address[20];
}rec
printf(“enter the name”);
scanf(“%s”,rec .name);
printf(“enter the age”);
sanf(“%d”,&rec.name);
printf(“enter the addresss”);
scanf(“%s”,rec.name);
printf(“name : %s\n”,rec.name);
printf(“age: %d\n”,rec.name);
printf(“address : %s\n”,rec.address);
}

ANOTHER TYPE OF DECLARATION

Structure record
{
char name [20];
int age;
char address[20];
}rec
Structure record person;
Here we define another structure variable person which has the same structure as record.

Initialization:
We can also initialize the value to the members of structrure as
struct
{
char name[20];
int age;
char address[20];
}rec = {“surech’, 24,”orissa”}
or
struct rec {
char name[20];
int age;
char address[20];
};
static struct rec person;

Here rec.name, rec.age, rec.address have values surech, 24, & newdelhi

Example :

#include< stdio.h>
#include< conio.h>
void main( );
struct
{
char name[20];
int age;
char address[ 20];
}
rec = { “ suresh”, 23, “ orissa”);
printf(“ %s%d%d\n”, rec.name, rec.age, rec.address);
}

Array of structure:
We known array is collection of same datatype of element.
Example:

We can teake 10 integer values is in the integer type array. Similary we can declare the array of
structure where every element of array of structure

struct rec{
char name[20];
int age;
char address[20];
}
person[10];
Here a person is an array of 10 element of person have structure of rec,means ach element of
person have three element I e name,age,address.
How structure elements are stored
Char title[ 20];
Int pages;
Float price;
};
STRUCTURE (Q)
1. WAP to accept name, age, address of any person.
2. WAP to accept record of 10 person which has name, age, & address and also
display them.
3. WAP to to display student name, marks( 6 student) and total marks & last find
out avg. WAP to display an employee information.
4. WAP to display book information.

:UNION:-
Union is same as structure. As the student structure members of different data types. In the
structure each member has its own memory location whereas members of union have same
memory location.
We can assign value to only one member at a time, so assigning value to another at that time
that has no memory.
When a union is declared, compiler automatically allocates a memory location to hold the largest
data type of members in the union. Thus the union is used for saving memory.
The concept of union is useful when it is not necessary to assign the values to all the members of
the union at a time.

UNION DECLARATION: -
Union union-name
{
Member 1;
Member 2;
------------
------------
}
Union is a keyword, it is necessary for declaration of union. The declaration of union variable is
same as structure.
Union union-name
{
Member 1;
Member 2;
------------
} variable-name;

This can also be declared as


Union union-name, variable-name;

1. WAP to display size of mark, grade use union.


-:FILE:-
File is a collection of data or text placed on the disk types such as:
• Sequential file :- data kept sequential
• Random access file :- data access and processed randomly.
Example of sequential file:- If we want to access the “44” records then 1st “43” records
should be read sequentially.
Example of random access file:- To reach “44” records.

FILE OPERATION IN ‘C’ PROGRAM: -


• Open a file
• Read the file/ Write data in the file
• Close the file

INPUT/OUTPUT FUNCTION:-
‘C’ supports three type of input/output function as follows:
• Character I/O
• String I/O
• File I/O

CHARACTER I/O:-
1. getchar( ):-
The function read character type data from standard input. This function
read one character at a time.
2. putchar( ):-
This function is point to one character at a time, which is taken by the
standard input.
2. getch( ):- This function read any alphanumeric from the standard input. The
different between getchar( ) and getch( ) is that getchar( ) continue access the
key board until the carriage return key is pressed. It stops accessing the
keyboard as soon as key is pressed.

Question :
WAP to display your name

You might also like