0% found this document useful (0 votes)
31 views

3 - Intro To C - Constants Var Datatypes

Uploaded by

shreeumaluti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

3 - Intro To C - Constants Var Datatypes

Uploaded by

shreeumaluti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Module-1, Chapter-1

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

High Level Low Level


Languages Languages

General Specific Machine Assembly


Purpose Purpose Level Level
Ex: C, Ex: Oracle
Python
C basics
C history
 C was developed in early 70’s by Dennis M Ritchie
 1960s, ALGOL
 1967, Martin Richards, BCPL
 1970, Ken Thomson, B
 1972, (ALGOL + BCPL + B)  C (Dinnis Ritchie –
Father of C, Bell labs, US)
 1978, K & R (Brian Kernighan), “The C Prog. lang.”
 1989, ANSI C
 1990, ANSI / ISO
Characteristics of C
 HLL, machine and platform independent(portable)
 small size, 32 keywords
 makes extensive use of function calls
 one of most popular languages(C++, Java, Python, Oracle)
 fast, flexible, and elegantly structured
 stable, not been revised since 1983
 Combines features of HLL with elements of assembler(LLL)
 Suitable for both systems and application programming
 Most widely used general purpose language in OSs and
embedded systems development
Our first C Program
Area and perimeter of /*C prog to find area and
rectangle: peri of rect*/
Write a C program to compute area(a) #include<stdio.h>
and perimeter(p) of rectangle for input
int main()
values of length(l) and breadth(b)
{
Algorithm: int l, b, a, p;
Step-1: Read length(l) and printf(“Enter len and br\n”);
breadth(b) scanf(“%d%d”,&l,&b);
Step-2: Compute area a = l * b;
and perimeter: p = 2 * l + 2 * b;
a = l × b, p = 2 × l + 2 × b print(“Area=%d”, a);
step-3: print area and print(“Peri=%d”, p);
perimeter }
Simple Programs
Write C program
1) To compute sum and product of 2 numbers
2) To print name and branch of a student
3) To compute the bill amount for N pens
4) To find simple interest for input values of principle amount(p), interest
rate(r), time(t)
5) To exchange contents of 2 variables
6) To find distance travelled by a vehicle for input values of u(initial vel),
t(time) and a(accn)
7) To find the stress in a circular rod when its area of cross section and
force applied is given
8) To Convert days to years, months and days (1000 days = __ years,
__mths, __ days)
9) To Convert temp from °C to ° F and Kelvin scales (100 °C = __ °F = __
°K) [ Formula: C=(F=32)/1.8 ]
10) To Convert length from m to km and mm (230m = __ km = __ mm)
/*To compute sum and product of 2 numbers*/
#include<stdio.h>
int main()
{
float x, y, sum, prod;
printf("C prog to compute sum and product of 2 nos \n");
printf(“Enter 2 numbers:\n”);
scanf("%f%f",&x,&y);
sum = x + y;
prod = x * y;
printf("Sum=%f\n", sum);
printf("Product=%f", prod);
}
/*sum and product of 2 nos*/
#include<stdio.h>
int main()
{
float x, y, sum, prod;
printf("C prog to compute sum
and product of 2 nos \n");
printf(“Enter 2 numbers:\n”);
scanf("%f%f",&x,&y);
sum = x + y;
prod = x * y;
printf("Sum=%f\n", sum);
printf("Product=%f", prod);
}
/*To interchange values of 2
variables*/
#include<stdio.h>
int main()
{
int x, y, temp;
printf("C prog to swap contents of 2
variables:\n");
printf(“Enter values of x and y:\n");
scanf("%d%d",&x,&y);
printf("Before exchange: x = %d, y= %d
\n",x,y);
/*Exchange process*/
temp = x;
x = y;
y = temp;
printf("After exchange: x = %d, y= %d
\n",x,y);
}
To find the stress in a circular rod when an axial force
F is applied on it
To find the stress in a circular rod when an axial
force F is applied on it
Example:
Program to compute stress in a rod
/*C prog compute stress in a rod*/
#include<stdio.h>
int main()
{
float f, r, a, s;
printf(“Enter radius of rod and force applied\n”);
scanf(“%f%f”,&r,&f);
a = 3.142 * r * r;
s = f / a;
print(“Stress in the rod = %f”, s);
}
To find deformation in a rod
subjected to a force F
Write a C program to compute the elongation of a
rod when an axial tensile force F is applied on it
Basic structure of C
Program
Basic structure of C Program
Documentation Section
Link Section /*C prog to find area and
Definition Section peri of rect*/
Global Declaration Section #include<stdio.h>
int main()
main() {
{ int l, b, a, p;
Declarations;
printf(“Enter len and br\n”);
Executable Statements;
scanf(“%d%d”,&l,&b);
}
a = l * b;
User Defined Function() p = 2 * l + 2 * b;
{ print(“Area=%d”, a);
Declarations; print(“Peri=%d”, p);
Executable Statements; }
}
/*C pgm to add two nos*/
Basic structure of C Program #include<stdio.h>
int main()
Documentation Section {
Link Section int x, y, sum;
Definition Section printf(“Enter two nos\n”);
Global Declaration Section scanf(“%d%d”,&x,&y);
sum=x+y;
main() printf(“Sum of nos = %d”,sum);
{ }
Declarations;
Executable Statements;
}

User Defined Function()


{
Declarations;
Executable Statements;
}
Character Set, C Tokens
C Character Set
Upper case letters: A to Z
Lower case letters: a to z
Digits: 0 to 9
Special Chars: , . ; : ? ‘ “ ! / \~ _ $ % & ^ * - + < > ( ) [ ] { } #
White spaces:
Blank space
Horizontal
Carriage return
New line
Form feed
C Tokens
1. Keywords
2. Identifiers
3. Constants
4. Operators
5. Strings
6. Special symbols
Keywords (Reserved Words)
• Special words that have fixed meaning
• Must be written in lowercase
• Can’t be used as variable names
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers
Names of various entities in a program(vars, fns, arrays)
Both upper/lower case permitted. (convention: lower)
_ is used to link two words in long identifiers (rate_of_int)
Rules:
1. First char must be an alphabet (or underscore)
2. Must consist of only letters, digits or underscore
3. Upper and lower case are significant. (sum, Sum, SUM)
4. Only first 31 char are significant
5. Can’t use a keyword
6. Must not contain white space
Constants

Constants, Variables and data types


Constants
Fixed values that do not change their value in a program.
Types:
Decimal
Numeric Integer Octal
Hexadecimal
Real Scientific
Constants
Exponential
Single
Character char
String
Numeric Constants
Spaces, commas, non-digit characters not permitted

Integer Constants: Sequence of digits (+ or -)


a) Decimal(10): 0 to 9
b) Octal(8): 0 to 7 (Leading 0) Ex: 045, 0327
c) Hexadecimal(16): 0 to 9, A to F (Preceded by 0x or 0X)
Ex: 0x176, 0X5B
Real Constants(Floating point constants):
Numbers containing factional part
a) Decimal Notation: whole no . fractional part
Ex: 2.7, -5.22, .6, -.9
b) Scientific Notation: mantissa e exponent
Ex: 3e2, -5E5, 5e-5, -1.2E-4
Character Constants
Single Character Constants:
Single character enclosed in single quote marks
Ex: ‘3’, ’a’, ’ ’, ‘?’
Note: These have integer values known as ASCII values

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)

Floating point type


float(32) Void
double(64) Used to specify type of UDFs
Long double(80)
Size and range of Data Types(16 bit m/c)
Type Size(Bits) Range
char or signed char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to +32767
unsigned int 16 0 to 65535
short int or signed short int 8 -127 to 127
unsigned short int 8 0 to 255
long int or signed long int 32 -2,14,74,83,648 to 2147483648

unsigned long int 32 0 to 4,29,49,67,295


float 32 3.4E-38 to 3.4E+38
double 64 1.7E-308 to 1.7E+308
long double 80 3.4E-4932 to 1.1E4932
Defining Symbolic Constants
#define is a preprocessor directive (#include)
#define NAME const_value
Ex: #define G 9.81
#define strength 60
• After definition, symbolic name should not be
assigned any other value in the program
• Symbolic names are not declared for data types. Its
type depends on type of constant.
• #define statements may appear anywhere in the
program but before it is referenced in the program.
Lab Programs
L1) C program to find mechanical energy of
a particle using e = mgh+1/2 mv2.
Example
• A child is sitting on a sled at the top of a 17 m
hill. The combined weight of the child and the
sled is 42 kg, and the child slides down the hill
with an initial velocity of 18.263 m/s.
Determine the total mechanical energy?

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

You might also like