Need some info

5 replies [Last post]
Offline
Last seen: 1 year 17 weeks ago
Joined: 01/07/2011
Posts:

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

tim.speed's picture
Offline
Last seen: 17 hours 43 min ago
Joined: 02/24/2010
Posts:
Where are you getting this

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

Offline
Last seen: 1 year 17 weeks ago
Joined: 01/07/2011
Posts:
Thanks for replying... I did

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...

tim.speed's picture
Offline
Last seen: 17 hours 43 min ago
Joined: 02/24/2010
Posts:
You can review the ASCII

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'

-----------------
Tim Speed
Compilr Developer and CTO

Offline
Last seen: 1 year 17 weeks ago
Joined: 01/07/2011
Posts:
I`ll try this. Thanks!

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.

magius96's picture
Offline
Last seen: 14 weeks 1 day ago
Joined: 04/13/2009
Posts:
RE:

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.

----------------------------------------------------------------------------------------------------
"Coding your life, Compiling your dreams, and crashing your future."
Compilr - C# and VB.NET Advisor