Need some info
Hello. Please answer me this:
#include
int main()
{
int a = 0;
char ch;
char arr[10];
for (ch=getchar(); a<10; a++, ch = getchar()) {
printf("Iterration of a = %d \n", a);
printf("CH out %c \n", ch);
}
return 0;
}
in this simple code, why a single entering of a character requires 2 for loops. Ex.
input 'R' and you`ll get 2 for iterrations one with CH = R other with CH = " "? Why is that? Is that because of the \0 terminating string in the end of GETCHAR()? And if so why it is called? Was not GETCHAR line buffered
Thanks for replying... I did the code some other way: #include
#define SIZE 10
int main()
{
char are[SIZE];
char c, *ch;
ch = are;
int a, b;
a =0;
printf("%p is the address of ARE[SIZE] \n", &are);
while ( a < SIZE-1 ) {
*ch++ = getchar();
a++;
}
this way the code shows that pressing ENTER key inserts a '\0' terminator to the "are" array and actually every time you press ENTER you fill array with 2 cells of memory... This was confusing because I was thinking of the bytes differences between char and int but after I set all to char so everything counts as 1 byte, I realised that getchar() does not excludes '\0' terminator... The NEWLINE tooks it`s place.... any info how to solve this? I tried getchar() - '\n' ineffective...
You can review the ASCII Table here: http://bestofthisweb.com/blogs/tag/ascii-table/
On some systems \n actually create two characters I believe... A CarriageReturn '13' followed by a LineFeed '10'...
But I think it is common for most systems to use the linefeed. Try ignoring getChar() values == 13 and treat 10 as '\n'
I`ll try this. Thanks!
I was thinking of the returning numeric valuse of int getchar(void) but I had no idea that 13 and 10 were the combinations of '\n' and '\r'.. I`ll try this as soon as possible.
When you use the include directive, you have to tell it what to include...
#include < stdio.h >
---- EDIT ----
just realized your include directive may be dissapearing in the HTML tags.
Where are you getting this code from?
ch=getchar(), being called twice seems like a bad idea unless the process is intentionally skipping a character.
-----------------
Tim Speed
Compilr Developer and CTO