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

Module 2_PPT_P1

The document covers the principles of programming using C, focusing on basic data types, type conversion, operators, and expressions. It explains various data types such as int, float, double, and char, along with signed and unsigned qualifiers. Additionally, it details operators including arithmetic, relational, logical, bitwise, and assignment operators, as well as the concept of operator precedence and associativity.

Uploaded by

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

Module 2_PPT_P1

The document covers the principles of programming using C, focusing on basic data types, type conversion, operators, and expressions. It explains various data types such as int, float, double, and char, along with signed and unsigned qualifiers. Additionally, it details operators including arithmetic, relational, logical, bitwise, and assignment operators, as well as the concept of operator precedence and associativity.

Uploaded by

anil-csbs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

BPOP103/203

Principles Of Programming using C

Module 2

Prof.Rajeshwari R, Assistant Professor


Dept. of CSE, CMRIT, Bangalore
 Basic Data Types
int

Integers are whole numbers that can have both zero, positive and negative values
but no decimal values. For example, 0, -5, 10
We can use int for declaring an integer variable.
int id=8; //valid
int id=1.8; //invalid

float and double


float and double are used to hold real numbers.
float salary=10.1;//valid
double price;
char
Keyword char is used for declaring character type variables. For example,
char test = ‘10’; // invalid
char test = ‘1’; // valid
char test = 10; // invalid
signed and unsigned

There are 2 types of qualifiers :

Sign qualifier- signed & unsigned


Size qualifier- short & long

• signed - allows for storage of both positive and negative numbers


• unsigned - allows for storage of only positive numbers

short- size of data is less / occupied memory space is less


long- size of data is more/ occupied memory space is more
signed int a=10; //valid
signed int a=-10; //valid
unsigned int a=10; //valid
unsigned int a=-10; //invalid
void

void is an incomplete type. It means "nothing" or "no type". You can


think of void as absent. For example, if a function is not returning
anything, its return type should be void.

Note that, you cannot create variables of void type.

void vname; //invalid

void function-name(); //valid


Type Conversion in C

A type cast is basically a conversion from one type to


another. There are two types of type conversion:

• Implicit Type Conversion


• Explicit Type Conversion
Type Conversion in C

int a=10;
float b=12.3;
char c=‘d’;

a=b; // value 12 will be stored inside variable ‘a’. Data loss is there.
b=a; // value 10.0 will be stored inside variable ‘b’

a=c; //ASCII value of ‘d’ will be assigned to the variable ‘a’


1) Implicit Type Conversion
(Lower to Higher Datatype Conversion)

• Also known as ‘automatic type


conversion’.
• Done by the compiler on its own, without any
external trigger from the user.
• Generally, takes place when in an expression
more than one data type is present. In such
condition type conversion (type promotion)
takes place to avoid loss of data.
• All the data types of the variables are
upgraded to the data type of the variable with
largest data type.
// An example of implicit conversion (lower to higher datatype)
#include<stdio.h>
int main()
{
int x = 10; // integer x
Output:
char y = 'a'; // character c
float z; //floating point z
x = x + y; //10+97 =107 x = 107, z = 108.2
z = x + 1.2; //z=107.0+1.2= 108.2
printf("x = %d, z = %f", x, z);
return 0;

}
2) Explicit Type Conversion (type casting)
(Higher to Lower Datatype Conversion)

This process is also called type casting and it


is user defined. Here the user can type cast
the result to make it of a particular data type.

The syntax in C:

(type) expression

Type indicated the data type to which the final


result is converted.
// C program to demonstrate explicit type casting (higher to lower datatype)

#include<stdio.h>
int main()
{

double fD = 24.1;
float sF=22.2;
int i=(int)fD;
float f = (float)fD;
int i2=(int)sF;
printf("explicit conversion of double into integer is=> %d\n",i);
printf("explicit conversion of double into float is=> %f\n",f);
printf("explicit conversion of float into int is=> %i\n",i2);
printf("%lf\n",fD);

return 0;
}
// C program to demonstrate explicit type casting (higher to lower datatype)
// C program to demonstrate explicit type casting (higher to lower datatype)

#include <stdio.h>
int main() {
float c = 5.55;
int s = (int)c+1;
printf("%d",s);
return 0;
}
Output
6
// C program to demonstrate explicit type casting (higher to lower datatype)
Operators and Expressions

