Is there something wrong with my code?
I am quite confused about inputing a string and a character...
Code:#include <stdio.h> #include <stdlib.h> int main () { int c[21]; while (scanf("%c\n",c) != EOF) { printf("%s\n",c); } return 0; }
Is there something wrong with my code?
I am quite confused about inputing a string and a character...
Code:#include <stdio.h> #include <stdlib.h> int main () { int c[21]; while (scanf("%c\n",c) != EOF) { printf("%s\n",c); } return 0; }
Yes, you are scanf-ing a character in variable of type character array.
You need to do something like scanf("%c",c[i]); where i is some valid index.
try this:
Code:int i = 0; while(scanf("%c\n",c[i++] != EOF){ printf("%s\n",c); }
You definitely don't need to be EOF checking when doing stdin.
If you're trying to populate an integer array, then you'll be using %d, not %c.
Code:for ( i = 0; i < 21; ++i ) { scanf ( "%d", &c[i] ); }
sorry, the code I posted was wrong...
It should be
i got a error message..Code:int main () { char c[21]; while (scanf("%c\n",c) != EOF) { printf("%c\n",c); } }
warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’
Code:int main () { char c[21]; int i=0; while (scanf("%c",&c[i])!=EOF) { printf("%c",c[i++]); } }