Learnem Educational
February 04, 2012, 01:01:53 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: getchar-EOF  (Read 2371 times)
Randy
Newbie
*
Posts: 11


View Profile WWW
« 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
« 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. :wink:
« Last Edit: January 01, 1970, 12:00:00 AM by Anonymous » Logged
Randy
Newbie
*
Posts: 11


View Profile WWW
« 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


View Profile
« 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!"
 :twisted:
« Last Edit: January 01, 1970, 12:00:00 AM by Mr. Cool » Logged
Randy
Newbie
*
Posts: 11


View Profile WWW
« 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
« 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


View Profile WWW
« 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  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!