Thread: question in getint

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    8

    question in getint

    Hello all. I am trying to figure out how ? affects the code.
    Can someone please help?
    Thank you in advance.

    Code:
    #include <stdio.h>
    
    #define BUFSIZE 100
    
    char buf[BUFSIZE];      /* buffer for ungetch */
    int bufp = 0;           /* next free position in buf */
    
    int getch(void)         /* get a (possibly pushed-back) character */
    {
            return (bufp > 0) ? buf[--bufp] : getchar(); // ? does what??? 
    }
    
    void ungetch(int c)     /* push character back on input */
    {
            if (bufp >= BUFSIZE)
                    printf("ungetch: too many characters\n");
            else
                    buf[bufp++] = c;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    the ? is a short form of an if

    <condition> ? true : false;

    so say I had
    Code:
    5 > 0 ? printf("Wee") : printf("ohhh");
    8 < 0 ? printf("Ahhh") : printf("Hehe");
    It would print Wee and then Hehe.

    You can also use it for assignments
    Code:
    x = y > z ? y : z;
    Which will assign to x the higher value between y and z.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    8
    Thank you very much for your quick response!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question in getint #2
    By jongmin in forum C Programming
    Replies: 5
    Last Post: 03-10-2004, 01:10 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM