Data Types Expressions C
Data Types Expressions C
1
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
Data Types in C
int :: integer quantity
Some of the basic data types can be
Typically occupies 4 bytes (32 bits) in memory. augmented by using certain data type
qualifiers:
• short
char :: single character • long
• signed
Typically occupies 1 byte (8 bits) in memory. • unsigned
Typical examples:
float :: floating-point number (a number with a decimal point) • short int
• long int
Typically occupies 4 bytes (32 bits) in memory. • unsigned int
• unsigned char
Constants
Numeric Character
Constants Constants
4
Single Character and String Constants
An example:
char cvar = ‘A’;
printf (“%c %d”, cvar, cvar); /* Print the same value twice, once as character, second time as integer */
Variable values and variable addresses
In C terminology, in an expression
speed (a variable name) refers to the contents of the memory location where the variable is stored.
&speed refers to the address of the memory location where the variable is stored.
Examples:
printf (“%f %f %f”, speed, time, distance); /* We need only the values of the vars to print them */
scanf (“%f %f”, &speed, &time); /* We need the address of the vars to store the values read */
Assignment Statement
int a;
a = 2*3.2;
• Type of r-value is float/double and the value is 6.4
• Type of l-value is int, so internally converted to 6
• So a stores 6, and not 6.4
9
Operators in Expressions
Assignment
Operators Operators
x+y 18
Examples:
x–y 8
distance = rate * time ;
x*y 65
netIncome = income - tax ;
speed = distance / time ; x/y 2
area = PI * radius * radius; x%y 3
y = a * x * x + b*x + c;
quotient = dividend / divisor;
remainder = dividend % divisor;
Operator Precedence of Arithmetic operators
x*y*z 🡪 ((x * y) * z)
Parenthesis may be used to change the precedence of
operator evaluation.
a + b + c * d * e 🡪 (a + b) + ((c * d) * e)
Integer, Real, and Mixed-mode Arithmetic
14
Solution: Typecasting
15
Restrictions on typecasting
16
Example: Finding Average of 2 Integers
int a, b;
float avg;
scanf(“%d%d”, &a, &b);
avg = ((float) (a + b))/2;
Wrong program !! Why?
printf(“%f\n”, avg);
int a, b;
float avg;
Correct programs
scanf(“%d%d”, &a, &b);
avg = (a + b)/2;
int a, b;
printf(“%f\n”, avg);
float avg;
scanf(“%d%d”, &a, &b);
avg = (a + b) / 2.0;
printf(“%f\n”, avg);
17
More Assignment Operators
18
Increment (++) and Decrement (--) operators
There are three logical operators in C (also Unary negation operator (!)
called logical connectives). • Single operand
! : Unary negation (NOT) • Value is 0 if operand is non-zero
&& : Logical AND • Value is 1 if operand is 0
| | : Logical OR
Example: ! (grade == ‘A’)
What do these operators do?
• They act upon operands that are
themselves logical expressions.
• The individual logical expressions get
combined into more complex conditions
that are true or false.
Logical Operators
Suppose we wish to express that a should not have the value of 2 or 3. Does the following expression capture this
requirement?
(( a != 2) || ( a != 3))
24
Example: AND and OR
Output
#include <stdio.h>
int main () 30
3 AND 0 = 0, 3 OR 0 = 1
{
int i, j;
scanf(“%d%d”, &i, &j);
printf ( “%d AND %d = %d, %d OR %d=%d\n”, i, j, i&&j, i, j, i||j ) ;
return 0;
25
Precedence among different operators
(there are many other operators in C, Operator Class Operators Associativity
some of which we will see later)
Unary postfix ++, -- Left to Right
prefix ++, --
Unary Right to Left
─ ! &
Binary * / % Left to Right
Binary + ─ Left to Right
26
Expression evaluation
An assignment expression evaluates to a value
Value of an assignment expression is the value assigned to the l-value
Example: value of
• a = 3 is 3
• b = 2*4 – 6 is 2
• n = 2*u + 3*v – w is whatever the arithmetic expression 2*u + 3*v – w evaluates to given the current values
stored in variables u, v, w
Consider a = b = c = 5
• Three assignment operators
• Rightmost assignment expression is c=5, evaluates to value 5
• Now you have a = b = 5
• Rightmost assignment expression is b=5, evaluates to value 5
• Now you have a = 5
• Evaluates to value 5
• So all three variables store 5, the final value the assignment expression evaluates to is 5
27
A more non-trivial example:
a = 3 && (b = 4)
• b = 4 is an assignment expression, evaluates to 4
• && has higher precedence than =
• 3 && (b = 4) evaluates to true as both operands of && are non-0, so final value of
the logical expression is true
• a = 3 && (b = 4) is an assignment expression, evaluates to 1 (true)
28
Statements and Blocks
x = 5;
i++;
printf (“The sum is %d\n”, sum”) ;
Braces { and } are used to group declarations and statements together into a compound statement, or block.
{
sum = sum + count;
count++;
printf (“sum = %d\n”, sum) ;
}
Doing More Complex Mathematical Operations
• Example
printf ("%f", sqrt(900.0));
• Calls function sqrt, which returns the square root of its argument
30
Math Library Functions
31
Math Library Functions
32
Computing distance between two points
Output
#include <stdio.h>
Enter coordinates of first point: 3 4
#include <math.h>
Enter coordinates of second point: 2 7
int main()
Distance = 3.162278
{
int x1, y1, x2, y2;
double dist;
printf(“Enter coordinates of first point: “);
scanf(“%d%d”, &x1, &y1);
printf(“Enter coordinates of second point: “);
scanf(“%d%d”, &x2, &y2);
dist = sqrt(pow(x1 – x2, 2) + pow(y1 – y2, 2));
printf(“Distance = %lf\n”, dist);
return 0;
}
33
Practice Problems
34