LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2003, 03:18 PM   #1
zombi3
LQ Newbie
 
Registered: Dec 2003
Distribution: Slackware
Posts: 8

Rep: Reputation: 0
Noob question about putchar and getchar


I was wondering why do i have to declare c=getchar() within the while loop?

Quote:
#include <stdio.h>
main()
{
int c;
c=getchar(); /*declare c to getchar() for character inputs*/
while(c != EOF){
putchar(c);
c=getchar();
}
}
 
Old 12-20-2003, 04:32 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The program works this way:
First, c=getchar() gets first character. Then the loop starts (checks if the char is EOF). In the loop, the char is printed and then next one is read. Then the loop ends and the expression in 'while' is calculated again. If the char is EOF, the loop ends, if it isn't - it continues (printed, then next char).
 
Old 12-20-2003, 04:38 PM   #3
zombi3
LQ Newbie
 
Registered: Dec 2003
Distribution: Slackware
Posts: 8

Original Poster
Rep: Reputation: 0
OOO okay that clears things up :P thanks alot.Thats why when i take out c=getchar() out of the loop; the loop doesn't end.

Last edited by zombi3; 12-20-2003 at 04:41 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
noob with a noob question phoonerorlater Linux - Newbie 2 09-29-2004 03:43 PM
getchar() question h/w Programming 3 12-23-2003 04:53 PM
Complete noob question from a noob noob_hampster Linux - Software 2 09-04-2003 12:03 AM
putchar and putc are not working .... purpleburple Programming 4 07-04-2002 03:28 PM
Simple question about putchar ... in C purpleburple Programming 1 06-30-2002 04:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration