int getchar(); or char getchar(); 
Author Message
 int getchar(); or char getchar();

Hi, I am trying to figure out if   "ch"   should be  declared as an
"int"   or   "char".   I tried it both ways, and both ways work.
Apparently most delcare it as   "char"  but it should be   "int."
Why?   Which one is better?
There is no loop involved.

--------------------------------------------------
int(main)void

{     char ch;

      printf("Welcome\n"
           "A)
           "B)
           "C)
           printf("Please enter the letter of your choice\n\n");

      ch=getchar();

      switch (ch)

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 int getchar(); or char getchar();

: Hi, I am trying to figure out if   "ch"   should be  declared as an
: "int"   or   "char".   I tried it both ways, and both ways work.
: Apparently most delcare it as   "char"  but it should be   "int."
: Why?   Which one is better?
: There is no loop involved.

int getchar() is better. Why? you say. On end of input, getchar()
returns a value which is defined in the C standard library as EOF.
This EOF value is usually outside the range of char, but not outside
the range of int.

: --------------------------------------------------
: int(main)void

This produces not one, not two, but three errors.
1) You cannot cast to "main", "main" is not defined yet.
2) You cannot have an identifier called "void", it's a reserved word.
3) This identifier is not a function, so it can't have a body following
it.
I presume you mean int main(void).

: {     char ch;

:       printf("Welcome\n"
:            "A)
:            "B)
:            "C)
:            printf("Please enter the letter of your choice\n\n");

Argh, what are you going to do, fry your compiler? If you want string
constants spanning several lines of source code, either "extend" them
with backslashes:
printf("This is one line \
and this is another!\n");
or make them into several adjacent pieces:
printf("This is one line"
"and this is another!\n");

:       ch=getchar();

This would be better off if ch was an int.

:       switch (ch)

And? Where is the body of this switch statement?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Normal is what everyone else is, and you're not."
   - Dr. Tolian Soran



Sat, 24 Aug 2002 03:00:00 GMT  
 int getchar(); or char getchar();
1.   Sorry, yes, I meant int main (void).

2.   I didn't include body of switch statement because I did not think
this would matter in deciding if ch should be int or char.

3.   I assume even if there is no loop involved, that "int"
would be a better choice?

Thank you.



Quote:

> : Hi, I am trying to figure out if   "ch"   should be  declared as an
> : "int"   or   "char".   I tried it both ways, and both ways work.
> : Apparently most delcare it as   "char"  but it should be   "int."
> : Why?   Which one is better?
> : There is no loop involved.

> int getchar() is better. Why? you say. On end of input, getchar()
> returns a value which is defined in the C standard library as EOF.
> This EOF value is usually outside the range of char, but not outside
> the range of int.

> : --------------------------------------------------
> : int(main)void

> This produces not one, not two, but three errors.
> 1) You cannot cast to "main", "main" is not defined yet.
> 2) You cannot have an identifier called "void", it's a reserved word.
> 3) This identifier is not a function, so it can't have a body
following
> it.
> I presume you mean int main(void).

> : {     char ch;

> :       printf("Welcome\n"
> :            "A)
> :            "B)
> :            "C)
> :            printf("Please enter the letter of your choice\n\n");

> Argh, what are you going to do, fry your compiler? If you want string
> constants spanning several lines of source code, either "extend" them
> with backslashes:
> printf("This is one line \
> and this is another!\n");
> or make them into several adjacent pieces:
> printf("This is one line"
> "and this is another!\n");

> :       ch=getchar();

> This would be better off if ch was an int.

> :       switch (ch)

> And? Where is the body of this switch statement?

> --


---------------------------\

Quote:
> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++
|
> | http://www.helsinki.fi/~palaste       W++ B OP+
|
> \----------------------------------------- Finland rules!
------------/

> "Normal is what everyone else is, and you're not."
>    - Dr. Tolian Soran

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 24 Aug 2002 03:00:00 GMT  
 int getchar(); or char getchar();
Quote:

> Hi, I am trying to figure out if   "ch"   should be  declared as an
> "int"   or   "char".   I tried it both ways, and both ways work.
> Apparently most delcare it as   "char"  but it should be   "int."
> Why?   Which one is better?
> There is no loop involved.

> --------------------------------------------------
> int(main)void
  int main(void)

> {     char ch;

>       printf("Welcome\n"
>            "A)
>            "B)
>            "C)
>            printf("Please enter the letter of your choice\n\n");

>       ch=getchar();

>       switch (ch)

The function getchar() returns an int, primarily in order to return EOF
if there are no more characters in a file.
Now consider your code above. What would happen if the user typed a ^D
(or whatever key combination denotes EOF on your platform?
So use int...and be sure to test the result.

HTH,
--ag

--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
A: Look for a lawyer who speaks Aramaic...about trademark infringement.



Sat, 24 Aug 2002 03:00:00 GMT  
 int getchar(); or char getchar();

Quote:

>Hi, I am trying to figure out if   "ch"   should be  declared as an
>"int"   or   "char".   I tried it both ways, and both ways work.
>Apparently most delcare it as   "char"  but it should be   "int."
>Why?   Which one is better?
>There is no loop involved.

>--------------------------------------------------
>int(main)void

>{     char ch;

>      printf("Welcome\n"
>           "A)
>           "B)
>           "C)
>           printf("Please enter the letter of your choice\n\n");

This is a syntax error. Whats it supposed to do?

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Sat, 24 Aug 2002 03:00:00 GMT  
 int getchar(); or char getchar();
I didn't think the details of
A, B & C were required.  I just
wanted to know whether ch should
be delcared as  int   or  char.

Sorry.


Quote:


> >Hi, I am trying to figure out if   "ch"   should be  declared as an
> >"int"   or   "char".   I tried it both ways, and both ways work.
> >Apparently most delcare it as   "char"  but it should be   "int."
> >Why?   Which one is better?
> >There is no loop involved.

> >--------------------------------------------------
> >int(main)void

> >{     char ch;

> >      printf("Welcome\n"
> >           "A)
> >           "B)
> >           "C)
> >           printf("Please enter the letter of your choice\n\n");

> This is a syntax error. Whats it supposed to do?

> Mark McIntyre

> C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Sent via Deja.com http://www.deja.com/
Before you buy.


Sun, 25 Aug 2002 03:00:00 GMT  
 int getchar(); or char getchar();

: I didn't think the details of
: A, B & C were required.  I just
: wanted to know whether ch should
: be delcared as  int   or  char.

: Sorry.

The previous poster wasn't inquiring the details of A, B & C, but
was pointing out the same thing I pointed out, namely that if you want
a string literal spanning several lines of source code, you MUST either
use trailing \ characters, or adjacent string literals.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Nothing lasts forever - so why not destroy it now?"
   - Quake



Sun, 25 Aug 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. getchar() and EOF, but casting it as (int)

2. getchar and 256 char hangup

3. 1)Testing input fm getchar 2)Char in struct

4. int to char / char to int

5. char to int, int to char...

6. conversion int to char, char to int ?????

7. char *fnpars(const char *fn, int p, int v)

8. Using getchar() instead of fgets or scanf

9. EOF, getchar() and preprocessor portability issues (Re: O

10. Getchar and loop

11. defaulting with getchar problem

12. getch vs getchar vs getc

 

 
Powered by phpBB® Forum Software