C doubt

  • Thread starter sangeeta chowdhary
  • Start date
S

sangeeta chowdhary

I tried this code,and getting output -hhe!
Not able to understand why i am getting exclamation mark in output.

#include<stdio.h>
#include<string.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++;
printf("%c",ch);
ch= ++i;
printf("%c",ch);
return 0;
}
 
T

Tim Streater

sangeeta chowdhary said:
I tried this code,and getting output -hhe!
Not able to understand why i am getting exclamation mark in output.

#include<stdio.h>
#include<string.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);


try ch = s[i++];
printf("%c",ch);
ch= ++i;


try ch = s[++i];
printf("%c",ch);
return 0;
}

If you meant to type what you typed, your output could be anything.

Otherwise you should check more carefully what you are doing.
 
F

Francois Grieu

#include<stdio.h>
#include<string.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++;
printf("%c",ch);
ch= ++i;
printf("%c",ch);
return 0;
}

I tried this code,and getting output -hhe!


It is unlikely that you got that '-'
Not able to understand why i am getting exclamation mark in output.

Because in "hhe!" the '!' comes after the 'e', this '!' is likely
produced by the printf statement after the one that produced the 'e', right?

So what likely is displayed for each expression involving both i and s?

What expression produced what is displayed as '!' ?

In what order is this expression evaluated ?

What does the ++ in that expression apply to ?

What really *IS* that thing that the ++ in that expression applies to ?

What is the value of i before and after evaluation of that expression ?

What is the i th char in s before and after evaluation of that expression ?

Can you learn without answering questions by yourself ?


François Grieu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,484
Latest member
MasqueradeOfSilence

Latest Threads

Top