nAutAxH AhmAd nAutAxH AhmAd - 1 year ago 59
C Question

Strings in C Program

I am making a program in C language which prints the ASCII value of the string/text including spaces. The program works fine and give exact ASCII values of string including spaces but the problem is that it also prints '10' at the end of all ASCII values.

This is my code :

int main()
char str[100];
int i;
printf("Enter a string: ");
printf("String is: %s\n",str);
printf("ASCII value in Decimal is: ");
for(i=0; str[i]!='\0'; i++)
printf("%d ",str[i]);

Please help me out, tell me what's the problem and how to fix it. Thanks in advance.

Answer Source

When you read a string with fgets the enter you press is included in the string( man page ). ASCII code for \n is 10.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download