BCSE 102 Structured and Object-
Oriented Programming (Th)
Dr. Biji C. L
Associate professor
Department of Analytics
School of Computer Science and Engineering (SCOPE)
Vellore Institute of Technology (VIT), Vellore
Email: [Link]@[Link]
Some Quick Tips: Keep a cheat
sheet for Format Specifier
Some Quick Tips: Keep a cheat
sheet for Format Specifier
scanf ( ):
• function that used to read data from the standard
input device; the keyboard.
• The syntax for the scanf ( ) function is scanf
(“conversion string”, &addr1, &addr2, …);
• “conversion string” contains specifiers: e.g., %d or %f
• &addr1, &addr2, … are memory addresses: e.g., &x or
&y
• The values that the program read from the standard
input device will be stored at these addresses.
• It is an error if the number of addresses not equal to
the number of conversion strings.
scanf ( ):
scanf ( ):
Example Code
Width Specifier in printf()
• %d (print as a decimal integer)
• %6d (print as a decimal integer with a width of at least
6 wide)
• %06d (print as a decimal integer with a width of at
least 6 wide, precedes with 0)
• %f (print as a floating point)
• %4f (print as a floating point with a width of at least 4
wide)
• %.4f (print as a floating point with a precision of four
characters after the decimal point)
• %3.2f (print as a floating point at least 3 wide and a
precision of 2)
Sample Snippets
Sample Snippets
Note: A minus(-) sign tells left alignment.
Why Skipping?
Some Quick Tips: Working with
Character
Is it really skipping?
• It's not skipping input, you already
supplied it. Say you enter 1 for the 1st
number. What you really enter is:
• '1' and '\n'.
• The '1' is read into 'a' and the '\n' remains
in the stream. When calling scanf() a next
time it reads the newline character that
was …
Some Quick Tips: Working with
Character
Operators
Types of Operator
• Arithmetic
• Relational
• Logical
• Bitwise
• Assignment
• Type Conversion
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
a++ (post-increment)
Vs
++a (pre-increment):
Sample Snippet
Result
Relational and Equality Operators
Examples
Memory with Values
Logical Operators
• To form more complicated conditions or
logical expressions
• Three operators:
– And (&&)
– Or (||)
– Not(!)
Logical And
Logical Or
Logical Not
Bitwise Operators
Bitwise Operators
Bitwise Operators (And)
Bitwise Operators (Or)
Bitwise Operators (Left Shift)
Bitwise Operators (Left Shift)
Bitwise Operators (Right Shift)
When 'A‘ = 60 and 'B' =13
ASSIGNMENT OPERATOR
/ Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
Precedence of Operators in C
Associativity of Operators in C
When precedence of two operators are same then
associativity of operator is considered for evaluation
Increment operator
• The increment operator is used to increase the current value of
the variable by 1.
• Represented by the double plus (++) symbol.
Example -1
#include<stdio.h>
int main()
{
int a, b,c;
a = 4;
b = 2;
c = -a+--b;
printf ( "c = %d", c) ;
return 0;
}
A pre-increment operator is used to increment the value of a
variable before using it in a expression.
In the Pre-Increment, value is first incremented and then used
inside the expression.
Output 1
c = -3
Example 2
#include<stdio.h>
int main()
{
int a, b, c;
a = 4;
b = 2;
c = -a+ b--;
printf ( "c = %d", c) ;
printf ( "b = %d", b) ;
return 0;
}
Output 2
c = -2
b=1
Automatic Type Conversion in C
Convert a variable from one data type to another data
type
When the type conversion is performed automatically by
the compiler without programmers intervention, such
type of conversion is known as implicit type
conversion or type promotion.
The compiler converts all operands into the data type of
the largest operand.
Rules for Implicit Type Conversion in C
•All the data types of the variables are upgraded
to the data type of the variable with largest data
type.
• bool -> char -> short int -> int ->
• unsigned int -> long -> unsigned ->
• long long -> float -> double -> long double
•If either of the operand is of type long double, then others
will be converted to long double and result will be long
double.
• Else, if either of the operand is double, then others are
converted to double.
• Else, if either of the operand is float, then others are
converted to float.
Example-Implicit Conversion
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
Output:
x = 107, z = 108.000000
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
• Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
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: Explicit Type Conversion
#include<stdio.h>
int main()
{ double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
Output:
sum = 2
Example 9
#include<stdio.h>
void main()
{
char a = 65;
char b = 100;
int c = a%b;
printf("%c",c);
}
Output 9
A
Example 10
#include<stdio.h>
void main()
{
int a = 165;
int b = 100;
int c = (float)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
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