why I can't separate c=getchar() from while body

This is a discussion on why I can't separate c=getchar() from while body within the C Programming forums, part of the General Programming Boards category; Hello, from K&R, separating c=getchar() leads the program to ignore CTRL-Z Code: #include <stdio.h> int main(void) { int c, pc; ...

  1. #1
    fsx
    fsx is offline
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29

    why I can't separate c=getchar() from while body

    Hello, from K&R, separating c=getchar() leads the program to ignore CTRL-Z

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int c, pc;
    
    	while(getchar != EOF)
    	{	
    		c = getchar();
    		if((c == ' ') && (pc != c))
    			putchar(c);
    		else if (c != ' ')
    			putchar(c);
    		pc = c;
    	}
    	return 0;
    }
    I would like to understand why, the compiler says: warning C4047: '!=' : 'int (__cdecl *)(void)' differs in levels of indirection from 'int' and I don't know what this means. Maybe the getchar() function returns an INT? I still lack the knowledge to understand what type VOID is.
    Thank you in advance.

    P.S. sorry I just found out that I omitted the () in getchar. Sorry and thanks anyway...
    Last edited by fsx; 04-21-2009 at 03:20 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, getchar() does return an int, which is important for detecting EOF (which is typically set as -1).

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    In C, a function name without () after it is a pointer to the function. __cdecl just identifies how arguments get passed back and forth when the function is called - you can generally ignore this. So "int (__cdecl *)(void)" is a pointer to a function taking no arguments (void) and retuning an int, which is exactly what getchar() is.

    Also, did I read correctly that you're calling getchar() twice per loop? Do you want to ignore every other character of input?

  4. #4
    fsx
    fsx is offline
    Registered User fsx's Avatar
    Join Date
    Apr 2009
    Posts
    29
    Quote Originally Posted by KCfromNC View Post
    Also, did I read correctly that you're calling getchar() twice per loop? Do you want to ignore every other character of input?
    Hey that's exactly what it happens sometimes... why does it ignores every other character? Shouldn't it print everything with getchar()? Sorry if the question seems silly but still I don't understand the underlying mechanics well...

    Best regards and thanks in advance

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    when u input a number of chars then each call to getchar() pulls the subsequnt char from the keyboard buffer.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by fsx View Post
    Hey that's exactly what it happens sometimes... why does it ignores every other character? Shouldn't it print everything with getchar()? Sorry if the question seems silly but still I don't understand the underlying mechanics well...

    Best regards and thanks in advance
    Look at it this way :
    Code:
    	
    while((c2 = getchar()) != EOF)
    {	
    	c = getchar();
    	if((c == ' ') && (pc != c))
    		putchar(c);
    	else if (c != ' ')
    		putchar(c);
    	pc = c;
    }
    See how the code never does anything with c2? This is exactly what your code is doing, you're just not explicitly assigning the result of the first getchar() to a variable. The getchar() call still reads a character from the input and tries to return it to you - your code just ignores it other than to check for EOF. But just because you don't save it doesn't mean that the compiler somehow knows to put the character back and wait for you to call the function again to get the character, and that this time you mean it.

    You'll need to take one of these calls to getchar() out of the loop to make things work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. html header and html body
    By Checker1977 in forum Tech Board
    Replies: 18
    Last Post: 11-23-2008, 05:52 AM
  2. separate integer into individual units
    By manzoor in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2008, 11:03 AM
  3. Enum and body parts
    By Rahtgaz in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2006, 05:05 PM
  4. Using strtok on separate functions
    By Thumper333 in forum C Programming
    Replies: 2
    Last Post: 10-24-2004, 03:19 PM
  5. scrolling text in the body
    By task in forum C Programming
    Replies: 7
    Last Post: 05-18-2002, 11:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21