0

I am in the process of learning the C Programming Language and currently I am exploring the comma operator. I feel that I am slowly wrapping my mind around this operator. I found a good discussion on this operator here:

What does the comma operator , do?

I also found several examples on how this operator can be used:

#include<stdio.h>
int main()
{
   int i;
   i = (1,2,3);
   printf("i:%d\n",i);
   return 0;
}

And

#include<stdio.h>
void main() 
{
   int num1 = 1, num2 = 2;
   int res;
   res = (num1, num2);
   printf("%d", res);
}

However, I recently ran across an unrelated example where the author happened to be using a comma operator and I am confused as to how it is being used in the example.

#include <stdio.h>
#include <stdlib.h>

struct date
{
    int month;
    int day;
    int year;   
};

struct date foo(struct date x)
{
    ++x.day;    
    return x;
}

int main(void)
{
    struct date today = {10, 11, 2004};
    struct date *newDate, foo();    // comma operator?
    return 0;   
}

The thing that is throwing me off is that after the comma operator, it appears that the foo() function is being called; however, the foo() function is declared to take one parameter, specifically of struct date. How is this function being called without any parameters and how does the comma operator fit into this example? Does the comma operator somehow pass the newDate pointer into foo()? Any insight would be appreciated. Cheers.

Community
  • 1
  • 1
Adam Bak
  • 1,269
  • 12
  • 15
  • 2
    That's not a comma operator, that's an _init-declarator-list_. May I suggest you get the basics before you start doing fancy (and often obfuscating) things? And that is a strongly deprecated function declarator. – too honest for this site Mar 13 '17 at 23:38
  • So the comma in the line, "struct date *newDate, foo();" is not being used a comma operator, but as a function declaration? – Adam Bak Mar 13 '17 at 23:43
  • 1
    As others wrote: it is two declarations. As a well-meant warning: do not get into the habit of doing this. It obfuscates the code **and** should generate a warning in modern C (i.e. since ca. 18 years). I'd be suspicious about such code. Don't try learning C from snippets you caught here and there, blogs, videos, etc., but get a good C book. – too honest for this site Mar 13 '17 at 23:47
  • This code is not deprecated; the above commentor is, as usual, speaking about his own personal style as if it were gospel. – M.M Mar 13 '17 at 23:56

1 Answers1

3

There is no comma operator involved in the line:

struct date *newDate, foo();

It merely declares newDate as pointer to struct date and foo as function returning struct date, that does not specify its arguments (K&R old-style function declaration).

The comma serves in this context as punctuator.

The general sentiment is to group function declarations at file scope and do not mix them with objects' declarations.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • Thank you @Grzegorz. The comma was throwing me off, and for some reason I thought that it was being used as an operator. – Adam Bak Mar 13 '17 at 23:50