C Operator
C Operator
Hierarchy
Types of
Operators
Increment/
Arithmetic Logical Bitwise
Decrement
• 2nd Pass
x=5+6-1
x=11-1
x=10
Rules for Evaluation of
Expression
• Parenthesized sub expression from left to right are evaluated
Implicit Explicit
In
Automatic Assignments
The data type of one operand is converted into data type of another operand
Implicit Type
Conversion
• Implicit type conversion, also known as coercion
long double
double
float
int
• Syntax:
• (datatype) expression
• Ex:
int x,y;
float x=(float)x/y;
Input/Output
Types of
Operations
Input Output
• Ex:
char variable_name;
variable_name=getchar();
Writing a
Character
• putchar(variable_name);
• Ex:
char c=getchar();
putchar(c);
Conversion
Specifications
Specifier meaning
%c a single character
%d or %i decimal integer
%h short int
%s string
%o octal integer
%x hexadecimal
• Ex 1
int marks;
scanf(“%d”,&marks)
;
• Ex 2
char str[30];
scanf(“%s”,str); Value will not be stored in str
• Ex 3
int basic,da; scanf(“%d
%d”,&basic,&da);
• Ex 4
int hra,da;
scanf(“%d:%d”,&hra,&da);
1500:200
• Ex 5
int num1,num2;
scanf(“%2d %5d”,&num1,num2);
21345 50
• 21 will be assigned to num1 and 345 will be assigned to
num2 and 50 that is unread will be assigned to next scanf
call
• Ex 6
int a,b;
scanf(“%d %*d %d”, &a,&b);
• 123 to a
• 456 skipped (because of 123 456 789
*)
• 789 to b
Examples Real
Numbers
• Ex 1
float x;
scanf(“%f”,&x);
• Ex 2
double y;
scanf(“%lf”,&y);
Examples char and
• Ex 1
string
char name1[15];
scanf(“%15c”,&name1);
• Ex 2
char name2[20];
scanf(“%s”,&name2);
• Ex 3
char add[20];
scanf(“%[a-z]”,&add);
• Ex 4
char add[20];
scanf(“%[^\n]”,&add);
Rules for
scanf
• Each variable must have a field specification
• For each field specification there must be variable address
• The scanf reads until
• A white space is found in numeric specification
• the maximum number of characters have been read
• An error is detected
• The end of file is reached
Formatted
Output
• printf() is used for printing results
• printf does not supply new line automatically. Thus ‘\n’ is used
Integer
Examples
• printf(“%d”,9678);
9 6 7 8
• printf(“%6d”,9678); 9 6 7 8
• printf(“%2d”,9678); 9 6 7 8
• printf(“%-6d”,9678); 9 6 7 8
0 0 9 6 7 8
• printf(“%06d”,9678);
Real
Examples
• Syntax: %w.pf
• w indicates the number of digits used for display
• p indicates the number of digits to be displayed after decimal
• Let y=98.7654;
• printf(“%7.4f”,y);
• printf(“%7.2f”,y); 9 8 . 7 6 5 4
• printf(“-7.2f”,y); 9 8 . 7 7
9 8 . 7 7
String
Examples
• Syntax: %w.ps
• w specifies width of field
• p specifies only first p characters of string are displayed
• Ex:
• char a[20]=“Hello World”;
• printf(“%s”,a);
H e l l o W o r l d
Assignmen
t
• WAP to print exponential value of 10.45678?
• WAP to print multiplication table of a number taken from
user (Don’t use loop)?
• If 5 digit number is input through keyboard WAP to
reverse the number?
• WAP to read the values of x and y and print the
expressions
• x+y/x-y
• x+y/2
• (x+y)(x-y)
• WAP to take number as input and display in triangular format. For ex
if number is 5678 then
5678
678
78
8
• WAP to print size of various data types in C
• WAP to find that given an integer is odd or even
• WAP to accept three numbers and print the largest
number without using if condition.