Unit1 C Programming 6 10 2022
Unit1 C Programming 6 10 2022
Unit1 C Programming 6 10 2022
ANSI C
Topics
• Types of Operators
• Relational, Logical, Assignment, Increment and Decrement, Bitwise,Ternery
Operators
• Type conversion
• Precedence and Order of Expression Evaluation
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=2.Binary value of x is 102
y=3.Binary 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: =
Let a=4;
}
Other Operators
comma operator :allows evaluation of multiple expressions separated by commas.
Example: int a,b,c; c=a, a=b, b=c;
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:
https://www.geeksforgeeks.org/1s-2s-complement-binary-number/
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.
https://cs-fundamentals.com/tech-interview/c/c-program-to-check-little-and-big-endian-architecture
Little Endian and Big Endian Formats
https://open4tech.com/little-endian-vs-big-endian-in-embedded-systems/
Macros
Preprocessor Commands:
The C compiler is made of two functional parts: a preprocessor and a
translator.
preprocessor
Object code((HAI.obj)
Linker
Executable code (HAI.exe)
Syntax:
int abs(int number);
long abs(long number);
long long abs(long number);
Ceiling Function
Example:
pow(2,5) gives 32
Example:
sqrt(4.0) gives 2.0
S.Durga 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.