c Program Practical With Theory
c Program Practical With Theory
standard.
reasons.
Easy to learn
Structured language
Text Editor
UNIX.
The files you create with your editor are called source files and
code. The source files for C programs are typically named with
Execute it.
The C Compiler
compiler.
compiler,
programming languages.
Why to use C?
C was initially used for system development work, in particular
Semicolons ;
In C program, the semicolon is a statement terminator. That is,
Identifiers
A C identifier is a name used to identify a variable, function, or
C Data Types
In the C programming language, data types refer to an extensive
Basic Types: They are arithmetic types and consists of the two
IntegerTypes
Floating-PointTypes:-
C Variables:-
Variable Definition in C:
int i, j, k;
char c, ch;
float f, salary;
double d;
Escapesequence Meaning
\\ \ character
\' ' character
\" "character
\? ? character
\a Alertor bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octalnumber of one to three digits
C Operators:-
operators:
Arithmetic Operators
Relational Operators
Logical Operators
Arithmetic Operators:-
Relational Operators:-
Logical Operators:-
Operator Description Example
&& CalledLogical AND (A&& B)is false.
operator. If both the
operands are
non-zero, then
condition becomes
true.
|| CalledLogical OR (A|| B)is true.
Operator. If any of the
two operands is
non-zero, then
condition becomes
true.
! CalledLogical NOT !(A && B) is true.
Operator. Use to
reverses the logical
state of its operand. If a
condition is true, then
Logical NOT operator
will make false.
When we are saying Input that means to feed some data into
binary files.
addressed in the same way as files and following three file are
screen.
Decision Making in C
a statement or statements
programming languages:
C programming language assumes any non-zero and non-null
if statement
An if statement consists of a boolean expression followed by
language is:
if(boolean_expression)
*/
code after the end of the if statement (after the closing curly
brace) will be executed.
switch statement :-
A switch statement allows a variable to be tested for equality
Syntax
is as follows:
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
default : /* Optional */
statement(s);
the switch. The default case can be used for performing a task
executes a target
Syntax
while(condition)
statement(s);
}
Here, statement(s) may be a single statement or a block of
statements.
Flow Diagram
For Loop in C
A for loop is a repetition control structure that allows you to
Syntax
The syntax of a for loop in C programming language is:
1.
The init step is executed first, and only once. This step allows
initialize any loop control variables. You are not required to put a
as a semicolon appears.
2.
loop is executed. If it is
false, the body of the loop does not execute and flow of
3.
After the body of the for loop executes, the flow of control
jumps back up to
condition.
4.
Flow Diagram
do...while loop in C
Unlike for and while loops, which test the loop condition at the
loop.
Syntax
do
statement(s);
}while( condition );
Syntax
statement(s);
}
statement(s);
language is as follows:
while(condition)
while(condition)
statement(s);
statement(s);
programming language is as
follows:
do
statement(s);
do
statement(s);
}while( condition );
}while( condition );
break statement in C
The break statement in C programming language has the
1.
loop is immediately
2.
If you are using nested loops (i.e., one loop inside another
will stop the execution of the innermost loop and start executing
the block.
Syntax
break;
The Infinite Loop :-
A loop becomes infinite loop if a condition never becomes false.
loop are required, you can make an endless loop by leaving the
conditional expression
empty.
C Structures
C arrays allow you to define type of variables that can hold
C programming, which
each book:
Title
Author
Subject
Book ID
Defining a Structure
new data type, with more than one member for your program.
statement is this:
member definition;
member definition;
...
member definition;
structure:
struct Books
PracticalProgram
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
printf("\n\t\tbazar road,");
printf("\n\t\tbandra(w),");
printf("\n\t\tmumbai 400050");
getch();
Explanation :-
#include <stdio.h>
#include <conio.h>
void main()
//DeclaringAndIntitalizing Variables
char xyz= 'A';
float fnum=87.65;
clrscr();
printf("\n\n");
getch();
#include <stdio.h>
#include <conio.h>
#include<string.h>
void main()
//DeclaringVariables
int rollno;
float height;
char name;
clrscr();
scanf("%s",&name);
scanf("%d",&rollno);
scanf("%f",&height);
printf("\nRoll no is:%d",rollno);
printf("\nHeightis:%f\n",height);
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
int a,b,c,d;
c=25;
d=12;
scanf("%d",&a);
scanf("%d",&b);
sum = a+b;//Addition
minus = a-b;//Subtraction
multi = a*b;//Multiplication
div= b/a;//Division
increase = ++c;
decrease = --d;
//Displayingthe results
clrscr();
getch();
#include <stdio.h>
#include <conio.h>
void main()
//Declaringvariables
clrscr();
int a;
char b;
float pi;
getch();
#include<conio.h>
# include <stdio.h>
void main()
int a,b,c;
scanf("%d", &a);
scanf("%d “,&b);
// Displayingthe numbers before interchanging
c=a;
a=b;
b=c;
getch();
#include<conio.h>
#include <stdio.h>
void main()
{
float basic,da,hra,salary;
char d[15];
clrscr();
scanf("%s",&d);
scanf("%f",&basic);
da=basic*40/100;
hra=basic*25/100;
salary=basic+da+hra;
//Displayingthe details
if(salary>=80000)
else if(salary>=50000)
else if(salary>=25000)
else
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
int num1,num2,sum;
sum= num1+num2;
if(sum>100)
else
getch();
#include <stdio.h>
#include <conio.h>
void main()
int age;
char name;
clrscr();
scanf("%s", &name);
scanf("%d", &age);
if (age>=19)
else
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
int num1,num2,num3;
clrscr();
scanf("%d %d %d",&num1,&num2,&num3);
getch();
#include <stdio.h>
#include <conio.h>
void main()
char abc;
scanf("%c", &abc);
if(abc=='a' || abc=='A')
else if (abc=='e')
else if (abc=='i')
else if (abc=='o')
else if (abc=='u')
else
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
int a;
scanf("%d",&a);
switch(a)
break;
break;
break;
break;
break;
break;
default: printf("wrongchoice");
getch();
#include<stdio.h>
#include<conio.h>
void main()
int a,b,choice,add,sub,div,mul;
clrscr();
scanf("%d",&a);
printf("\nEnter 2ndNumber");
scanf("%d",&b);
scanf("%d",&choice);
add=a+b;
sub=a-b;
div=a/b;
mul=a*b;
switch(choice)
break;
break;
break;
break;
default: printf("wrongchoice");
}
getch();
#include <stdio.h>
#include <conio.h>
void main()
int product;
while (count<=5)
product= num1*num2;
printf("Product=%d\n", product);
count= count+1;
num1 = num1+1;
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
int x=1,n,r;
clrscr();
scanf("%d",&n);
while(x<=10)
r=n*x;
printf("\n%d*",n);
printf("%d=",x);
printf("%d ",r);
x++;
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
int i=1, j;
while(i <=10)
j=1;
while (j <= i)
printf("*");
j++;
printf("\n");
i++;
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
int number;
int sum=0;
scanf("%d", &number);
if (number>0)
while(number>0)
sum = sum+number;
number = number-1;
printf(“\nsum is : %d”,sum);
}
else
getch();
#include <stdio.h>
#include <conio.h>
void main ()
inta = 10;
do
a = a + 1;
}while( a < 20 );
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
int a,b,c;
clrscr();
scanf("%d",&b);
for(a=1;a<=10;a++)
c=a*b;
printf("%d*",b);
printf("%d=",a);
printf("%d\n",c);
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
int a,b;
for(a=1;a<=10;a++)
for(b=1;b<=a;b++)
printf("*",b);
printf("\n");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k,a,b,c;
for(i=1;i<=8;i++)
for(j=8;j>=i;j--)
printf("");
for(k=1;k<=i;k++)
printf("*");
printf("\n");
getch();
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c,x,y,z;
printf("Enter a number..\n");
scanf("%d",&x);
for(a=1;a<=x;a++)
for(y=1;y<=a;y++)
printf("");
for(z=1;z<=a;z++)
printf("* ");
printf("\n");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
int x[3];
x[0]=10;
x[1]=20;
x[2]=30;
printf("%d",x[0]);
printf("%d",x[1]);
printf("%d",x[2]);
getch();
#include<stdio.h>
#include<conio.h>
void main()
int x[2][3];
//firstrow of array
x[0][0]=10;
x[0][1]=20;
x[0][2]=30;
clrscr();
printf("%d",x[0][0]);
printf("%d",x[0][1]);
printf("%d",x[0][2]);
x[1][0]=40;
x[1][1]=50;
x[1][2]=60;
printf("\n%d",x[1][0]);
printf("%d",x[1][1]);
printf("%d",x[1][2]);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
gets(a);
gets(b);
if (strcmp(a,b) == 0)
Else
getch();
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char a[20],b[20];
clrscr();
scanf("%s",a);
printf("ENTER 2ndTHESTRING");
scanf("%s",b);
strcat(a,b);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char a[20],b[20];
int len;
clrscr();
scanf("%s",a);
len =strlen(a);
strcpy(b,a);
printf("copystringis %s",b);
getch();
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char a[20];
clrscr();
scanf("%s",a);
strrev(a);
getch();
#include<stdio.h>
#include<conio.h>
void main()
int a=10;
printf(“address of a =%u”,p);
printf(“value of a =%d”,*p);
getch();
#include<stdio.h>
#include<conio.h>
void main()
int x,*p1,**p2;
x=5;
p1=&x;
p2=&p1;
printf(“x=%d”,x);
printf(“address of x =%u”,&x);
printf(“address of p1=%u”,p1);
printf(“address of p2=%u”,p2);
getch();
}
#include<conio.h>
#include<stdio.h>
int main()
float m, n ;
clrscr();
// function call
n = square ( m ) ;
getch();
{
float p;
p= x * x ;
return ( p );
#include<stdio.h>
#include<conio.h>
#include<string.h>
structBooks
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main()
{
structBooks Book1; /* Declare Book1 of type Book */
clrscr();
/* book 1specification */
Book1.book_id = 6495407;
/* book 2 specification */
strcpy(Book2.title,"Telecom Billing");
Book2.book_id= 6495700;
/* printBook1 info */
/* printBook2 info */
printf( "\nBook 2 title : %s", Book2.title);
getch();
QuestionAndAnswerForinterviewPurpose
a variable/function
variable/function).
binary trees.
aid in debugging?
Placing comment symbols /* */ around a code, also referred to
the code. The idea is that if the code is in fact correct, you
saves you time and effort on having to retype the codes if you
operations.
times as specified by the outer loop. For each turn on the outer
loop, the inner loop is first performed.
code?
codes, it will still work without error if you used it for a single line.
statements.
What are header files and what are its uses in C
programming?
Header files are also known as library files. They contain two
constants?
Can I use “int” data type to store the value 32768? Why?
need arises. For example: you can have a code like ” printf
program?
only those header files that would contain the functions you will
increase the overall file size and load of the program, and is not
used, the rest are accessible through the index name (grade[0],
was placed there in the first place. Comments begin with /* and
program.
What is debugging?
During program compilation, errors that are found will stop the
output is met.
errors?
compiler.
TRUE.
stored in a variable?
To get the length of a string value, use the function strlen(). For
behave like a high level language while at the same time can
programming in C?
“a++”?
infinite loops.
program?
necessarily move from one statement to the next one, but rather
statement?
The basic data types are int, char, and float. Int is used to
is omitted?
program statement?