C language is rich in built-in operators and provides the following types of


operators:

• Arithmetic Operators +, -, *, /, %, ++, --


• Relational Operators >, <, ==, !=, >=, <=
• Logical Operators &&, ||, !
• Bitwise Operators &, |, ^, >>, <<
• Assignment Operators =, +=, -=, *=, /=
• Increment and decrement operators ++, --

• Conditional operators ?: (Expression1) ?(Expression2):(Expression3)


• Miscellaneous Operators sizeof(), &, *
Arithmetic operator:

These are used to perform mathematical calculations like addition,


subtraction, multiplication, division and modulus.
Increment & Decrement Operators
#include<stdio.h>
int main()
Let a=10 {
a++; // 1st a=10, then a=11 int a=3;
a--; // 1st a=11, then a=10 printf(“ value of a=%d”, a--);
++a; // 1st a=11, then a=11 //a=3
//post increment. The operator will first
--a; // 1st a=10, then a=10 display the value of ‘a’ as 3 then it will
decrement the value of ‘a’ to 2.
printf(“ value of a=%d”, ++a);
//pre increment. The operator will first
increment the value of ‘a’ to 3 and then will
display the value of ‘a’ as 3.
return 0;
}
Relational Operators:

These operators are used to compare the value of two


variables.
Following table shows all the relational operators supported
by C language.

Assume variable A holds 10 and variable B holds 20, then:


True=1, False=0
a=10, b=20
printf(“%d”, 12+a<b);
// 12+10<20
//22<20 // false=0
Output: 0
Logical Operators:

These operators are used to perform logical operations on the given


two variables. Following table shows all the logical operators
supported by C language. Assume variable A holds 1 and variable B
holds 0, then:
Logical Operators

The truth tables (0- false, 1- true)

p q p && q p || q p != q
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operation. Bitwise


operators are used in bit level programming. These operators can
operate upon int and char but not on float and double.

showbits( ) function can be used to display the binary representation of


any integer or character value.

Bit wise operators in C language are:

& (bitwise AND),


| (bitwise OR),
~ (bitwise OR),
^ (XOR),
<< (left shift)
>> (right shift).
Bitwise Operators

The truth tables for &, |, and ^ are as follows:

p q p&q p|q p^q


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
The Bitwise operators supported by C language are explained in the following table.
Assume variable A holds 60 (00111100) and variable B holds 13 (00001101), then:
01001001
1+8+64=73
Count from right most bit.
1st right most bit=1
2nd right bit=2
3rd right bit=4
4th right bit=8
5th right bit=16
6th right bit=32
7th right bit=64
8th right bit=128
Class work: Assume variable A holds 72 and
variable B holds 60 . perform bit wise AND, OR,
XOR operation and show the result in numeric
Ans:
A=72= 1001000
B=60= 0111100
(&) = 0001000 = 8
(|) = 1111100 =252
(^) = 1110100 = 116
Binary Left shift(<< ) Operator
A=60

Binary representation: 00111100

A= A<<1

After (A<<1) Binary representation: 01111000

A=8+16+32+64=120

Ans: A=120 // (A*2)


Binary Right shift(>>) Operator

A=60

Binary representation: 00111100

A= A>>1

After (A>>1) Binary representation: 00011110

A=2+4+8+16=30

Ans: A=30 // (A/2)


Assignment Operators: (=)

In C programs, values for the variables are assigned using assignment


operators.

There are following assignment operators supported by C language:


Shorthand assignment operators
a=10, b=20

a+=b // a=a+b // a=10+20=30

a-=b // a=a-b // a=10-20=-10

a*=b // a=a*b // a=10*20=200

a/=b // a=a/b // a=10/20=0

a%=b // a=a%b // a=10%20=10

a<<=2 //a=a<<2

a>>=2 //a=a>>2
Increment and Decrement operators

In C, ++ and – are called increment and decrement operators respectively.


Both of these operators are unary operators, i.e, used on single operand.
++ adds 1 to operand and – subtracts 1 to operand respectively. For
example:

Let a=5
a++;
a--;
++a;
--a;
When i++ is used as prefix(like: ++var), ++var will increment the value
of var and then return it but, if ++ is used as postfix(like: var++),
operator will return the value of operand first and then only increment
it. This can be demonstrated by an example:

