Revision C - Intro
Revision C - Intro
Revision C - Intro
Compiler Interpreter
Takes entire program as Takes instruction by
input and generate a output instruction as input and
file with object code gives an output. But does
not generate a file
Errors are displayed after Errors are displayed for
entire program is checked every instruction
interpreted (if any)
Variable Declaration in C
• In C, it is mandatory to do variable declaration
• We say variable's type, whether it is an integer
(int), floating-point number (float), character
(char) etc
• Syntax is type of variable, white space, name
of variable semicolon
• Eg: int number;
White spaces and Indentation
• No problem of difference between white space and
tab in C
• Block of code in C need not be indented as in Python
• In C, Curly braces are used for giving a block of code
Eg: Block of code in ‘C’
{
-----
}
Problem
• Little Bob loves chocolate, and he goes to a
store with Rs. N in his pocket. The price of
each chocolate is Rs. C. The store offers a
discount: for every M wrappers he gives to the
store, he gets one chocolate for free. This offer
is available only once. How many chocolates
does Bob get to eat?
PAC For Chocolate Problem
Input Processing Output
}
Output 2
warning: overflow in implicit constant conversion [-
Woverflow]
17
256 is 0
257 is 1 and so on
Example 3
#include<stdio.h>
void main()
{
char a = 27;
char b = 25;
char c = a -b;
printf("%c",c);
}
Output 3
A special character
Arithmetic Operators in C
Operator Description
+ Adds two operands
− Subtracts second operand from the first.
∗ Multiplies both operands
∕ Divides numerator by denominator
% Modulus Operator and remainder of after
an integer division.
++ Increment operator increases the integer
value by one
-- Decrement operator decreases the integer
value by one
Precedence of Operators in C
++, -- Post increment Operators
++, -- Pre increment Operators
c = -a+--b;
c = -a+ b--;
}
Output 9
A
Example 10
#include<stdio.h>
void main()
{
int a = 165;
int b = 100;
int c = a/b;
printf("%d",c);
}
Output 10
1
Example 11
#include<stdio.h>
void main()
{
int a = 165;
int b = 100;
float c = a/b;
printf("%f",c);
}
Output 11
1.000000
Example 13
#include<stdio.h>
void main()
{
int a = 165;
float b = 100;
float c = a/b;
printf("%f",c);
}
Output 13
1.650000
Explicit Type Conversion
Type conversion performed by the programmer is known
as explicit type conversion
Explicit type conversion is also known as type casting.
Type casting in c is done in the following form:
(data_type)expression;
where, data_type is any valid c data type,
and expression may be constant, variable or an expression
For example, x=(int)a+b*d;
Explicit Type Conversion
The following rules have to be followed while converting
the expression from one type to another to avoid the loss
of information:
• All integer types to be converted to float.
• All float types to be converted to double.
• All character types to be converted to integer.
Example 14
#include<stdio.h>
void main()
{
int a = 165;
int b = 100;
float c = (float)(a/b);
printf("%f",c);
}
Output 14
1.000000
Example 15
#include<stdio.h>
void main()
{
int a = 165;
int b = 100;
float c = (float) a/b;
printf("%f",c);
}
Output 15
1.650000
Assignment
• Students have to check all operators for all types of
operands, also work on to understand precedence of
operators
Declaration and Definition of Variables in C
• Declaration – Information to compiler about the
variable
• Definition – Memory is allocated for the variable
according to type
• For all storage classes declaration and definition are
same except for extern
• When a keyword extern precedes, its only a
declaration and the compiler waits for definition
without raising error
Problem
ABC company Ltd. is interested to computerize the pay calculation of their
employee in the form of Basic Pay, Dearness Allowance (DA) and House Rent
Allowance (HRA). DA and HRA are calculated as certain % of Basic pay(For
example, DA is 80% of Basic Pay, and HRA is 30% of Basic pay). They have the
deduction in the salary as PF which is 12% of Basic pay. Propose a
computerized solution for the above said problem.