nAutAxH AhmAd nAutAxH AhmAd - 2 months ago 14
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.


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