Originally Posted by
barim
I am trying to read characters and count its numbers. First, my compiler prompted me for the size of the array. I added number 50, why do I have to specify size in this case?
I have an erro message:
parse error before "char"
Here is a fixed version of your code. Please read it carefully and ask for details if necessary.
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int read_line (char *str, size_t n)
{
int ch;
size_t i = 0;
/* All the characters are read.
* We don't know that the input line was trucated...
*/
while ((ch = getchar ()) != '\n' && ch != EOF)
{
if (i < (n - 1))
{
str[i++] = ch;
}
}
str[i] = 0;
return i;
}
int main ()
{
char str[10];
size_t n;
printf ("Enter your name please:\n");
n = read_line (str, sizeof str);
printf ("Your name '%s' has %u characters\n", str, (unsigned) n);
system ("pause");
return 0;
}