Hi,
i have just started learning C and trying some code. Here is a snippet which is causaing some troubles.
printf("the value of EOF is %d\n",EOF);
c = getchar() ;
while (c != EOF){
putchar©;
c=getchar();
}
the printed EOF value is -1. that' fine. however i am having troubles with the rest of the code. when i run it, and say i type for fun cccccccccccccccccc and then hit return,the whole chain of characters is printed out. however getChar is meant to read only ONE character. when i type -1, the loop is not exited. instead -1 is printed. any explanation please.
Cheers
troubles with getchar
Started by elvira23, Sep 01 2009 09:06 AM
16 replies to this topic
#1
Posted 01 September 2009 - 09:06 AM
|
|
|
#2
Posted 01 September 2009 - 09:42 AM
-1 is two characters, '-' and '1'. You will need to type Ctrl-D or Ctrl-Z to exit (depending on your OS).
#3
Posted 01 September 2009 - 11:01 AM
thanks for your reply. i was able to exit with ctrl+c being in winXP. but this not my main question. getchar is meant to read one char, why is it reading more, including ctrl-c or -1 or ccccccccccccccccc
#4
Posted 01 September 2009 - 11:11 AM
elvira23 said:
the printed EOF value is -1. that' fine.
elvira23 said:
when i run it, and say i type for fun cccccccccccccccccc and then hit return,the whole chain of characters is printed out. however getChar is meant to read only ONE character.
elvira23 said:
when i type -1, the loop is not exited. instead -1 is printed. any explanation please.
#5
Posted 01 September 2009 - 12:14 PM
thanks for your reply. silly me! Indeed, i changed the loop code with the following:
getchar();
if((c = getchar()) != EOF)
putchar©;
i typed in then: cfgh, only the char c was displayed afterwards. a side point though, i had to precede the if statement above with getchar, otherwise the program execution terminates before allowing me to type in any character. a bank space is printed instead automatically.
why is that? may be because stdin is empty but i did not hit return for the getchar function to go and read the content of stdin
getchar();
if((c = getchar()) != EOF)
putchar©;
i typed in then: cfgh, only the char c was displayed afterwards. a side point though, i had to precede the if statement above with getchar, otherwise the program execution terminates before allowing me to type in any character. a bank space is printed instead automatically.
why is that? may be because stdin is empty but i did not hit return for the getchar function to go and read the content of stdin
#6
Posted 01 September 2009 - 12:39 PM
elvira23 said:
thanks for your reply. silly me! Indeed, i changed the loop code with the following:
getchar();
if((c = getchar()) != EOF)
putchar©;
i typed in then: cfgh, only the char c was displayed afterwards. a side point though, i had to precede the if statement above with getchar, otherwise the program execution terminates before allowing me to type in any character. a bank space is printed instead automatically.
why is that? may be because stdin is empty but i did not hit return for the getchar function to go and read the content of stdin
getchar();
if((c = getchar()) != EOF)
putchar©;
i typed in then: cfgh, only the char c was displayed afterwards. a side point though, i had to precede the if statement above with getchar, otherwise the program execution terminates before allowing me to type in any character. a bank space is printed instead automatically.
why is that? may be because stdin is empty but i did not hit return for the getchar function to go and read the content of stdin
#include <stdio.h>
int main ()
{
int c;
getchar();
if ( (c = getchar()) != EOF )
putchar(c);
return 0;
}
/* my output
cfgh
f
*/
#7
Posted 01 September 2009 - 12:44 PM
there was a printf before getchar. i am using quincy to edit and run the code. here is the code
#include <stdio.h>
main()
{
float fahr, celcious;
float lowFahr, hiFahr;
int c;
printf(" please type in the low fahr degree ");
scanf("%f",&lowFahr);
printf(" please type in the high fahr degree ");
scanf("%f",&hiFahr);
for(fahr=lowFahr;fahr<=hiFahr;fahr+=10)
{
celcious= (5.0/9)*(fahr-32);
printf(" fahr temp is %.2f and celcious is %.2f\n",fahr,celcious);
}
printf("the value of EOF is %d\n",EOF);
/* while ((c = getchar()) != EOF)
putchar(c);
*/
getchar();
if((c = getchar()) != EOF)
putchar(c);
}
#8
Posted 01 September 2009 - 12:50 PM
ignore...
#9
Posted 01 September 2009 - 12:53 PM
What's wrong with something like this?
#include <stdio.h>
int main ()
{
int c;
for ( c = getchar() ; c != '\n'; c = getchar() )
putchar(c);
return 0;
}
And it's "Celsius". :P
Wow I changed my sig!
#10
Posted 01 September 2009 - 12:57 PM
elvira23 said:
there was a printf before getchar. i am using quincy to edit and run the code. here is the code
#11
Posted 01 September 2009 - 01:04 PM
sorry for the confusion caused. what i should have said is the following. if i remove the getchar before the if statement, a new line is displayed and i am not allowed to type in any character. if i insert getchar as given above, i am able to type any my list of charcters. however, it is the first character and not the second which is displayed back by putchar
#12
Posted 01 September 2009 - 01:08 PM
ZekeDragon said:
What's wrong with something like this?
#include <stdio.h>
int main ()
{
int c;
for ( c = getchar() ; c != '\n'; c = getchar() )
putchar(c);
return 0;
}
And it's "Celsius". :Pnothing wrong. i am learning C so i am changing some codes from a textbook to improve my understanding


Sign In
Create Account


Back to top









