Learnem Educational
February 04, 2012, 01:01:53 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
:
Home
Help
Search
Login
Register
Learnem Educational
>
Free Ebooks Discussion
>
Programming in C in 7 Days (Free Ebook)
>
getchar-EOF
Pages: [
1
]
« previous
next »
Print
Author
Topic: getchar-EOF (Read 2371 times)
Randy
Newbie
Posts: 11
getchar-EOF
«
on:
August 15, 2002, 07:56:28 PM »
On page 16 of K&R, 2nd ed., we are given the following program:
[code:18jjlwb2]
#include <stdio.h>
main()
{
int c;
c=getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
[/code:18jjlwb2]
I tried to write a program that did the same without looking at the code and I ended up with nearly the same thing, but I used the "char" type instead of the "int" type as they did. The book explained this in the following way:
"We must declare 'c' to be a type big enough to hold any value that 'getchar' returns. We can't use 'char' since 'c' must be big enough to hold EOF in addition to any possible 'char'. Therefore we use 'int'. "
I still don't understand why 'int' must be used instead of 'char'. If 'int' can hold any value, why can't 'char' as well?
«
Last Edit: January 01, 1970, 12:00:00 AM by Randy
»
Logged
Anonymous
Guest
I know the answer...
«
Reply #1 on:
August 16, 2002, 12:35:32 AM »
Quote
I still don't understand why 'int' must be used instead of 'char'. If 'int' can hold any value, why can't 'char' as well?
Quote
Because char can only hold/ display alpha char but int can hol/ PERFORM both char and numerals up to 255.
I hope this helps and I pray I am not wrong about this either. I am really sure about my answer.
«
Last Edit: January 01, 1970, 12:00:00 AM by Anonymous
»
Logged
Randy
Newbie
Posts: 11
(No subject)
«
Reply #2 on:
August 16, 2002, 11:39:14 AM »
OK, thanks, I wasn't aware of that. Makes more sense. :)
«
Last Edit: January 01, 1970, 12:00:00 AM by Randy
»
Logged
Mr. Cool
Newbie
Posts: 11
(No subject)
«
Reply #3 on:
August 16, 2002, 10:36:33 PM »
[quote:3llro6cs]Baton Rouge, LA, USA
[/quote]
Are you attending LSU. I used to live in shreveport, go LSU. I miss mardi gra. Those were party times.
anyway "Hello!"
«
Last Edit: January 01, 1970, 12:00:00 AM by Mr. Cool
»
Logged
Randy
Newbie
Posts: 11
(No subject)
«
Reply #4 on:
August 19, 2002, 12:03:38 PM »
I graduated from LSU back in '96. I am an analyst now, but have never learned C (my major was math) and thought it was well-past time I learned how to code in a "real" UNIX language so am trying now.
«
Last Edit: January 01, 1970, 12:00:00 AM by Randy
»
Logged
Anonymous
Guest
Cool!
«
Reply #5 on:
August 21, 2002, 02:07:30 PM »
[quote:11bryrmz]I am an analyst now, but have never learned C (my major was math) [/quote]
Thats' cool, Randy.
Would it be alright if I have any questions about math that I can ask you.
If you have any questions about C or computers. Don't be afraud to ask.
By the way GO TIGERS!
«
Last Edit: January 01, 1970, 12:00:00 AM by Anonymous
»
Logged
Randy
Newbie
Posts: 11
(No subject)
«
Reply #6 on:
August 22, 2002, 01:52:55 PM »
Sure, be my guest, I will answer anything if I can. :)
«
Last Edit: January 01, 1970, 12:00:00 AM by Randy
»
Logged
Pages: [
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Free Ebooks Discussion
-----------------------------
=> Web Design in 7 days (Free ebook)
=> Programming in C in 7 Days (Free Ebook)
=> ASP Web Programming in 7 days
Loading...