I'm a newbie to C and I know that Char type variables can only hold one character - something like
char c = 'a';
If this is true, how does this C program I wrote work?
#include "stdafx.h"
#include <stdio.h>
int main()
{
char c;
c=getchar();
while (c!=EOF)
{ putchar(c);
c = getchar();
}
getchar();
return 0;
}
If you follow the example, variable c is a char, so it can only hold one character, but when I the program, I can type a whole string like "Hello world" into the console, and after I hit Enter (I know that the getchar() function will assign what i've typed in to variable c), the console prints a whole string like "Hello world" too (that's what the putchar() function does.) I ran this code in Visual Studio 2010.
What I thought is that when I type the string "Hello world" and type enter, the program must give a warning because an overflow happened. Nevertheless, when I enter a string over the size limit of a char type (1 byte length) everything runs fine. Why is there no error?