0% found this document useful (0 votes)
44 views

Operators: Main (Int X 5 Float K 0.8 Printf ("%d",sizeof (X+K) ) Printf ("%d",sizeof (X 7) ) Printf ("%D",X) )

The document contains explanations of C code snippets with various operators, data types, and functions like printf(), scanf(). It discusses output for code using unary, binary, assignment operators, bitwise operators, precedence rules, type conversions, and more. Examples show proper and erroneous usage of operators, functions and how inputs/outputs change based on order of operations.

Uploaded by

pco000
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Operators: Main (Int X 5 Float K 0.8 Printf ("%d",sizeof (X+K) ) Printf ("%d",sizeof (X 7) ) Printf ("%D",X) )

The document contains explanations of C code snippets with various operators, data types, and functions like printf(), scanf(). It discusses output for code using unary, binary, assignment operators, bitwise operators, precedence rules, type conversions, and more. Examples show proper and erroneous usage of operators, functions and how inputs/outputs change based on order of operations.

Uploaded by

pco000
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Operators

main()
{
int x=5;
float k=0.8;
printf(“%d”,sizeof(x+k));
printf(“%d”,sizeof(x=7));
printf(“%d”,x);
}

O/P: 4 2 5
EXPLANATION: The first printf() statement will print 4 because when
we add two different data types(in this case int and float) we will get the size of the
data type which occupies more bytes
main()
{
printf(“%d %d %d %d”,23,023,0x23,0X23);
}

O/P-23 19 35 35
main()
{
printf(“%d %o %x”,72,72,72);
}

O/P-72 110 48
main()
{
int s=90000;
int r=40000;

printf(“%d\t%d”,s,r);
}

O/P: 24464 -25536

Explanation:
(i) If 32767 < value < 65535, ans = value – 65536
(ii) If value > 65535, ans = value % 65536
main()
{
int a=33300;
float b=3.4e100;

printf(“a=%d b=%f”,a,b);
printf(“%d%d”,sizeof(a),sizeof(b));
}

O/P- -32536 +INF


24
int main()
{
char a=321;
printf("%d %c",a,a);
}

O/P- 65 A
main()
{
int a=1234;
printf(“%d\n”,(printf(“%d\n”,printf(“%d\n”,a))));
}

O/P-1234
4
1
main()
{
int x;
x=-3+4-7*8/5%10;
printf(“x= %d”,x);
}

O/P- x = 0
main()
{
int a=300*300/300;

printf(“%d”,a);
}

O/P-81
main()
{
char a=‘A’;
float p=8.0;

printf(“%d\n”,sizeof(8));
printf(“%d%d\n”,sizeof(a),sizeof(‘A’));
printf(“%d%d”,sizeof(p),sizeof(8.0));
}

O/P- 2
12
48
main()
{
float a=5,b=7;
int c;
c=a%b;

printf(“%d”,c);
}

O/P-Errors
Explanation:Modulus operator cannot be applied to floating-point variable
int main()
{
int x=5,a;
a=x++ + x++ + x++;
printf(“%d %d”,x,a);
}

O/P-8 15
int main()
{
int x=5,a;
a=x- - + x- - + x- -;
printf(“%d %d”,x,a);
}

O/P- 2 15
int main()
{
int x=5,a;
a=++x + ++x + ++x;
printf(“%d %d”,x,a);
}

O/P- 8 24
int main()
{
int x=5,a;
a=- -x + - -x + - -x;
printf(“%d %d”,x,a);
}

O/P- 2 6
main()
{
int a,b;
a=7;
b=a++ + ++a;
printf(“%d %d”,a,b);
}

O/P-9 16
main()
{
int a=5,b=7;
c=a- - * - -a * b++ + ++a * - -b;
printf (“%d%d%d”, a,b,c);
}

O/P-4 7 180
int main()
{
int a=5;
printf(“%d%d%d”, ++a, a--, a++);
}

O/P-6 6 5
 int main()
 {
   printf(NULL);
   return(0);
 }  
 

(c)No Output

Here NULL is treated as a NULL string.


 int main()
 {
   printf("%%",7);
   return(0);
 }   

O/P-%

% is a format specifier and it prints %. The excess arguments are merely ignored i.e. 7.
 int main()
 {
   printf("d%",8);
   return(0);
 }

 O/P-d%
 
 int main()
 {
   printf("Hi Friends"+3);
   return(0);
 }  

O/P-Friends
int main()
 {
   printf("Work" "Hard");
   return(0);
 }  

O/P-WorkHard
 int main()
 {
   int val=5;
   printf("%d %d %d %d",val,--val,++val,val--);
   return(0);
 }

O/P-Compiler dependant
 int main()
 {
   int val=5;
   val=printf("C") + printf("Skills");
   printf("%d",val);
   return(0);
 }

 O/P-CSkills7
 int main()
 }
   int val=5;
   printf("%d",5+val++);
   return(0);
 {  

O/P-Compile-Time Error
 int main()
 {
   printf("%d %d %d",5,!5,25-!25);
   return(0);
 }  
O/P-5 0 25
 

Negation (!) of non-zero value always returns 0 (zero). Thus the arguments
to the printf will be 5 0 25-0. Thus it prints 5 0 25
 int main()
 {
   int a=500,b=100,c=30,d=40,e=19;
   a+=b-=c*=d/=e%=5;
   printf("%d %d %d %d %d",a,b,c,d,e);
   return(0);
}

O/P-300 -200 300 10 4

Be careful while evaluating such type of expression. Precedence plays a major


role during computation.
Here the short-hand assignment operators precedence is from R->L.
Thus it prints 300 -200 300 10 4
[NOTE : 3 values entered by the user are:100 200 300] :
 int main()
 {
   int a=1,b=2,c=3;
   printf(“Enter the values of a, b, c:”);
scanf("%d %*d %d",&a,&b,&c);
   printf("a=%d b=%d c=%d",a,b,c);
   return(0);
 }   

O/P-100 300 3

The * (asterisk) in the scanf statement indicates an assignment suppression operator


that skips the related value
and picks the next value in the input series.
  [NOTE : THE USER INPUT IS :A B C] : 
int main()
 {
   char a,b,c;
   printf(“Enter the values of a, b, c:”);
scanf("%c%c%c",&a,&b,&c);
   printf("a=%c b=%c c=%c",a,b,c);
   return(0);
 }
 
 

O/P-a=A b= c=B
While accepting we're expecting NO whitespaces between the three different inputs
i.e. characters but O/P-a=A b= c=B we've encountered characters in the actual input.
main()
{
int p,q;
printf(“Accept the values for p and q”);
scanf(“ %d%d ”, p, q);

printf(“p=%d q=%d”,p,q);
}

O/P:Segmentation Fault
int main()
{
int a=5;
printf(“%d”,5++);
}

O/P-Error

L-Value required
int main()
{
int x=6>>1;
printf(“%d”,x);
}

O/P: 3
int main()
{
int x=6<<1;
printf(“%d”,x);
}

O/P: 12
int main()
{
int a=3,b;
b=a+++5;
printf(“%d %d”,a,b);
}

O/P- 4 8

You might also like