3 - Intro To C - Constants Var Datatypes
3 - Intro To C - Constants Var Datatypes
Introduction to C
Introduction to C, Structure of C program, Files
used in a C program, Compilers, Compiling and
executing C programs, variables, constants,
Input/output statements in C
Programming
Languages
Programming Languages
String Constants:
Sequence of characters enclosed in double quotes.
Ex: “work is worship”, “2000”, ”x+5”, “ “, “?*”
Don’t have equivalent integer value
Problems for practice
1. Develop a C program to read the student details like Name,
USN, and Marks in three subjects. Display the student details,
total marks and percentage with suitable messages.
2. Develop C program to receive Cartesian coordinates (x, y) of a
point and convert them into polar coordinates (r, Ⴔ)
2 2 -1
[Hint: r = x +y , Ⴔ = tan (y/x)]
3. Develop C program to compute Gross salary of an employee
when the basic salary is input by the user adding a dearness
allowance of 40% and house rent allowance of 20%.
4. Size of a plot is input in square feet. Write a C program to
compute its equivalent in square meter.
5.
Write a C program to calculate total expenditure for
the following work:
It is required to setup circular lawn of radius R at the
center of a rectangular field of size L x B. Setting of
lawn costs Rs. 100/ square unit. The remaining area is
to be tiled which costs Rs 150/ square unit and also it is
required to fence the field which costs Rs. 55/ unit
length.
/*C prog to find total expenditure, lawn, tiles*/
#include<stdio.h>
int main()
{
float l, b, r, ar, ac, p, cost;
printf("C prog to find total expenditure of lawn and tiles:\n");
printf("Enter len and br of rect\n");
scanf("%f%f",&l,&b);
ar = l * b;
ac = 3.142 *r*r;
p = 2 * l + 2 * b;
cost = (ar - ac)*150 + ac*100 + p*55;
printf("Total expenditure = %f\n", cost);
}
Problems for practice
Write a program to calculate travel cost per person if the following
data is input:
a. Number of persons travelling
b. Vehicle Mileage
c. Fuel cost per litre
d. distance travelled by vehicle
Variables
A data name that may be used to store a data value.
May take different values during the execution of prgm
• Must be declared before used
• Must be used once declared
Declaration: data-type v1, v2, v3…., vn;
Ex: int x, y; float a, b, c;
Purpose:
i. Tells compiler what the variable name is
ii. Specifies the type of data it will hold
Declaration + Initialization:
int x = 10, y;
Data types
C is rich in its data types
ANSI C supports 3 classes of data:
1. Primary (fundamental) data types
2. Derived data types: arrays, functions, pointers
3. Used defined data types: structures, unions, DSs
Primary Data Types
Integral Type
Integer Char(8)
signed unsigned Signed
Int(16) unsigned
short int(8)
long int(32)
TME=KE+PE=(1/2)mv2 + mgh
= (½) (42 kg) (18.263 m/s)2 + (42 kg) (9.81 m/s2) (17 m)
≈ 004 J + 7004 JE ≈ 14,008 J
The total mechanical energy for this system is equal to 14,008 Joule
program to find mechanical energy of a particle
/*C pgm to find mechanical energy of a particle using e = mgh+1/2 mv2*/
#include<stdio.h>
main()
{
float m, h, v, e;
printf("C program to compute mechanical energy of a particle\n");
/*Accepting input*/
printf("Enter mass(kg), height(m) and velocity(m/s) of particle\n");
scanf("%f%f%f",&m,&h,&v);
/*Calculating mechanical energy*/
e=m*9.81*h+0.5*m*v*v;
/*printing the result*/
printf("Mechanical energy of a particle=%0.2f\n",e);
}
L2) C program to convert km into meters and centimeters
/*C pgm to convert kilometers into meters and centimeters*/
#include<stdio.h>
main()
{
float km, m, c;
printf("C pgm to convert km into meter and centimeters:\n");
/*Accepting input*/
printf("Enter distance in kilometers:\n");
scanf("%f",&km);
/*Converting to metre and centimetre*/
m=km*1000;
c = km*100000;
/*printing the result*/
printf("In metre = %0.2f m\n",m);
printf("In centimetre = %0.2f cm",c);
}
Files used in a C program
Compilers
Compiling and executing C programs
Introduction to C:
• Structure of C program,
• Files used in a C program,
• Compilers,
• Compiling and executing C programs
• Variables, Constants,
• Input / Output statements in C