Session 1 - 2
Session 1 - 2
-2147483648
Real Constants
• Two forms
• Fractional Form
• Exponential Form
Rules for Fractional Form Real Constants
• Must have at least one digit
• Must have a decimal point
• Could be either positive or negative
• Default sign is positive
• No commas or blanks are allowed within a real
constant
• Eg: +325.34, 426.0 , -32.76, -48.5792
Exponential Form of Real Constants
}
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--;
c = ++a + a++;
}
Output 6
c = 10 a = 6
Example 7
#include<stdio.h>
main()
{
int a, b,c;
a = 4;
c = ++a + ++a;
}
Output 7
c = 12 a = 6
Example 8
#include<stdio.h>
main()
{
int a, b,c;
a = 4;
c = a++ + ++a;
}
Output 8
c = 10 a = 6
Associativity of Operators in C
When precedence of two operators are same then
associativity of operator is considered for evaluation
Partial C code for Isogram Problem
/* Code to get the number of letters from user and print it*/
#include<stdio.h>
int main()
{
int num_Of_Letters; // Declaration of variable is mandatory in C
int num_Of_Words ; // Memory is allocated but not initialized
scanf(“%d”,&num_Of_Letters);
//If num_Of_Letters in less than or equal to zero then error
printf(“Number of letters is %d”,num_Of_Letters);
// multiply all numbers from n to 1 to find number of
// number of isogram words that can be formed
Compile and Run
}
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 12
#include<stdio.h>
void main()
{
int a = 165;
int b = 100;
float c = a/b;
printf("%f",c);
}
Output 12
- 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
Scope and Lifetime of a Variable in C Language
Global scope :
When variable is defined outside all functions.
It is then available to all the functions of the program and
all the blocks program contains.
Local scope :
When variable is defined inside a function or a block, then
it is locally accessible within the block and hence it is a
local variable.
Global vs Local Scope
int global = 100; // global variable declared
void main()
{
int local = 10; // local variable declared
printf("Global variable is %d",global);
printf("Local variable is %d",local);
func1();
}
void func1()
{
printf("Global inside func1 is %d",global);
Storage Classes in C
Four Storage Classes, classified based on life and scope of access: