Unit-1
ANSI C
Topics
• Types of Operators
• Relational, Logical, Assignment, Increment and Decrement, Bitwise,Ternery
Operators
• Type conversion
• Precedence and Order of Expression Evaluation
• Little Endian and Big Endian Formats
Relational Operators
Less than: <
Greater than: >
Less than or equal to: <=
Greater than or equal to: >=
Equality Operators
Equal to : ==
Not equal to: !=
Suppose x and y are two integer variables with values 20 and -10 respectively.
Expression Value
x<y 0
x>y 1
x<=y 0
x>=y 1
x==y 0
x!=y 1
Logical Operators
Binary Operators
Logical AND: &&
Logical OR: ||
Unary Operators
Logical NOT: !
Suppose x and y are two integer variables with values 20 and -10 respectively.
Expression Value
x&&y 1
x||y 1
!x 0
Bitwise Operators
Bitwise AND: &
Bitwise OR: |
Bitwise Negation: ~
Bitwise XOR: ^
Left shift: <<
Right shift: >>
Suppose x and y are two integer variables with values 2 and 3 respectively.
x=[Link] value of x is 102
y=[Link] value of y is 112
Expression Value
x&y 2
x|y 3
~x -3 Note:
x^y 1 • ~x=2’s complement of x=-(x+1)
• x<<p=x*2p
x<<1 4
• x>>p=x/2p
x>>1 1
Assignment Operators and Compound
Assignments
Assignment Operator: =
Expression Equivalent Expression
x=y x=y
x+=y x=x+y
x-=y x=x-y
x*=y x=x*y
x/=y x=x/y
x&=y x=x&y
Increment and Decrement Operators
Increment: ++
Decrement: --
Both are unary operators.
They can be
pre-increment(++i)/ post increment(i++)
pre-decrement (--i)or post decrement(i--)
The Pre and post increment/decrement operators differ in the value used for
operand when they are embedded in expression
Prefix and Postfix Increment/Decrement Operators
Let a=4;
Expression Equivalent x value a value
Statements
x=a++ x=a; 4 5
a=a+1;
x=++a a=a+1; 5 5
x=a;
x=a-- x=a; 4 3
a=a-1;
x=--a a=a-1; 3 3
x=a;
Conditional Operator (?:)
It is ternary operator used for simple conditions.
Syntax: expression1?expression2:expression3
expression1 is evaluated, if it is true, expression2 is evaluated else, expression3 is
evaluated
Examples:
check if number is zero or non-zero:
inumber==0?printf(“Zero”):printf(“Non-zero”);
Check if no is even/odd:
inum%2==0?printf(“%d is even”,inum):printf(“%d is odd”,inum);
Check if the number is zero or non-zero
#include<stdio.h>
void main()
{
int iNumber;
printf("Enter an integer:");
scanf("%d",&iNumber);
iNumber==0?printf("Zero"):printf("Non-zero");
}
Other Operators
comma operator :allows evaluation of multiple expressions separated by commas.
Example: int a,b,c; c=a, a=b, b=c;
sizeof(type): prints size in bytes
Examples: sizeof(int)
sizeof(float)—4
Type conversion:
Type casting: Explicit type conversion
Syntax: (datatype)operand
Examples:
1. int i=4; float j; j=(float)i;
2. fCentigrade=fFahrenheit*((float)9/5)+32;
Precedence and Order of Expression Evaluation
Precedence: order in which different operators in complex expression are
evaluated
Associativity: order in which operators with same precedence in a complex
expression are evaluated. Associativity is applied only when there are more
than one operators of same precedence.
Operator Precedence Groups
Operator Category Operators Associativity
Unary operators () [] . - ++ -- ! sizeof (type) ~ R->L
Arithmetic multiply, divide, remainder */% L->R
Arithmetic add and subtract +- L->R
Bitwise shifts << >> L->R
Relational < <= > >= L->R
Equality == != L->R
Logical and && L->R
Logical or || L->R
Conditional operator ?: R->L
Assignment operator = += -= *= /= %= R->L
Comma , L->R
Examples
a=5 ; b=10 a=5 ; b=10
c = a* 2 + b / 2 ; c = a* 2 + b % 2 ;
c = 5 * 2 + 10 / 2 ; c = 5 * 2 + 10 % 2 ;
c = 10 + 10 / 2 ; c = 10 + 10 % 2 ;
c = 10 + 5; c = 10 + 0;
c = 15; c = 10;
Find the values
1. x=7*6%15/9 2. x=7*(6%15)/9
• x=1 x=4
Find the values
• The following expression has a single pair of parentheses missing that makes it syntactically wrong and it
does not compile. Place a pair of parentheses in proper places to correct the expression and evaluate it.
for x = 2, y = 3, z = 5, w = 7:
x*w+y*z=7
Find the values
• The following expression has a single pair of parentheses missing that makes it syntactically wrong and it
does not compile. Place a pair of parentheses in proper places to correct the expression and evaluate it.
for x = 2, y = 3, z = 5, w = 7:
Correct expression is x * w + y * (z = 7) with value 35
Number Systems
Decimal System: It is set of numbers that has digits 0-9. It has base 10.
Example: 10,28,9,7
Binary System: It includes either 0s or 1s. It has base 2.
Example:1010, 11100, 10001, 111
Octal System: It includes numbers with digits 0-7. It has base 8.
Examples:12, 34, 21, 7
Hexadecimal System: It includes numbers with digits 0-9 and alphabets A to F
denoting numbers 10 to 15 respectively. It has base 16.
Examples: 0A, 1C, 9, 7
Number Conversions
Decimal to Binary
Binary to Decimal
Binary to Octal
Octal to Binary
Binary to Hexadecimal
Hexadecimal to Binary
2’s complement Representation
Convert the number into binary
Find 1’s complement: change 1s to 0s and 0s to 1s
Binary Add 1 to it
Examples:
2's complement of "0111" is "1001"
2's complement of "1100" is "0100"
[Link]
2’s complement Representation
Represent 53 and −69 in binary 8-bit 2’s complement representation.
2’s complement Representation
Represent 53 and −69 in binary 8-bit 2’s complement representation.
Answer:
53 = 00110101
-69 = 10111011
Little Endian and Big Endian Formats
In a multibyte data type such as int or long or any other multibyte data type,
the right most byte is called least significant byte(LSB) and
the left most byte is called most significant byte (MSB).
Big Endian: In big endian format, the most significant byte is stored first, thus
gets stored at the smallest address byte.
Little Endian: In this format, the least significant byte is stored first.
[Link]
Little Endian and Big Endian Formats
[Link]
Macros
Preprocessor Commands:
The C compiler is made of two functional parts: a preprocessor and a
translator.
The preprocessor is a program which processes the source code
before it passes through the compiler.
The translator is a program which converts the program into machine
language and gives the object module.
If a statement starts with symbol # those are called preprocessor
commands.
These are processed by the preprocessor before the compilation.
There are three major tasks of a preprocessor directive:
Inclusion of other files (file inclusion)
Definition of symbolic constants and macros(macro definition)
Conditional compilation of program code/Conditional execution of
preprocessor directives
[Link] Devi,IT Dept
Macros
• Preprocessor- it is a program that processes our
source program before it is passed to the compiler.
C source code (HAI.C)
preprocessor
Expanded source code
(HAI.I)
compiler
Object code(([Link])
Linker
Executable code ([Link])
[Link] Devi,IT Dept
Example using macros
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main() 3.14*radius*radius;
{
float area, radius=3.0;
area= PI*radius*radius;
printf(“%f”, area);
Use of Macro-
}//main suppose a constant value like 10 appears many
times in the program. This value may be changed
to 8 sometimes.
you need to go through the program and manually
change each occurrence of the constant.
However, if you have defined constant value with
#define directive, you need to make one change in
the #define directive.
[Link] Devi,IT Dept
Find the output?
#include <stdio.h>
#define m 5+5
const int n = 5+5;
void main() {
int a = 0, b = 0;
a = m * m;
b = n * n;
printf("%d %d\n", a, b);
}
Math Functions: #include<math.h>
The following are some of the math functions:
Absolute
Ceil
Floor
Truncate
Round
Power
Square Root
All the above functions are available in math.h header file.
NOTE: While compiling in shell/terminal use –lm option when
math.h file is included.
e.g.: gcc program.c -lm
[Link] Devi,IT Dept
Absolute Functions:
An absolute value is the positive rendering of the values
regardless of its sign.
The integer functions are abs, labs, llabs.
Syntax:
int abs(int number);
long abs(long number);
long long abs(long number);
The float function is fabs:
float abs(float number);
Example: abs(3) gives 3
fabs(3.4) gives 3.4
[Link] Devi,IT Dept
Ceil Function:
A ceil function returns integer value that is greater than or equal
to a given number.
Format of ceil function:
double ceil(double number);
float ceilf(float number);
long double ceill(long double number);
Example:
ceil(-1.9) gives -1.0
ceil(1.1) gives 2.0
Ceiling Function
[Link] Devi,IT Dept
Floor Function:
A floor function returns the integer value that is equal to or
less than a number.
Format of floor function:
double floor(double number);
float floorf(float number);
long double floorl(long double number);
Example:
floor(-1.1) gives -2.0
floor(1.9) gives 1.0
[Link] Devi,IT Dept
Truncate Function:
The truncate function return the integral in the direction of 0.
Format of trunc function:
double trunc(double number);
float truncf(float number);
long double truncl(long double number);
Example:
trunc(-1.1) gives -1.0
trunc(1.9) gives 1.0
Round Function:
The round function returns the nearest integral value.
Format of round function:
double round(double number);
float roundf(float number);
long double roundl(long double number);
Example:
round(-1.1) gives -1.0
round(1.9) gives 2.0
round(-1.5) gives -2.0
[Link] Devi,IT Dept
Power Function:
The power function returns the value of the x raised to the power
y that
is xy.
Format of pow function:
double pow(double number1, double number2);
Example:
pow(2,5) gives 32
Square Root Function:
The square root function returns the non negative square root of a
number.
Format of sqrt function:
double sqrt(double number);
Example:
sqrt(4.0) gives 2.0
[Link] Devi,IT Dept
Lab Problems-6
1. Write a C program to check if the given number is odd or even using a ternary
operator.
2. Write a C program to swap two values without using temporary variable using bitwise
operators.
3. Write a C program to find the largest of two numbers using ternary operator.
4. Write a C program to check if the given number is divisible by 5 and 11 or 7 using
logical and ternary operators.
5. Write a C program to read two points and find the distance between two points.
6. Write a C program to read the total marks and print the following messages based on
below conditions using ternary operator.
• Passed: “Congratulations! you passed”
• Failed: “Sorry you are failed”
Note: Use Hungarian Notation while naming variables.