GD INFOTECH Complete C Language Notes Website
This page is organised topic-wise so that teaching becomes easy in class. Every topic opens separately, making explanation simple for teacher and students.
The content below is arranged from your uploaded notes and structured as a full teaching webpage.
1. Introduction & History
Start teaching from the background of C language.
History of C Language
How to teach this topic
First explain that programming languages developed step by step. Then show how C came after earlier languages.
C-------PROGRAMMING LANGUAGE-----------1972-----------DENNIS RITCHIE-------AT & T BELL LABS AMERICA-------B--------1970------KEN THOMPSON-----AT & T BELL LABS
HISTORY
COBOL FORTRAN
BCPL
CPL
B
C
C COMPILER :-Which compile the c program into machine language because computer understand only machine language that is 0 and 1 binary language. C EDITOR :- An Editor is an program much like a Word Processor that you use to write and edit the Source Code of any program .
2. Basics, Comments, Structure of C Program
Introduce the basic writing style of a C program.
Comments in C
Teaching tip
Tell students comments are ignored by compiler and used for explanation.
COMMENT COMMENT // FOR SINGLE COMMENT MULTIPLE LINE COMMENT /*……………….. …………………*/
Structure of C Program
STRUCTURE OF C PROGRAM
// COMMENT..........................................
#include<stdio.h>
#include<conio.h>
void main()
{
getch( );
}
Printf() and First Program of C
Printf()
Printf() is used to print on the output screen like
FIRST PROGRAM OF C
/* This programe will display introduction to c*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Printf(“introduction to c++”);
Printf(“introduction to c++”);
Printf(“introduction to c++”);
getch();
}
3. Data Types, Format Specifiers, Input & Output
Teach storage, format symbols and basic input functions.
Data Types in C
Data type in c Char- store alphabets Int- storing only integer value and occupied 2bytes on hard disk Float- all storing decimal values 4bytes in memory
Format Specifiers There are many format specifiers defined in C. Take a look at the following list: %i or %d int %c char %f float %lf double %s string
Validation Rules for Variable Names
Validation- Validation name rules- a. Variable name may be a combination of alphabets, digits or underscores and should not be more than 8 characters. b. First character must be an alphabet or an underscore (_). c. No commas or blank spaces are allowed in a variable name only underscore allowed. int bas,sal Invalid int bas_sal Valid d. Among the special symbols, only underscore can be used in a variable name. e.g.: emp_age, item_4, etc. e. No word, having a reserved meaning in C can be used for variable name.
Program 2 - Sum of Two Numbers
Programe 2
//sum of two given numbers
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Int num1=25,num2=35,num3;
Clrscr();
Num3=num1+num2;
Printf(“sum is %d”,num3);
getch();
}
In the above programe first line will reserve the space for three
Memory
Num1 num2 num3
25 35 -
With statement
Num3=num1+num2;
Num1 num2 num3
25 35 60
Program 3 - Character Variable
Programe 3
/*use of character variable*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Char alphavar;
Char numvar=’7’;
Clrscr();
Alphavar=’A’;
Printf(”value of alphavar is %c”,alphavar);
Printf(”value of numvar is %c”,numvar);
Numvar=’B’;
Printf(”value of number after changing %c“,numvar);
getch();
}
getch(), getche() and getchar()
getch(),getche() and getchar functions The above three functions are single character function means the above functions can be used to input single character from the keyboard. getch() 1. The character input by the user is not displayed on the screen 2. It does not wait for enter
Programe Example
/*use of getch()*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
char ch;
clrscr();
ch=getch();
getch();
}
output
when we input or press any key from the keyboard it does not display anything on the screen because the character input by the user is not displayed on the screen
and if we want to show this character on the screen we have to use putchar() so putchar() is single character output function
/*use of putchar()*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
char ch;
clrscr();
ch=getch();
putchar(ch);
getch();
}
getche()
1. The character input by the user is displayed on the screen
2. It does not wait for enter
/*use of getche()*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
char ch;
clrscr();
ch=getche();
putchar(ch);
getch();
}
output
when we input or press any key from the keyboard it displays on the screen because as per the feature of getche() the character input by the user is displayed on the screen and again that character will also displayed due to the putchar() and please note here we don't press any enter key to display the character second time on the screen because as per second feature it does't require to wait for enter key.
getchar()
1. The character input by the user is displayed on the screen
2. It waits for enter key.
/*use of getchar()*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
char ch;
clrscr();
ch=getchar();
putchar(ch);
getch();
}
output
when we input or press any key from the keyboard it displays on the screen because as per the feature of getchar() the character input by the user is displayed on the screen but it is to be noted that function putchar() will not be working till you press enter key from the keyboard because it is the second feature that it wait for enter key to run the next line
Program 4 - Float Variables and Format Width Specifier
Programe 4
/*float variables in c*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Float numvar1=11.57;
Float numvar2=2.5;
Clrscr();
Printf(”value of numvar1 is %f”,numvar1);
Printf(”value of numvar2 is %f”,numvar2);
getch();
}
format width specifier means to give spaces to write the output for example if we want to write d=40 after two space we will give the format specifier like %4d means two blank spaces and two spaces for 4 and 0
Programe
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Int i=40;
Clrscr();
Printf(”%d\n”,i); // it will take no space
Printf(”%4d\n”,i); // it will take two blank spaces
Printf(”%8d\n”,i); // it will take 6 blank spaces because 2 reserve for 40
Printf(”%10d\n”,i);// it will 8 blank spaces and 2 reserve for 40
getch();
}
This example gives a demonstration:
#include <stdio.h>
int main() {
int x = 123;
printf("Printing 123 using %%0d displays %0d\n", x);
printf("Printing 123 using %%1d displays %1d\n", x);
printf("Printing 123 using %%2d displays %2d\n", x);
printf("Printing 123 using %%3d displays %3d\n", x);
printf("Printing 123 using %%4d displays %4d\n", x);
printf("Printing 123 using %%5d displays %5d\n", x);
printf("Printing 123 using %%6d displays %6d\n", x);
printf("Printing 123 using %%7d displays %7d\n", x);
printf("Printing 123 using %%8d displays %8d\n", x);
printf("Printing 123 using %%9d displays %9d\n", x);
return 0;
}
Output:
Printing 123 using %0d displays 123
Printing 123 using %1d displays 123
Printing 123 using %2d displays 123
Printing 123 using %3d displays 123
Printing 123 using %4d displays 123
Printing 123 using %5d displays 123
Printing 123 using %6d displays 123
Printing 123 using %7d displays 123
Printing 123 using %8d displays 123
Printing 123 using %9d displays 123
Notice that in the first 4 cases, 123 is displayed in the same way as when you normally use %d. Why? Simple - the number of spaces on the screen that 123 can be displayed is greater than or equal to 3.
But also, if you write %09d, the program will display zeros before the number itself. In the above example, it will display:
Printing 123 using %09d displays 000000123
An advantage of using this, is that you can count the minimum field of the number!
This example should help clarify things:
#include <stdio.h>
int main() {
float x = 3.141592;
printf("Printing 3.141592 using %%f \t displays %f\n", x);
printf("Printing 3.141592 using %%1.1f \t displays %1.1f\n", x);
printf("Printing 3.141592 using %%1.2f \t displays %1.2f\n", x);
printf("Printing 3.141592 using %%3.3f \t displays %3.3f\n", x);
printf("Printing 3.141592 using %%4.4f \t displays %4.4f\n", x);
printf("Printing 3.141592 using %%4.5f \t displays %4.5f\n", x);
printf("Printing 3.141592 using %%09.3f displays %09.3f\n", x);
printf("Printing 3.141592 using %%-09.3f displays %-09.3f\n", x);
printf("Printing 3.141592 using %%9.3f displays %9.3f\n", x);
printf("Printing 3.141592 using %%-9.3f displays %-9.3f\n", x);
return 0;
}
Output:
Printing 3.141592 using %f displays 3.141592
Printing 3.141592 using %1.1f displays 3.1
Printing 3.141592 using %1.2f displays 3.14
Printing 3.141592 using %3.3f displays 3.142
Printing 3.141592 using %4.4f displays 3.1416
Printing 3.141592 using %4.5f displays 3.14159
Printing 3.141592 using %09.3f displays 00003.142
Printing 3.141592 using %-09.3f displays 3.142
Printing 3.141592 using %9.3f displays 3.142
Printing 3.141592 using %-9.3f displays 3.142
You may have noticed that if you use a negative value for the minimum width specifier, the output will not be affected by a zero after the minus sign.
Also, in the case for decimal numbers, the decimal point occupies a character space on the screen.
Introduction of User Input - scanf()
Introduction of user input
scanf is a function that reads data with specified format from a given string stream source
Programe5
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Int num1,num2,num3;
Clrscr();
Printf(”enter the first number”);
Scanf(“%d”,&num1);
Printf(”enter the Second number”);
Scanf(%d”,&num2);
Num3=num1+num2;
Printf(”sum is %d”,num3);
getch();
}
4. Operators & Exercises
Teach arithmetic and assignment operators with programs and classroom practice.
Mathematical Operators in C
Mathematical operater in c + plus _ minus * multiply % modulus
Programe6
/*programming using mathematical operaters*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Int num1,num2,num3,num4,num5;
Num1=2;
Num2=num1+2;
Num3=num1*num2;
Num4=num3/num2;
Num5=5%2;
Printf(”value of num1 is %d\n”,num1);
Printf(”value of num2 is %d\n”,num2);
Printf(”value of num3 is %d\n”,num3);
Printf(”value of num4 is %d\n”,num4);
Printf(”value of num5 is %d”,num5);
getch();
}
Output—
Value of num1 is 2
Value of num2 is 4
Value of num3 is 8
Value of num4 is 2
Value of num5 is 1
Mathematical Assignment Operators
Mathematical assignment operaters + = suppose a + =5 a=a+5 - = “ a - =5 a=a-5 * = “ a * =5 a=a*5 / = “ a / =5 a=a/5
Programe7
/*programe to show mathematical assignment operaters*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Int num1=2,num2=3,num3=4,num4=8;
Clrscr();
num1 + = 2;
num2 * = 2;
num3 - = 1;
num4 / = 2;
printf(”value of num1 is %d\n”,num1);
printf(”value of num2 is %d\n”,num2);
printf(”value of num3 is %d\n”,num3);
printf(”value of num4 is %d”,num4);
getch();
}
Output---
Value of num1 is 4
Value of num2 is 6
Value of num3 is 3
Value of num4 is 4
Exercises
EXERCISES:-
1. WAP to input principal, rate and time from keyboard.calculate simple interest
Hint…………..
si = p*r*t / 100;
Float p,r,t,si;
2. WAP to input basic salary, da,hra from the keyboard program to calculate the gross salary.
Hint………..
Gs=bs+da+hra;
3. WAP to input only the basic salary from keyboard only.
Da is 30% of basic salary
ta is 40% of basic salary
And gs=bs+da+ta
4. The distance between two cities ( in km ) are input through the keyboard WAP to convert and print distance in meters , feet, inches and centimeters.
M=km * 1000;
Cm=m* 100;
Inch=cm/2.54;
Ft=inch/12;
5. two number are input through the keyboard into the the location of c and d WAP to interchange the contents.
5. Decision Making
Use condition checking with if, else, else if and switch case.
Decision Making and Relational Operators
LESSON 2
DESION MAKING
WHEN ONE STATEMENT OR MORE STATEMENTS ARE EXECUTED ON THE BASIS OF THE TRUTH OF CONDITION THEN WE USE……………
if ___________________
if _________________else
if____elseif________else
RELATIONAL OPERATERS
In the above statement relational operaters are used……
> greater
< less than
= = equal to
! = not equal to
> = greater than or equal to
< = less than and equal to
Program Using if Statement Only
//PROGRAM USING IF STATEMENT ONLY
# Include<stdio.h>
# Include<conio.h>
Void main()
{
Int num1,num2;
Clrscr();
Printf(”enter the value of num1”);
Scanf(“%d”,&num1);
Printf(”enter the value of num2”);
Scanf(“%d”,&num2);
If(num1>num2)
{
Printf(”num1 is greater than num2”);
}
getch();
}
if...else Program
If____________else
If you are required to execute one out of two statement than you have to use if__else
/*PROGRAM TO USE IF---------ELSE*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int a,b;
clrscr();
printf(”enter the value of a)”;
scanf(“%d”,&a);
printf(”enter the value of b”);
scanf(“%d”,&b);
if(a>b)
printf(“a is greater”);
else
printf(“b is greater”);
getch();
}
Nested if and else if Program
NESTED IN BLOCKS
IF ALTERNATE ARE MORE THAN ONE
//PROGRAMING USING IF_____ELSE IF_____ELSE
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int marks;
clrscr();
printf(“enter the marks”);
scanf(“%d”,&marks);
if(marks>=85)
printf(“merit”);
else
{
if(marks>=60)
printf(“first division”):
else
{
if(marks>=50)
printf(“second division”);
else
{
If(marks>=40)
Printf(“third division”);
else
printf(“fail”);
}
}
}
getch();
}
/*one more programe of nested if’s*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int day;
clrscr();
printf(“enter week day”);
scanf(“%d”,&day);
if(day= =1)
printf(“Sunday”);
else
{
if(day= =2)
printf(“monday”);
else
{
if(day= =3)
printf(”tuesday”);
else
{
if(day= =4)
printf(“wednesday”);
else
{
if(day= =5)
printf(“thursday”);
else
{
if(day= =6)
printf(“friday”);
else
{
if(day= =7)
printf(“Saturday”);
else
printf(“invalid day”);
}
}
}
}
}
}
getch();
}
Switch Case
SWITCH CASE
also use when we have to choose one out of many switch()
//PROGRAM TO IMPLEMENT SWITCH CASE
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int day;
clrscr();
printf(“enter week day”);
scanf(“%d”,day);
switch(day)
{
case1:
printf(”Sunday”);
break;
case2:
printf(”monday”);
break;
case3:
printf(”tuesday”);
break;
case4:
printf(”wednesday”);
break;
case5:
printf(”thursday”);
break;
case6:
printf(”friday”);
break;
case7:
printf("saturday”);
break;
default:
printf("invalid day”);
break;
}
getch();
}
Decision Making Exercises
EXERCISE
1) WAP TO INPUT THE AGE OF PERSON THROUGH KEYBOARD IF AGE > =18 THAN OUTPUT SHOULD BE PERSON IS ELIGIBLE FOR VOTE ELSE PRRSON IS NOT ELIGIBLE FOR VATE.
2) WAP TO INPUT YEAR FROM KEYBOARD OUTPUT SHOULD BE WHETHER THE YEAR IS LEEP YEAR OR NOT.
3) WAP TO INPUT THE NUMBER FROM KEYBOARD AND OUTPUT SHOULD BE WHETHER THE NUMBER IS EVEN OR ODD.
4) WAP TO INPUT THE NO. OF CALLS THROUGH PROGRAM TO CALCULATE THE TELEPHONE BILL RATE’S ARE AS FOLLOW:-------
ON FIRST 100 CALLS .80P PER CALL
NEXT 100 CALLS 1.00 PER CALL
NEXT 200 Calls 1.50 PER CALL
AND NEXT 2 PER CALL
SUPPOSE WE INPUT 350 CALLS THE BILL SHOULD BE
FIRST 100 100*.80 80
NEXT 100 100*1 100
NEXT 150 150*1.50 225
450
HINT:
cin<<n;
if(n<=100)
bill=n*.80;
else
{
if(n<=200)
bill=80+(n-100)*1;
else
{
if(n<=400)
bill=80+100+(n-200)*i.5;
else
bill=80+100+300+(n-400)*2;
}
}
6. Loops
Practice repeated execution with for, while, do while and nested loops.
Introduction to Loops
LOOPS
When one or more statements are to be executed again & again till certain condition then we use loop
Loop types………….
for loop
While loop
do while loop
for loop
for( initial value ; condition ; increment/decrement)
{
Statement
}
for Loop Programs
/*WAP to print 1 to 20 through for loop*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int i ;
clrscr();
for(i=1;i<=20;i++)
{
Printf(“%d”,i);
}
getch();
}
/*print number 20 to 1*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int i;
clrscr();
i >=1
for(i=20;i>=1;i--)
printf(%d”,i);
getch();
}
/*print number 1 to 100 divisible by 2*/
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int i;
clrscr();
for(i=2;i<=100;i=i+2)
printf(“%d\n”,i);
getch();
}
Logical Operators and Program
Logical operators
&& AND
! NOT
|| OR
//PRINT NUMBER 1 TO 100 DIVISIBLE BY 3 AND 5
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int i;
clrscr();
for(i=1;i<=100;i++)
{
If(i%3= =0 && i%5= =0)
}
Printf(“%d\n”,i);
getch();
}
Loop Exercises
EXCERSISE 1) WAP to print the number from 1 to 100 which are divisible by 5 2) WAP to calculate the sum of natural number. 3) WAP to input number from keyboard and through for loop calculate the factorial of such number
Nested Loop
NESTED LOOP OR INNER LOOP
Loop with in another loop is called nested loop
Like……..
For( )
{
For( ) //nested
}
//WAP to display the output of program as follows……..
1
12
123
1234
12345
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf(“%d”,j);
printf(“\n”);
}
getch();
}
Exercise 4 Patterns
EXERCISE 4
1) SHOW THE OUT PUT AS FOLLOW
1
22
333
4444
55555
HINT:-
for(i=1;i<=5;i++)
{
for(j=1;j<+i;j++)
printf(“%d”,i);
printf(“\n”);
2)SHOW THE OUTPUT AS FOLLOW…………
55555
4444
333
22
1
HINT:-
for(i=5;i>=1;i--)
{
for(j=1;j<+i;j++)
printf(“%d”,i);;
printf(“\n”);
}
}
3)SHOW THE OUTPUT AS FOLLOW……..
12345
1234
123
12
1
HINT:-
for(i=5;i>=1;i--)
{
for(j=1;j<+i;j++)
printf(“%d”,j);
printf(“\n”);;
}
While Loop
WHILE LOOP
While(condition)
{
……………..statement…………..
…………………………………..
}
//PRINT 1 TO 20 THROUGH WHILE LOOP
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int i=1;
clrscr();
while(i<=20)
{
Printf(“%d”,i);
i++;
}
getch();
}
EXERCISE=5
ATTEMPT ALL THE QUESTIONS OF FOR LOOP THROUGH WHILE LOOP.
HINT:------
1 55555
22 4444
333 333
4444 22
55555 1
int i=1,j; int i=5;
clrscr(); clrscr();
while(i<=5) while(i>=1)
{ {
j=1; j=1;
while(j<=i) while(j<=i)
{ {
Printf(“%d”,j) printf(“%d”,i);;
j++; j++;
} }
Printf(“\n”); Printf(“\n”);
i--;
i++;
} }
Do While Loop and Conditional Operator
DO WHILE LOOP
//PRINT 1 TO 20 THROUGH DO WHILE LOOP
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int i=1;
clrscr();
do{
printf(“%d”,i);
i++;
}
While(i<=20);
getch();
}
ATTEMPT ALL THE QUESTIONS OF FOR LOOP THRUGH DO WHILE LOOP
CONDITIONAL OPERATORS
these are also called as ternary operators because it has three expressions_...........
EXPRESSION 1 ? EXPRESSION 2 : EXPRESSION 3
TRUE
FALSE
//EVEN AND ODD THROUGH CONDITION OPERATORS
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“enter the number”);
scanf(“%d”,&a);
a%5= =0?printf(“even number”):printf(“odd number”);
getch();
}
//ANOTHER PROGRAM OF USING CONDITIONAL OPERATOR
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,greater;
clrscr();
printf(“enter the first number”);
scanf(“%d”,&a);
printf(”enter the second number”);
scanf(“%d”,&b);
greater=(a>b)?a:b;
printf(“the greater value is %d”,greater);
getch();
}
7. Arrays
Teach one dimensional array, two dimensional array, input, largest value and sorting.
One Dimensional Array
ARRAY
Similar set of elements are called array.
ONE DIMESIONAL ARRAY
ARRAY DECLARATION
int marks[5]
marks[0] marks[4]
INITIALISATION OF ARRAY
int marks[5]={10,11,25,30,35}
marks[0] marks[4]
10 11 25 30 35
//SIMPLE ARRAY PROGRAM
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int arr[7]={11,12,13,14,15.16,17};
int I;
clrscr();
printf(“contents of array”);;
for(i=0;i<7;i++)
{
Printf(“%d”,arr[i])
Printf(’/t’);
}
getch();
}
//INPUT IN ARRAY THROUGH KEYBOARD
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int arr[7],i;
clrscr();
printf(“enter 7 array”);
for(i=0;i<7;i++)
scanf(“%d”,&arr[i]);
printf(“the output of 7 array are”);
for(i=0;i<7;i++)
printf(“%d”,arr[i]);
printf(’\t’);
getch();
}
//LARGEST NUMBER OF AN ARRAY
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int arr[10],I,n,lar;
clrscr();
printf(“enter 10 values”);
for(i=0;i<10;i++)
scanf(“%d”,&arr[i]);
lar=arr[0];
for(i=0;i<10;i++)
{
If(lar<arr[i])
lar=arr[i];
}
Printf(“largest value=%d”,lar);
getch();
}
/* SORT READ NUMBER FROM INPUT DEVICE AND SORT THEM IN ASSENDING ORDER */
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int arr[10];
int i,j,n,temp;
clrscr();
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0;i<10;i++)
printf("\t%d",arr[i]);
getch();
}
Two Dimensional Array
TWO DIMESIONAL ARRAY
The array which have two dimesions first for row and other from column is called two dimesional array.
DECLARATION:------
int aee[3][3];
MEMORY PICTURE
0 1 2
0
1
2 0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
INITIALISATION OF ARRAY
int arr[3][3]={ 45, 47,50
70,80,90
100,120,130}
MEMORY PICTURE
0 1 2
0
1
2 45 47 50
70 80 90
100 120 130
//SIMPLE PROGRAM OF 2D ARRAY
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int arr[3][3]={ 45, 47,50
70,80,90
100,120,130}
Clrscr();
printf("contents of array are \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf(%d",arr[i][j]);
printf("\n");
}
getch();
}
//SUM OF MARIX 3*3
# Include<stdio.h>
# Include<conio.h>
Void main()
{
int arr[3][3],arr1[3][3],add[3][3];
int i,j;
clrscr();
printf("enter the contest of first array”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&arr[i][j]);
}
printf("enter the contest of second array”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&arr1[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d",arr[i][j]);
add[i][j]=arr[i][j]+arr1[i][j];
}
printf("addition of 3*3 matrix is \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d",arr[i][j]);
printf("\n");
}
getch();
}
8. Strings
Explain character arrays and string handling functions.
Introduction to String
STRING
Character arrays are called string.
Declaration:----------
Char arr[5]=”sunil”
s
u
n
i
l
arr[0] arr[4]
but the above statement will be wrong because there is no space for ‘/0’ which must be at the end.
Char arr[6] =”sunil”
s
u
n
i
l
‘/0’
arr[0] arr[5]
Simple String Programs
//SIMPLE PROGRAM OF STRING
#include<stdio.h>
#include<conio.h>
Void main()
{
Char name[ ] =”cal c computer”;
Clrscr();
printf(”name is \n”);
printf("%s",name);
getch();
}
//wap which read a string and then print that
#include<stdio.h>
#include<conio.h>
Void main()
{
Char str[20];
Clrscr();
printf("enter string \n”);
scanf("%s",str);
printf("the string is %s”,str);
getch();
}
String Handling Functions
STRING HANDLING FUNCTION:------------------------ Strlen() → find length of string Strcat() → append the contents of string at the end of first Strcpy() → copy the contents of second string in first Strcmp() → compare two strings Strupr() → convert lower case string to upper case Strlwr() → convert upper case string to lower case Strrev() → reverse the string
//STRLEN()
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char str[50];
int n;
Clrscr();
printf("enter the string”);
scanf("%s",str);
n=strlen(str);
printf("length is %d”,n);
getch();
}
//STRCAT()
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char str[30],str1[15];
Clrscr();
printf("enter the first string”);
scanf("%s",str);
Cout<<”enter the second string”;
scanf("%s",str);
Strcat(str,str1);
printf("string after concatenate is %s",str);
getch();
}
//STRCPY()
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char str[15],str1[15];
Clrscr();
printf("enter the second string”);
scanf("%s",str);
Strcpy(str,str1);
printf("string after copy %s”str);
getch();
}
//STRCMP()
Compare two strings the result of this function is given as…..
If both are equal value given by it are 0
If str is less as per
ASCII code value is <0
If str is greater as per
ASCII code value is >0
//STRCMP()
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char str[15],str1[15];
int n;
Clrscr();
printf("enter the first string”);
gets(str)
printf("enter the second string”);
gets(str1)
n= strcmp(str,str1);
if(n= = 0)
printf("both strings are equal”);
if (n<0)
printf("string is lower as per ASCII code”);
if(n>0)
printf("string is greater as per ASCII code”);
getch();
}
//STRUPR()
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char str[15];
Clrscr();
printf("enter the contents of lower case”);
gets(str);
Cout<<”contents in upper case are”<<endl;
puts(str);
getch();
}
//STRLWR()
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char str[15];
Clrscr();
printf("enter the contents of upper case”);
gets(str);
printf("contents in lower case are \n”);
puts(strlwr(str));
getch();
}
//STRREV()
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char str[15];
Clrscr();
printf("enter the string”);
gets(str);
printf(”string in reverce order is \n”);
puts(strrev(str));
getch();
}
9. Functions
Break large programs into smaller reusable parts.
Introduction to Function
FUNCTION
When our program become longer and complex then it converts into small parts that small parts are called function.
OR
We can define it as a set of statements that can be executed as any time by using only the function name.
FUNCTION CAN BE :---
Void:- those which does nit return any value to the main function are called void type function.
Return:-those which return same value to the main are called return type function.
With argument:-the function which have same arguments within its are called with arguments function.
Without argument:-those which hanve no arguments with its function are called without arguments.
Void Function Without Argument
// SUM OF TWO NUMBERS VOID WITHOUT ARGUMENT FUNCTION
#include<stdio.h>
#include<conio.h>
Void main()
{
Clrscr();
add( );
getch();
}
Void add( )
{
int a,b,s;
printf("enter the value of a”);
scanf(“%d”,&a);
printf("enter the value of b”);
scanf(“%d”,&b);
s=a+b;
printf("sum is %d”,s);
}
Void Function With Argument
//FUNCTION VOID AND WITH ARGUMENT
#include<stdio.h>
#include<conio.h>
Void main()
{
int a,b;
clrscr();
printf("enter the value of a”);
scanf(“%d”,&a);
printf("enter the value of b”);
scanf(“%d”,&b);
add(a,b);
getch();
}
Void add(int x,int y)
{
int z;
z= x+y;
printf("sum of two integer no. %d",z);
]
Function With Different Types of Arguments
//FOLLOWING IS THE PROGRAM WITH DIFFERENT TYPES OF ARGUMENT
#include<stdio.h>
#include<conio.h>
Void main()
{
int n;
char section;
float per;
clrscr();
printf("enter section a b or c”);
scanf(“%c”,§ion);
printf("enter total no. of students”);
scanf(“%d”,&n);
cout<<”enter percentage”;
scanf(“%f”,&per);
display(section,n,per);
getch();
}
void display(char s, int t, float p)
{
printf("the section %c\n”,s);
printf("total no. of students %d\n”,t);
printf("percentage %f\n”,p);
}
Return Type Function
//example of function type with arguments
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c
clrscr();
printf("enter the first no.”);
scanf(“%d”,&a);
printf("enter the second no.”);
scanf(“%d”,&b);
c=sum(a,b);
printf("sum is %d”,c);
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
return z;
}
Array and Function
//ARRAY AND FUNCTION
#include<stdio.h>
#include<conio.h>
void maim()
{
int a[10];
int i;
clrscr();
printf("enter the 10 array”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
output(a);
getch();
}
void output(int a[])
{
int i;
for(i=0;i<10;i++)
printf("\n%d",a[i]);
}
//SUM OF ARRAY THROUGH FUNCTION AND ARRAY
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[10];
int I;
cout<<”enter 10 array”;
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
sumarray(a);
getch();
}
Void sumarray (int x[] )
{
int value=0,i;
for(i=0;i<10;i++)
value=value+x[i];
printf("sum of array %d”,value);
}
10. Pointers & Passing Arguments
Explain address, pointer declaration and call by value / call by reference.
Pointer Basics
POINTER
Pointer is a variable which hold the address of another variable.Pointer variable can't hold the value of any variable it stores only the address of other variable.
Declaration of pointer variable
int *p;
int x=5;
x
2048 Adress of variable
p=&x; By this statement adress of x will be stored in pointer variable p like below
p
Adress stored in pointer variable p
2056
it is to be noted that pointer variable data type will be same as of the data type of a variable whose address is stored like if we want to store the address integer type variable then pointer variable will also be integer
like int x=5;
in the above case if we want to store the address of x variable in any pointer variable like p we must have to taken pointer variable p as integer
int *p;
like supose variable float y=25.6 and we want to store its address in pointer variable p then we must declare pointer variable as float *p
in character case same position will be applicable
Program example
#include<stdio.h>
#include<conio.h>
void main()
{
int x=5,*p;
clrscr();
p=&x;
printf("value of x is %d",x); // output 5
printf("value of x is %d",&x); //show address of x variable
printf("value of x is %d",p); //show address of x variable
printf("value of x is %d",*p); //value of x
getch();
}
means if pointer is shown in print statement with * then it will show the value of variable where address is stored in it.
Array with Pointer
Array with pointer
pointer when incremented always point to immediatly next block of its own type
supose
int x,*p;
x
2000
p
2002
p=&x;
p
2002
p=p+1;
if we add 1 to pointer it will jump to the nect block 2002
#include <stdio.h>
int main( )
{
int *p;
int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
/* Assigning the address of val[0] to pointer: 88820*/
p = &val[0];
for ( int i = 0 ; i <= 6 ; i++ )
printf("%d",*(p+i));
getch();
}
Call by Value and Call by Reference
Two Ways of Passing Argument to Function in C Language :
1. Call by Reference
2. Call by Value
Let us discuss different ways one by one –
A.Call by Value :
#include<stdio.h>
#include<conio.h>
int main() {
int num1=50,num2=70;
printf("\nNumber 1 : %d",num1);
printf("\nNumber 2 : %d",num2);
getch();
}
void interchange(int number1,int number2)
{
int temp;
temp = number1;
number1 = number2;
number2 = temp;
}
Call by Value
While Passing Parameters using call by value , xerox copy of original parameter is created and passed to the called function.
Any update made inside method will not affect the original value of variable in calling function.
In the above example num1 and num2 are the original values and xerox copy of these values is passed to the function and these values are copied into number1,number2 variable of sum function respectively.
As their scope is limited to only function so they cannot alter the values inside main function.
B.Call by Reference/Pointer/Address
#include<stdio.h>
int main() {
int num1=50,num2=70;
interchange(&num1,&num2);
printf("\nNumber 1 : %d",num1);
printf("\nNumber 2 : %d",num2);
return(0);
}
void interchange(int *num1,int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
Explanation : Call by Address
While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function.
Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.
11. Recursion
Show how a function can call itself again and again.
Recursion Definition and Factorial Program
RECURSION
WHEN the function calls it self again AND AGAIN that is called recursion.
EX:_______
#INCLUDE<stdio.h>
Void main()
{
Void funct 1();
…………………………
………………………..
funct 1();
{
void funct 1()
}
funct 1():
}
//example of factorial through recursion
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
clrscr();
printf("enter the integer no.”);
scanf(“%d”,&n);
x=fact(n);
printf("factorial is %d”,x);
getch();
}
fact(long int n)
{
int value;
if(n= =1)
return(1);
else
value=n*fact(n-1)
return value;
}
24
value=5*fact(4) 6
→ 4*fact(3) 2
→3*fact(2) 1
→2*fact(1)
value=120
12. Structure and Union
Teach grouped data, array of structures, nested structure and union.
Structure Basics
STRUCTURE AND UNION
structure is a collection of data items which canbe of different type int, float or anything.
DECLARATION:-
structure student {
char name[10];
int roll_no;
char branch[10];
int sem;
};
Simple Program Using Structure
//SIMPLE PROGRAM USING STRUCTURE
#include<stdio.h>
#include<conio.h>
void main()
{
struct student {
char name[10];
int roll_no;
char branch[10];
int sem;
};
void main()
{
student std1,std2;
clrscr();
printf("enter the details of first student\n");
printf("enter the name”);
get(std1.name);
printf("enter the roll_no:");
scanf(“%d”,&std1.roll_no);
cout<<”enter branch”;
gets(std1.branch);
printf("enter semester”);
scanf(“%d”,&std1.sem);
printf(”enter the details of second student”);
printf(”enter the name”);
gets(std2.name);
printf(”enter the roll_no:);
scanf(“%d”,&std2.roll_no);
printf(”enter branch”);
gets(std2.branch);
printf(”enter semester”)
scanf(“%d”,&std2.sem);
printf(”details of first students are”);
printf("%s \n%d \n%s \n%d",std1.name,std1.roll_no,std1.branch,std1.sem);
cout<<”details of second students are”<<endl;
printf("%s \n%d \n%s \n%d",std2.name,std2.roll_no,std2.branch,std2.sem);
getch();
}
/* THE FOLLOWING PRIGRAM WILL ALSO IMPLIMENTS THE STRUCTURE */
#include<stdio.h>
#include<conio.h>
struct employee
{
int emp_code;
char emp_name[20];
char designation[10];
float salary;
};
void main()
{
employee emp1,emp2;
clrscr();
printf("enter the details of first employee\n”);
printf("enter the employee code”);
scanf("%d",&emp1.emp_code);
printf("enter the employee name”);
scanf(“%s”,emp1.emp_name);
printf("enter designation”);
scanf(“%s”,emp1.designation);
printf("enter salary”);
scanf(“%f”,&emp1.salary);
emp2=emp1;
printf("employee code of second employee %d\n”,emp2.emp_code);
printf("employee name of second employee %s\n”,emp2.emp_name);
printf("employee designation is %s\n”emp2.designation);
printf("employee salary is %f”,emp2.salary);
getch();
}
Array and Structure
//ARRAY AND STRUCTURE
#include<stdio.h>
#include<conio.h>
struct student
{
int roll_no;
char student_name[20];
char address[15];
int age;
};
void main()
{
student std[10];
int i;
clrscr();
printf("accept the value into array”);
for(i=1;i<10;i++)
{
printf("enter the details of %d student”,i);
printf("enter roll no.”);
scanf(“%d”,&std[i].roll_no;
printf("enter student name”);
scanf(“%s”, std[i].student_name);
printf("enter address”);
scanf(“%s”, std[i].address;
printf("enter age”);
scanf(“%d”,&std[i].age);
}
printf("enter the records you enter are");
for(i=1;i<10;i++)
{
printf(" roll no. %d\n”,std[i].roll_no);
printf("student name %s\n”,std[i].student_name);
printf("address %s\n”,std[i].address);
printf("age %d”,std[i].age<<endl;);
}
}
}
getch();
}
Nested Structure
NESTED STRUCTURE
STRUCTURE WITH IN ANOTHER STRUCTURE IS CALLED NESTED STRUCTURE.
LIKE:-
struct tag1
{
data type member_name;
………………………
……………………..
struct tag2 member_name;
};
//PROGRAM USING NESTED STRUCTURE
#include<stdio.h>
#include<conio.h>
struct account
{
int account_no;
char account_type;
float account_balance;
};
struct employee
{
int emp_code;
char emp_name[20];
char designation[10];
float salary;
struct account account_details;
};
employee emp1;
clrscr();
printf(“accepting values for structure employee using variable empl”);
printf(“\n”);
printf(”enter the employee code”);
scanf(“%d”,&emp1.emp_code);
printf(”enter the employee name”);
scanf(“%s”, emp1.emp_name);
printf(“enter desidnation”);
scanf(“%s”, emp1.designation);
printf(“enter salary”);
scanf(“%f”,&emp1.salary);
printf(“enter account no.”);
scanf(“%d”,&emp1.account_details.account_no”);
printf(“enter account type 's' for saving and 'c' for current ”);
scanf(“%d”,&emp1.account_details.account_type”);
printf(“enter account balance”);
scanf(“%f”,&emp1.account_details.account_balance”);
printf(“displaying details of employee \n”);
printf(“employee code is %d\n”,emp1.emp_code);
printf(“employee name is %s\n”,emp1.emp_name);
printf(“employee designation is %s\n”,emp1.designation);
printf(“employee salary is %d\n”,emp1.salary);
printf(“account is %d”,emp1.account_details.account_number);
printf(“account type is %c\n”,emp1.account_details.account_type;
printf(“balance is %f”,emp1.account_details.account_balance);
getch();
}
Union
UNION
LIKE STRUCTURE BUT NOT STRUCTURE HOLD ONLY THE ONE VALUE THAT IS ASSIGNED AT THE LAST.
//PROGRAM USING UNION
#include<stdio.h>
#include<conio.h>
Void main()
{
union data
{
int i;
float f;
};
data d;
clrscr();
d.f=20.5;
d.i=10;
printf(“first number %d\n”,d.i);
printf(“second number %f”,d.f);
d.f=20.5;
printf(“first number %d\n”,d.i);
printf(“second number %f”,d.f);
getch();
}
13. File Handling in C
Finish the course with permanent storage concepts and file functions.
Introduction to File Handling
file handling in c If we want to store data permanently or we want to store data in our computer's hard disk for future use then we have to use the concept of file handling.File name has two parts filename.extension like raman.txt,shyam.doc etc. The following are the functions that are used for file handling purpose and these all functions are found in header file stdio.h Functions mainly used in c are fopen() fclose() getc() putc() fgets() fputs() fprintf() fscanf() etc.
fopen():- fopen function is used to open the file .for opening file following steps are undertaken
step 1:-
in step 1 file pointer variable is created as below
syntax
FILE fie pointer
example
FILE *fp;
fopen() function will create a new file if file already not exists and if exists it will open a file only which has already beem made.
fopen() function syntax
filepointer=fopen("file name","mode");
so we require three things to open the file