#include <stdio.h>
int main()
{
int c=2; Output
printf(“%d\n”,c++); 2
Printf(“%d”,++c);
return 0; 4
}
Conditional Operators (? :) / Ternary operator
Conditional operators are used in decision making in C programming , i.e,
executes different statements according to test condition whether it is either true or
false.

Syntax of conditional operators

(Conditional expression) ? (expression1) : (expression2)

If the conditional expression is evaluated to true , then expression1 is executed.


If the conditional expression is evaluated to false , then expression2 is executed
Or (using if-else)
#include<stdio.h>
int main() #include<stdio.h>
{ int main()
int x, y ; {
printf(“ enter the value for x”); int x, y ;
// x=10 printf(“ enter the value for x”);
scanf ( “%d”, &x ) ; // x=10
scanf ( “%d”, &x ) ;
y = ( x> 5 ? 3 : 4 ) ; //y=3 if ( x > 5 )
//This statement will store 3 in y if y=3;
x is greater than 5, otherwise it else
will store 4 in y. y=4;
printf(“y=%d”, y); printf(“y=%d”, y);
return 0; return 0;
} }
Miscellaneous Operators:

There are few other operators supported by c language.

Sizeof is a much used operator in the C or C++. It is a compile time unary operator
which can be used to compute the size of its operand. The result of sizeof is of
unsigned integral type which is usually denoted by size_t.
// The sizeof(variable)operator computes the size of a variable. And, to print the
result returned by sizeof, we use %lu format specifier. l= long integer, u= unsigned

Syntax: sizeof(variable_name) // size of the variable_name


will be displayed
#include<stdio.h>
int main() {
int i;
float f;
double d;
char c;

// sizeof evaluates the size of a variable


printf("Size of int: %lu bytes\n", sizeof(i)); // Size of int: 4 bytes
printf("Size of float: %lu bytes\n", sizeof(f)); // Size of float: 4 bytes
printf("Size of double: %lu bytes\n", sizeof(d)); // Size of double: 8 bytes
printf("Size of char: %lu byte\n", sizeof(c)); // // Size of char: 1 byte
return 0;
}
1) Display ASCII value of given
chaíacteí
int a;
chaí c=‘x’;
a=c; // chaíacteí ‘x’ is conveíted to ASCII value & stoíed in ‘a’
píintf(“ASCII value of %c=%d”, c, a); // ASCII value of
x=123

2) Conveít Fahíenheit to Celsius


float fahíenheit, celsius;
píintf("\nEnteí tempeíatuíe in Fahíenheit:");
scanf("%f", &fahíenheit);
celsius=(fahíenheit - 32)*5/9;
píintf("\nCelsius = %.3f",celsius);
Conversion of Mathematical Expression into C Equivalent Expression

Mathematical Expression C Expression


Conversion of Mathematical Expression into C Equivalent Expression

Mathematical Expression C Expression

sqrt(s*(s-a)*(s-b)*(s-c))

(-b+sqrt(b*b-4*a*c))/(2*a)
Conversion of Mathematical Expression into C Equivalent Expression

Mathematical Expression C Expression

a.) 𝑥
+ 𝑦 (x/(b+c))+(y/(b-c))
𝑏+ 𝑏−

b.) 𝑎 + 𝑏(𝑎𝑑+𝑒)
− 𝑐 a+((b*(a*d+e))/(b-a))-c/d
𝑏 −𝑎 𝑑
Operators Precedence & Associativity in C

Operator precedence determines the grouping of terms in an


expression. This affects how an expression is evaluated. Certain
operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition
operator.

Here, operators with the highest precedence appear at the top of the
table, those with the lowest appear at the bottom. Within an
expression, higher precedence operators will be evaluated first.
Operators examples
Relational Expression Description
This condition is used to check whether the x is
x%2 = = 0
an even number or not. The relational
x=7, result=false=0 expression results in value 1(true) if x is an
even number otherwise results in value
0(false).

It is used to check whether a is not equal to b.


a!=b This relational expression results in 1 if a is not
a=10, b=20, result=true=1 equal to b otherwise 0.

It is used to check whether the expression


a+b = = x+y "a+b" is equal to the expression "x+y".
a=1, b=2, x=2, y=3. result=false=0
It is used to check whether the value of a is
a>=9
greater than or equal to 9.
a=9, Result=true=1

You might also like