I was trying to make a c program to count the number of characters entered, and here is the code:
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=0;
while(getchar()!= EOF)
++i;
printf("%d\n", i);
}
Well this doesn't work. After entering the string there is no value printed. It also doesn't work if I declare char c and use scanf.
But the following code works :
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int cl=0,i;
char str[20];
gets(str);
for(i=0; str[i]!= NULL ; i++)
{
cl = cl +1;
}
printf("%d", cl);
}
Why I don't know. Btw I am using codeblocks IDE with gcc compiler.