C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 13)
13.
What will be the output of the program ?
#include<stdio.h>
int main()
{
static char s[25] = "The cocaine man";
int i=0;
char ch;
ch = s[++i];
printf("%c", ch);
ch = s[i++];
printf("%c", ch);
ch = i++[s];
printf("%c", ch);
ch = ++i[s];
printf("%c", ch);
return 0;
}
Discussion:
34 comments Page 1 of 4.
Hemanth said:
8 years ago
int main()
{
static char s[25] = "The cocaine man";
int i=0;
char ch;
ch = s[++I]; //pre-increment pointer from s[0] to s[1] i.e s[1]= h
printf("%c", ch); // print s[1]=h
ch = s[i++]; //post-increment pointer from s[1] to s[2]
printf("%c", ch);// print s[1]=h because ch = s[i++]; is post increment which increment after next print
ch = i++[s]; //post-increment pointer from s[2] to s[3]
printf("%c", ch); // print s[2]=e because ch = s[i++]; is post increment which increment after next print
ch = ++i[s];// this increment s[3] which is blank space by incrementing the ascii address 20 to 21(!)
printf("%c", ch);//print (!)
return 0;
}
{
static char s[25] = "The cocaine man";
int i=0;
char ch;
ch = s[++I]; //pre-increment pointer from s[0] to s[1] i.e s[1]= h
printf("%c", ch); // print s[1]=h
ch = s[i++]; //post-increment pointer from s[1] to s[2]
printf("%c", ch);// print s[1]=h because ch = s[i++]; is post increment which increment after next print
ch = i++[s]; //post-increment pointer from s[2] to s[3]
printf("%c", ch); // print s[2]=e because ch = s[i++]; is post increment which increment after next print
ch = ++i[s];// this increment s[3] which is blank space by incrementing the ascii address 20 to 21(!)
printf("%c", ch);//print (!)
return 0;
}
(6)
Manohar Reddy said:
1 decade ago
//Remember %c for number means it will print equivalent ASCII value//.
I=0; s[++i] ==> s[1] ==> h.
I=1; s[i++] ==> s[1] ==> h /*Since it is post increment no change to inside i*/.
I=2; i++[s] ==> (i++) [s] ==> s[i++] ==> s[2] ==>e /*post increment to I */.
I=3; ++i[s] ==> ++ (i[s]) ==> ++ (s[i]) ==> ++ (s[3]) ==> ++ (32) ==> 33.
==> ASCII value of 33 !
S[3] is nothing but "space" in the given string. ASCII value for "space" is 32.
It was incremented by 1. So 32+1=33.
ASCII value of 33 will be print.
I=0; s[++i] ==> s[1] ==> h.
I=1; s[i++] ==> s[1] ==> h /*Since it is post increment no change to inside i*/.
I=2; i++[s] ==> (i++) [s] ==> s[i++] ==> s[2] ==>e /*post increment to I */.
I=3; ++i[s] ==> ++ (i[s]) ==> ++ (s[i]) ==> ++ (s[3]) ==> ++ (32) ==> 33.
==> ASCII value of 33 !
S[3] is nothing but "space" in the given string. ASCII value for "space" is 32.
It was incremented by 1. So 32+1=33.
ASCII value of 33 will be print.
Tarlika said:
9 years ago
Here for first ch=s[++i]: first i=1,then ch=s[1]='h'.
For second ch=s[i++]:first ch=s[i]=s[1]='h',then i++becomes i=2;.
For third ch=i++[s];ch=2[s]='e';then i++ becomes i=3;.
For fourth ch=++i[s];here first ch=3[s]=s[3]=' '(space), ASSCII value=32 after that ++i[s] Takes place so ch=33 which means ASSCII Value for '!' so ch='!'.
For second ch=s[i++]:first ch=s[i]=s[1]='h',then i++becomes i=2;.
For third ch=i++[s];ch=2[s]='e';then i++ becomes i=3;.
For fourth ch=++i[s];here first ch=3[s]=s[3]=' '(space), ASSCII value=32 after that ++i[s] Takes place so ch=33 which means ASSCII Value for '!' so ch='!'.
(2)
Matt said:
9 years ago
++i means that i is incremented first and then evaluated.
i=0;
if(++i == 1) this is evaluated as ((i+1) == 1) which is ((0+1) == 1) which is true.
i++ means that i is evaluated first and then incremented.
i=0;
if(i++ == 1) this is evaluated as (i == 1) which is (0 == 1) which is false and then i = i+1;
i=0;
if(++i == 1) this is evaluated as ((i+1) == 1) which is ((0+1) == 1) which is true.
i++ means that i is evaluated first and then incremented.
i=0;
if(i++ == 1) this is evaluated as (i == 1) which is (0 == 1) which is false and then i = i+1;
Richa said:
7 years ago
++i is pre incrementor.
Whereas i++ is postincrementor, both of these are the binary operator and has LOW PRIORITY when compared to Assignment operator(=),so in 1st case, ++i yields s[1], and then due to priority case first " VALUE IS ASSIGNED AND THEN INCREMENTED".
Whereas i++ is postincrementor, both of these are the binary operator and has LOW PRIORITY when compared to Assignment operator(=),so in 1st case, ++i yields s[1], and then due to priority case first " VALUE IS ASSIGNED AND THEN INCREMENTED".
Iranna said:
1 decade ago
s[++i]=s[1]=h here i is incremented and assigned
s[i++]=s[1]=h here i is incremented but not assigned
i++[s]<->(i++)[s]<->s[i++]=s[2]=e
++i[s]<->++(i[s])<->++(s[i])=++(s[3])=++(asci value of space)=++(32)=33 is equivalent to !
So ans is hhe!
s[i++]=s[1]=h here i is incremented but not assigned
i++[s]<->(i++)[s]<->s[i++]=s[2]=e
++i[s]<->++(i[s])<->++(s[i])=++(s[3])=++(asci value of space)=++(32)=33 is equivalent to !
So ans is hhe!
Hariharasubramaniam said:
1 decade ago
s[3] is the space in the string "The cocaine mam" i.e
s[0]==T
s[1]==h
s[2]==e
s[3]==(space) so the ascii value for space was 32,
Since ++i[s] == ++ (i[s]) == ++ (s[i])== ++ (s[3]), so ++(32)==33.
ASCII value 33 will print === !
s[0]==T
s[1]==h
s[2]==e
s[3]==(space) so the ascii value for space was 32,
Since ++i[s] == ++ (i[s]) == ++ (s[i])== ++ (s[3]), so ++(32)==33.
ASCII value 33 will print === !
(1)
Ragini said:
1 decade ago
s[++i] = given i=0 ++i means 0+1 therefore s[1]=h.
s[i++] = s[1] itself because post increment therefore s[1]=h.
i++[s] = s[i+1] as now i=1 increment i => 1+1=2 s[2]=e.
++i[s] = ++(s[i]) => ++s[2] => 33(ascii value ) so !.
s[i++] = s[1] itself because post increment therefore s[1]=h.
i++[s] = s[i+1] as now i=1 increment i => 1+1=2 s[2]=e.
++i[s] = ++(s[i]) => ++s[2] => 33(ascii value ) so !.
FlameNfire said:
1 decade ago
int i=0;
std::cout<<i++;
//Adds 1 to i, but output i's value before adding.
int a=0;
std::cout<<++a;
//Adds 1 to a, but output a's value after adding.(Which seems more logical for us).
std::cout<<i++;
//Adds 1 to i, but output i's value before adding.
int a=0;
std::cout<<++a;
//Adds 1 to a, but output a's value after adding.(Which seems more logical for us).
Vivek said:
3 years ago
Why it didn't take s[4]=c and gave the answer hhec directly, I mean to say as it did in the first 3 cases, why not it's the same as in the last case. Why do they take ASCII value in the last?
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers