Thread: Comma operator

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    Comma operator

    Hello all,

    Code:
    #include <stdio.h>
    int main()
    {
        printf("How is "), printf("this "), printf("possible."), getchar();
        return 0;
    }
    This code seems to work fine. So the meaning of the 'comma' operator seems to be different. Could anyone give me definitation of 'comma' operator in C? This seems to be like programming Prolog

    Thanks

    Harish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It executes all the statements and returns the right-most statement, basically.
    In your statement, what would be returned is the value of getchar(). That is, if you wanted to save it. Otherwise it acts like different statements separated with ;.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It is particularly useful in for loops when you are trying to iterate or test for two things at the same time:

    Code:
    int i, count;
    for(i = 0, count =5;i < MAX;i++,count++);
    Other than that I think there are probably a couple of situations when you could be legitimately using it, but it generally just obfuscates your code.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. classes and objects
    By GoldenBoy in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2010, 07:28 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM