Lecture Notes by Rashmi Paneru Pandey (Be-C) : Unit - 3 Variables and Data Types
Lecture Notes by Rashmi Paneru Pandey (Be-C) : Unit - 3 Variables and Data Types
UNIT - 3
Variables and Data Types
1. Fundamental of C
Program is a set of instructions to interact with computer. The programs are written in programing language. C is a
procedural programming language developed at AT & T‘s Bell Laboratories of USA in 1972. It was designed and
written by a man named Dennis Ritchie. C program is a sequence of characters that will be converted by the C compiler
into machine code.
2. Character set of C
Character set is a package of valid characters recognized by compiler/interpreter. Each natural language has its own
alphabet and characters set as the basic building. We combine different alphabets to make a word, sentence, paragraph
and a meaningful passage. Similarly each computer languages have their own character set. Particularly, C also has its
own character set, shows in following table:
3. Keywords
Keywords are the predefined reserved (built-in) words whose meaning has already been defined to the c compiler. A
programmer can‘t use the keywords for other task than the task for which it has been defined. For example: a programmer
can‘t use the keyword for variable names.
4. Identifiers
Identifiers are the user defined names and consists of a sequence of letters and digits with digit or
underscore as a first character. It may be name of the variables, functions, arrays, etc. In identifiers we can
use both upper and lower cases.
5. Constants
Constant in C refers to fixed values that do not change during execution of a program. C supports several types of
constants, such as numeric constants and character constants.
5.1Numeric constants:
LECTURE NOTES BY RASHMI PANERU PANDEY(BE-C )
It consists of:
5.1.1 Integer Constants:
It refers to the sequence of digits with no decimal points. The three kinds of integer constants are:
Decimals: Decimal numbers are set of digits from 0 to 9 with leading +ve or –ve sign. For example: 121, -512
etc.
Octals: Octal numbers are any combination of digits from set 0 to 7 with leading 0. For example: 0121, 0375 etc.
Hexadecimals: Hexadecimal numbers are the sequence of digits 0-9 and alphabets a-f (A-F) with preceding 0X or
0x. For example: 0xAB23, 0X787 etc.
6. Delimiters
Delimiters are small system components that separate the various elements (variables, constants, statements) of a
program. They are also known as separators. Some of the delimiters are:
Comma: It is used to separate two or more variables, constants.
Semicolon: It is used to indicate the end of a statement.
Apostrophes (single quote): It is used to indicate character constant.
Double quotes: It is used to indicate a string.
White space: space, tab, new line etc.
7. Variables
A variable is a data name that may be used to store a data value. Unlike constants that remain unchanged during the
execution of a program, a variable may take different values at different times during execution. Constant supplied by the
user is stored in block of memory by the help of variable.
Consider an expression:
2*x + 3*y = 5. In this expression the quantities x and y are variable. A variable is a named location in memory that is
used to hold certain values.
Declaring variable
After defining suitable variable name, we must declare them before used in our program. The declaration deals with
two things:
It tells the compiler what the variable name is.
It specifies what type of data the variable can hold.
Syntax:
Data_type variable_names;
For example:
int account_number;
float a, b, c;
char name[10];
8. Data Types
Data types are used to declare the variables and tell the compiler what types of data the variable will hold. Data type
is useful in C that tells the computer about the type and nature of the value to be stored in a variable. The type of a
variable determines how much space it occupies in storage and how the bit pattern stored is interpreted. Generally the
data types can be categorized as:
9. Operators
C supports a rich set of operators. An operator is a symbol that tells the computer to perform mathematical or logical
operations on operands. Operators are used in programs to manipulate data. C operators can be classified into a number
of categories:
9.1Arithmetic operators:
Arithmetic operators are used for performing most common arithmetic (mathematical) operations. There are generally
five arithmetic operators in C such as addition (+), subtraction (-), multiplication (*), division (/) and modulus division (%).
9.4Relational operators
These operators are used to form relational expressions, which evaluates to either true or false. (True - 1, false - 0). It is
used for comparisons. C supports six relational operators.
Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
9.7Conditional Operator
The ternary operator pair ? and : are used to construct conditional operator. An expression that makes use of conditional
operator is called conditional expression. Its general syntax is:
IfCondition? TRUE case : FALSE case
Example: x=(a>b)?a:b;
y; then the data type of z must be only one. To handle such type of problems, the type conversion and type casting are
introduced. The process of converting one data type into another type is known as type casting. C provides two types of
type conversions:
11.1 Comment:
A comment is a descriptive sentence written in a program. Although a comment is not compulsory in a program
but it is good practice as it makes a program simpler to understand the logic, more readable, and correcting the future
errors. A comments are not executable statements therefore the compilers ignore the comments. Comments in C
can be written in two ways:
11.1.1 Single line comment: The comment that starts with // is known as single line comments.
11.1.2 Multiline comment: The comment that starts with /* and end with */ is known as multiline comment.
Header File in C :
Header file contains different predefined functions, which are required to run the program. All header files should be
included explicitly before main ( ) function. It allows programmers to separate functions of a program into reusable code
or file. Header file has extension like '*.h'. The prototypes of library functions are gathered together into various categories
and stored in header files. Various header files with their functions are:
stdio.h (standard input output header file) used for standard function like
printf() - output function used to print a message or value of a variable on the screen.
LECTURE NOTES BY RASHMI PANERU PANDEY(BE-C )
11.3 main( )
C program may consist of more than one function. If a program consists only one function it must be main () that shows the
beginning of a program. It is not possible to write a program without main () because program execution always starts with
this function. The main ( ) is not followed by comma or semicolon.
The function main ( ) encloses all the statements within a pairs of braces {…..}. Each line written between the braces is
a statement. Every statement is terminated by semicolon. The semicolon at the end of a statement separates one
statement from other. More than one statement can be written on the same line by separating them with individual semi
column. However, it is good practice to write one statement per line.
putchar(ch);
printf(): The library function printf () is used to output the values. This function can be used in variety of form as
described below:
Character that will be printed on the screen as they appeared
Syntax: printf (―control string‖);
Example: printf (―Enter name of student‖);
Format specification that define the output format for display of each item
Syntax: printf (―control string‖, arg1, ..argn);
Example: printf (―%d‖, a);
Escape sequence character such as new line (\n), tab (\t) bell (\b) etc.
Example: printf (―\t‖);
LAB SECTION
getch();
clrscr();
}
6. If a number is input through the keyboard. WAP to find its square root.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int n, z;
clrscr( );
printf(―enter a number‖);
scanf(―%d‖, &n);
z=sqrt(n);
printf(―\nthe square root of %d is %d‖, n, z);
getch( );
}
7. Three quantities principle, time and rate are input through the keyboard. WAP to find simple interest.
#include<stdio.h>
#include<conio.h>
void main( )
{
float p, t, r, si;
clrscr( );
printf(―enter the principle, time and rate‖);
scanf(―%f%f%f‖, &p, &t, &r);
si=(p*t*r)/100;
printf(―\n the simple interest=%f ‖,si);
getch( );
}
8. Two numbers are input through the keyboard. WAP to find addition, subtraction, multiplication,
quotient and remainder after division.
#include<stdio.h>
#include<conio.h>
void main( )
{
int x, y, z1, z2, z3, z4, z5;
clrscr( );
printf(―Enter two numbers‖);
scanf(―%d %d‖, &x, &y);
z1=x+y;
z2=x-y;
z3=x*y;
z4=x/y;
z5=x%y;
printf(―summation=%d, difference=%d, multiplication=%d, quotient=%d and remainder=%d‖, z1, z2, z3, z4, z5);
getch( );
}
9. Temperature of a city in centigrade is input through the keyboard. WAP to convert this temperature into
Fahrenheit degree.
#include<stdio.h>
LECTURE NOTES BY RASHMI PANERU PANDEY(BE-C )
#include<conio.h>
void main( )
{
float c, f;
clrscr( );
printf(―enter the temperature in centigrade‖); scanf(―%f‖, &c);
f=1.8*c+32;
printf(―the temperature in Fahrenheit = %f‖, f);
getch( );
}
10. If the marks obtained by a student in 5 different subjects are input through the keyboard. WAP to find his/her total
and percentage mark.
#include<stdio.h>
#include<conio.h>
void main( )
{
int m1, m2, m3, m4, m5, total, per;
clrscr( );
printf(―enter marks‖);
scanf(―%f%f%f%f%f‖, &m1, &m2, &m3, &m4, &m5);
total=m1+m2+m3+m4+m5;
per=total/5;
printf(―total marks=%f and percentage=%f‖, total, per);
getch( );
}
11. WAP to find the area & perimeter of a rectangle and area & circumference of a circle.
#include<stdio.h>
#include<conio.h>
#include<math.h>/*for function pow( )*/
void main( )
{
float l, b, area1, perimeter, rad, area2, cir;
clrscr( );
printf(―Enter length, breadth of a rectangle:‖);
scanf(―%f%f‖, &l, &b);
printf(―Enter radius of a circle:‖);
scanf(―%f‖, &rad);
area1 = l*b;
perimeter = 2*(l+b);
area2 = 3.14*rad*rad;
cir = 2*3.14*rad;
printf(―\nAarea of rectangle= %f \n‖,area1);
printf(― Perimeter of rectangle = %f‖, perimeter);
printf(―\nArea of circle = %f‖, area2);
printf(―\nCircumference of circle = %f‖, cir);
getch();
}
12. If a 4 digit number is input through the keyboard. WAP to find reverse.
#include<stdio.h>
LECTURE NOTES BY RASHMI PANERU PANDEY(BE-C )
#include<conio.h>
void main( )
{
int n, d1, d2, d3, d4, rev=0;
clrscr( );
printf(―Enter a 4 digit number‖);
scanf(―%d‖, &n);
d4=n%10;
rev=rev+d4*1000;
n=n/10;
d3=n%10;
rev=rev+d3*100;
n=n/10;
d2=n%10;
rev=rev+d2*10;
n=n/10;
d1=n%10;
rev=rev+d1;
printf(―reverse=%d‖, rev);
getch( );
}
Alternate method
#include<stdio.h>
#include<conio.h>
void main( )
{
int d1,d2,d3,d4,n;
printf(―Enter a 4 digit number‖);
scanf(―%d‖, &n);
d4=n%10;
n=n/10;
d3=n%10;
n=n/10;
d2=n%10;
n=n/10;
d1=n%10;
printf(―reverse=%d%d%d%d‖, d4,d3,d2,d1);
getch( );
}
13. Write a Program to find the area of triangle when three sides of a triangle are given.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,s,area;
printf ("\nENTER THE THREE SIDES OF A TRINAGLE:");
scanf ("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt((s* (s-a)*(s-b)*(s-c)));
printf("\n Sides of triangle are: \na=%f\nb=%f\nc=%f",a,b,c);
printf("\n The area of triangle is:%f ",area);
getch();
LECTURE NOTES BY RASHMI PANERU PANDEY(BE-C )