Chapter - 01: "Variables, Constants & Expressions": (C - Programming)
Chapter - 01: "Variables, Constants & Expressions": (C - Programming)
B.Tech. 1st, 2nd, 3rd & 4th Sem, B.C.A., B.Sc. (Computers & I.T.), PGDCA,
B.Pharmacy
Teacher : Sandeep Sangwan (9417565720, 9417524702, 9463741635)
S.C.F. 204, 2nd Floor (Above Chandigarh Sweets Pvt. Ltd.), Sec-14,
Panchkula
[ C PROGRAMMING ]
STRUCTURE OF C PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
2
clrscr();
printf("Hello");
getch();
}
#include : is a preprocessor directive. It is a indication for the C compiler to add
the particular header file inside the program.
stdio.h : standard input output header file
conio.h : console input output header file
void
: means empty or null. i.e. the function is not returning any value.
main( ) : is a function nessary for C program. Every C program must contain
atleast one function main().
clrscr( ) : It is a library function defined inside conio.h header file and it is used
to clear the screen.
getch( ) : This function is also a library function define inside conio.h header file
and it is used to get character from the keyboard for output.
printf( ) : Standard output function used to display given data.
{
: indicates the beginning of the function
}
: indicates the end of the function.
;
: Statement terminator used to terminate a C-statement.
C Character Set :
Digits
Special
Symbols
A, B, C, ..,Y, Z
a, b, c,, y, z
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
~ ` ! @ # $ % ^ & * ( ) _ - + =
| \ { } [ ] : ; < > , . ? /
CONSTANTS :
C-Programming
3
Types of C Constants :
C constants can be divided into two major categories:
(a) Primary Constants
(b) Secondary Constants
Primary Constants
Integer Constant
Real Constant
Character Constant
String constant
Secondary Constants
Array
Pointer
Structure
Union
PRIMARY CONSTANTS :
(a) Integer constants : Integer constants are whole number without any decimal part.
Variables can be declared as integers in the following ways :
int x=5 , y=6;
short int x =3, y=10;
long int x=21 , y=636;
(b) Floating point constants : Floating point number are the numbers containing decimal
points (Real numbers). They may be written in one of the two forms called fractional form
or exponent form.
A real number in fraction form consists of signed or unsigned digits including a
decimal point between digits. The following are some real numbers in fractional form:
2.0, 17.5,
- 13.0, - 0.00625
A real number in exponent form consists of two parts : mantissa and exponent. For
instance 5.8 can be written as 0.58 x 101. The following are some real numbers in
exponent form:
152E05, 1.52E07, 0.152E08, 152.0E08, 152E+8, 1520E-04
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
4
Range
-128 to +127
0 to 255
-32768 to +32767
Size
1 Byte
1 Byte
2 Bytes
Format
%c
%c
%d
2 Bytes
%u
0 to 216
i.e. 0 to 65536
-231 to (231-1)
%ld
0 to (232 1)
float
-231 to (231-1)
4 Bytes
or 31 bits
4 Bytes
or 32 bits
4 Bytes
-263 to (263-1)
8 Bytes
%lf
long double
-279 to +(279-1)
10 Bytes
%Lf
%lu
%f
C-Programming
5
Correct
count
test23
high_bal
Incorrect
1count
hi!there
highbalance
Keywords are the reserved words whose meaning has already been explained to
the C compiler. The keywords cannot be used as variable names because if we do so we
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
6
are trying to assign a new meaning to the keyword, which is not allowed by the computer.
There are 32 keywords available in C. The complete list of keywords in C are given
below :
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
far
for
goto
if
int
long
near
register
return
short
signed
static
struct
switch
typedef
union
unsigned
void
while
OPERATORS :
An operator is a symbol or letter which causes the compiler to take an action and
yield a value. The operations being carried out are represented by operators. An operator
acts on different data items/entities called Operands.
TYPES OF OPERATORS
1.
2.
3.
4.
5.
1.
Arithmetic Operators
Relational Operators
Logical Operators
Ternary Operators
Increment and Decrement Operators
Arithmetic Operators
C-Programming
7
+
*
/
%
Addition
Subtraction
Multiplication
Division
Modulo(Remainder of an integer division)
Addition Operator(+)
int a = 4, b = 2, c;
c = a + b;
Output : c = 6
Subtraction Operator(-)
int a = 4, b = 2, c;
c = a - b;
Output : c = 2
Multiplication Operator(*)
int a = 4, b = 2, c;
c = a * b;
Output : c = 8
Division Operator(/)
int a = 5, b = 2, c;
c = a / b;
Output : c = 2
Modulus Operator(%)
int a = 5, b = 2, c;
c = a % b;
Output : c = 1
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
8
C-Programming
9
{
clrscr( );
printf("Name : Himani Madan");
printf("\nAddress : #1261/12,Pkl");
printf("\nPhone : 9417565720");
printf("\n\nNAME\tADDRESS\t\tPHONE");
printf("\nSurbhi\t#1211/14,Pkl\t8712828");
getch( );
}
Prog. 3 : WAP for Integer Initialization
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int num = 27;
printf("Given integer is : %d",num);
getch( );
}
Prog. 4 : WAP for Integer Input from user
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int num;
printf("Enter any integer : ");
scanf("%d",&num); // & stands for memory address of variable
printf("\nGiven integer is : %d",num);
getch( );
}
Prog. 5 : WAP for Floating point number Initialization
#include<stdio.h>
#include<conio.h>
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
10
void main( )
{
clrscr( );
float price;
price = 500.75;
printf("Given price is : %.2f",price);
getch( );
}
C-Programming
11
void main( )
{
clrscr( );
char ch;
printf("Enter any character value : ");
scanf("%c",&ch);
printf("\nGiven character is : %c",ch);
getch( );
}
Prog. 9 : WAP for string initialization
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
char name[20] = "Sandeep Sangwan";
printf("%s",name);
getch( );
}
Prog. 10 : WAP for string input from user
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
char name[20];
printf("Enter name : ");
gets(name);
printf("\nMy name is : %s",name);
getch( );
}
Prog. 11 : WAP to accept two integers and display their sum, difference, product,
quotient and remainder
#include<stdio.h>
#include<conio.h>
void main()
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
12
{
clrscr( );
int a,b;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
printf("\nSum is : %d",a+b);
printf("\nDifference is : %d",a-b);
printf("\nProduct is : %d",a*b);
printf("\nQuotient is : %d",a/b);
printf("\nRemainder is : %d",a%b);
getch( );
}
Prog. 12 : WAP to accept the name, basic salary, HRA and DA of an employee.
Calculate and display the gross salary of employee.
gs = bs+hra+da
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
char name[20];
float bs,hra,da,gs;
printf("Enter name of employee : ");
gets(name);
printf("Enter basic salary, HRA and DA of %s : ",name);
scanf("%f%f%f",&bs,&hra,&da);
gs = bs+hra+da;
printf("\nGross salary of %s is : %.2f",name,gs);
getch( );
}
Prog. 13 : WAP to accept the name & basic salary of an employee. Calculate and
display the gross salary of employee acc. to conditions given below :
HRA = 40% of basic salary.
DA = 30% of basic salary.
gs = bs+hra+da
#include<stdio.h>
#include<conio.h>
void main( )
{
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
13
clrscr( );
char name[20];
float bs,hra,da,gs;
printf("Enter name of employee : ");
gets(name);
printf("Enter basic salary of %s : ",name);
scanf("%f",&bs);
hra = bs*((float)40/100);
da = bs*0.3;
gs = bs+hra+da;
printf("\nGross salary of %s is : %.2f",name,gs);
getch( );
}
Prog. 14 : WAP to display the area and perimeter of a rectangle
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int l,b;
float area,perimeter;
printf("Enter length and breadth of rectangle : ");
scanf("%d%d",&l,&b);
area = l*b;
perimeter = 2*(l+b);
printf("\nArea of rectangle is : %.2f",area);
printf("\nPerimeter of rectangle is : %.2f",perimeter);
getch( );
}
Prog. 15 : WAP to display the area and circumference of a circle
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
float rad,area,cmf,pi;
pi = (float)22/7;
printf("Enter radius of circle : ");
scanf("%f",&rad);
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
14
area = pi*rad*rad;
cmf = 2*pi*rad;
printf("\nArea of circle is : %.2f",area);
printf("\nCircumference of circle is : %.2f",cmf);
getch( );
}
Prog. 16 : WAP to accept the marks of a student in 5 different subjects and display
the aggregate and total percentage (M. Imp.)
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int eng,hin,math,sc,comp;
float agg,per;
printf("Enter marks of 5 subjects : ");
scanf("%d%d%d%d%d",&eng,&hin,&math,&sc,&comp);
agg = eng+hin+math+sc+comp;
per = agg/5;
printf("\nAggregate marks are : %.2f",agg);
printf("\nTotal percentage is : %.2f",per);
getch( );
}
Prog. 17 : Mathematical functions (Square root)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
clrscr( );
float num;
printf("Enter number : ");
scanf("%f",&num);
printf("\nSquare root of %.2f is : %.2f",num,sqrt(num));
getch( );
}
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
15
C-Programming
16
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
clrscr( );
float a,b,c,s,area;
printf("Enter three sides of a triangle : ");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea of triangle is : %.2f",area);
getch( );
}
Prog. 21 : WAP to accept the temperature in Fahrenheit and convert it into
Centigrades
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
float f,c;
printf("Enter temperature in Fahrenheit : ");
scanf("%f",&f);
c = (f-32)/1.8;
printf("\nEqu. temperature in Centigrade is : %.2f",c);
getch( );
}
Prog. 22 : WAP to swap two numbers. (M. Imp.)
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,temp;
printf("Enter two numbers a and b : ");
scanf("%d%d",&a,&b);
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)
C-Programming
17
printf("\nBefore swapping\n");
printf("a : %d\tb : %d",a,b);
temp=a;
a=b;
b=temp;
printf("\n\nAfter swapping\n");
printf("a : %d\tb : %d",a,b);
getch();
}
CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720,
9417524702,9463741635)
C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA,
MCA, B.PHARMACY)