Unit-II Introduction To C Programming
Unit-II Introduction To C Programming
Kernighan &
K&RC 1978
Dennis Ritchie
* multiplication
/ division
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
• // Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c; c = a; // c is 5
printf("c = %d\n", c); c=5
c += a; // c is 10
printf("c = %d\n", c); c = 10
c -= a; // c is 5
printf("c = %d\n", c); C=5
c *= a; // c is 25
printf("c = %d\n", c); c=25
c /= a; // c is 5
printf("c = %d\n", c); c=1
c %= a; // c = 0
printf("c = %d\n", c); c=0
return 0;
}
4.Relational Operators
• A relational operator checks the relationship between two operands. If the relation
is true, it returns 1; if the relation is false, it returns value 0.
• Relational operators are used in decision making and loops.
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
• Assume A = 60 and B = 13 in binary format, they will be
as follows −
• A = 0011 1100
• B = 0000 1101
• -----------------
• A&B = 0000 1100 A*B=
• A|B = 0011 1101 A+B=
• A^B = 0011 0001
• ~A = 1100 0011
1 2 4 8 16 32 64 128 256 512 1024 2048
Example
#include <stdio.h>
main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c ); Line 1 - Value of c is 12
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c ); Line 2 - Value of c is 61
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c ); Line 3 - Value of c is 49
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c ); Line 4 - Value of c is -61
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c ); Line 5 - Value of c is 240
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c ); Line 6 - Value of c is 15
}
7.Conditional Operator in C
• The conditional operator is also known as a ternary operator.
• The conditional statements are the decision-making
statements that depend upon the output of the expression.
• As a conditional operator works on three operands, so it is
also known as the ternary operator.
• The operands may be an expression, constants, or variables.
• It starts with a condition, hence it is called a conditional
operator.
• Syntax of Conditional Operator in C:
expression1 ? expression2 : expression3;
or for simplicity, we write it as
condition ? true-statement : false-statement;
The expression1 is evaluated, it is treated as a logical
condition.
If the result is non-zero then expression2 will be evaluated
otherwise expression3 will be evaluated.
The value after evaluation of expression2 or expression3
is the final result.
• The conditional operator in C works similar to the conditional
control statement if-else.
• Hence every code written using conditional operator can also be
written using if-else.
• When compared with if-else, conditional operator performance is
high.
if(expression1)
{
expression2;
}
else
{
expression3;
}
Write a C program to find the maximum in the given two
numbers using the conditional operator
#include<stdio.h>
int main()
{
float num1, num2, max;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
max = (num1 > num2) ? num1 : num2;
printf("Maximum of %.2f and %.2f = %.2f", num1, num2,max);
return 0;
} max=(34>45)?34: 45;
8.SPECIAL OPERATORS IN C
a+bX c a+b*c
ax2+bx+c a*x*x+b*x+c
(4ac/2a) (4*a*c)/(2*a)
Operator precedence
Rules for evaluation of expression
• Evaluate the sub expression from left to right if
parenthesized.
• Arithmetic expression from left to right using the rules of
precedence.
• Highest precedence is given to the expression with in
parenthesis
• Apply the associative rule,if more operators of the same
precedence occurs.
• Evaluate the inner most sub expression if the parenthesis are
nested.
Type Conversion
• Type Conversion refers the process of changing an entity
of one data type into another.
• Mix the types of values in your arithmetic expression.
• To take advantage of certain features of one data type to
another.
Example:
• Integers can be stored in a more compact format and later
converted to a different format enabling operations not
previously possible.
Two types of type conversion
i.Implicit ,ii.Explicit
• Implicit-Compulsory conversion
• It’s an automatic type conversion ,mixed type expression
data of one or more subtypes can be converted to a super
type as needed at run time, so that the program will run
correctly.
• Explicit Conversion: Type Casting
• Explicit Conversion can be made to convert one data type to
another by forcefully and it is different from the implicit
conversion.
Syntax:var2=(datatype)var2;
int a =20; float(a);
float(a) will contain 20.000000
Input and Output operations
• C language 2 types of input/output statements are available.
• I/O operations carried out through function calls.
I/O functions