Output of the programme?


#1

#include<studio.h>
#define PRODUCT(x) (x*x)
void main()
{
int i=3,j,k,l;
j=PRODUCT(i+1);
k=PRODUCT(i++);
l=PRODUCT(++i);
printf("%d\n%d\n%d\n%d",i,j,k,l);
}
The output of program is
7
7
12
49
Can anyone explain the values of k and l ?


#2

j = 3+1 * 1+3 = 7
k = i++i++ = 3 * 4
l = ++i
++i = ?


#3

What will be the output of the following C code?
void main()
{
int a[3] = {67,43,49};
int *p = a;
printf("%d",++ * p);
printf("%d", * + +p);
printf("%d", * p++);
}

}


#4

Due to editor some thing are typed differently ,there is no space between ++ and *p in first printf,and also in 3.
a)This question is easy,and if you solve it carefully you will get the correct answer
the first statement increases the value to which p is pointing to.That value is 67,since its pre increment,output will be 68 of first statement.

b)In the second we are shifting p to one pointer right ,so p now will point to 43,and we are printing value at a[1] actually so 43 will be printed.

c)in the third since its post increment the answer will be same.43.

Bonus :whats the output of printf("%d, * p ++) or printf("%d, * ++ p ) if we print these statements after the last print,


#5

From above question last value of printf is 43,if we applied two statement given in a question’s last two line ,
For printf("%d",*p++); = 44 because after last printf given in question give 43 but after post increment it becomes 44. Therefore for above printf value of p is 44.
And for print("%d",
++p);=45 ,because we can see in above lines value of *p =44 but after pre -increment value of *p become 45.


#6

I think after 3rd printf,p will point to third location of the array,so printf("%d, * p ++) will give 49,after that the pointer will not point to some random location,so p i think can print anything,will have to check that it doesnt throw error.


#7

Here in first ie., ("%d,*p++) the complier will read this statement as (p++) the last value of p will be post incremened( ie., if the value of p is 10 it will become 11 but will not be printed output will be 10 ). In the second statement ie., ("%d,++p) the complier will read that statement as *(++p) so the last value will be incremented by 1 and the output will be the incremented value of p (ie ., if the value of p is 10 then the output will be 11 ).


#8

J=3+11+3=3+1+3=7
K=i++i++= 34=12
L=7
7=49


#9

i=3
j=i+1i+1= 3+13+1= 3+3+1=7
k=i++i++ = 34= 12 here value of i becomes 5
l=++i*++i = 7*7= 49 here we have used the pre increment operator which makes the value of i from 5 to 7.


#10

as we can see.

that there is i and k and j and lst l as.

i=3
j=i+1i+1= 3+13+1= 3+3+1=7
k=i++i++ = 34= 12 here value of i becomes 5
l=++i*++i = 7*7= 49 here we have used the pre increment operator which makes the value of i from 5 to 7.


#11

i=3
j = i+1*i+1 = i+i+1=3+3+1 = 7
k = i++*i++ = 4*3 = 12 now here i become 5
l = ++i*++i = 7*7 = 49 now here i become 7


#12

The given array is a single dimensional array. p is a pointer and p=a.
‘a’ stores the base address of the 0th element of the array a[]. So p also points to 0th element of array a [].

  1. *p means accessing the data stored in the address pointed by ‘p’. This gives 67. This value is pre incremented. So ans is 68.
  2. ++p means p now points to the next element of the array a[]. So ans is 43.
  3. *p++ means p is post-incremented and then the value at the respective address is accessed.
    As it is post incremented so ans is still 43.
    After this step *p++. If we printf(*p) ans would be 49.