I am reading SAMS "Learn C in 21 days" and have come upon an example program that I do not quite understand.  It's pretty short, and I was wondering if someone could help me out with it.  Here it is:
#include <stdio.h>
main()
{
int ch;
     
while ((ch = getchar()) != '\n')
putchar(ch);
return 0;
}
First of all, why did they just type main() with no return type specified? That is the first time they have done that in the book and they offer no explanation for it in the text. Second, that if statement in the program above has me confused. I dont quite understand how it is being evaluated. What I thought happens is: ch is assigned the value inputted through the function getchar(). Then, that expression will evaluate to either 0 or 1 to test for truth for the while statement, but that doesnt make sense because then it is being compared to the newline character, which is never going to be 0 or 1. What is going on here?
			
			#include <stdio.h>
main()
{
int ch;
while ((ch = getchar()) != '\n')
putchar(ch);
return 0;
}
First of all, why did they just type main() with no return type specified? That is the first time they have done that in the book and they offer no explanation for it in the text. Second, that if statement in the program above has me confused. I dont quite understand how it is being evaluated. What I thought happens is: ch is assigned the value inputted through the function getchar(). Then, that expression will evaluate to either 0 or 1 to test for truth for the while statement, but that doesnt make sense because then it is being compared to the newline character, which is never going to be 0 or 1. What is going on here?
 
				
		 
			 
 
		 
 
		 
 
		 
 
		 
 
		
 Facebook
Facebook Twitter
Twitter