I was editing the source code of a program I had written few days back, and observed an interesting thing.
I had below two statements:
newNode->data = 5
and
newNode->next = NUll
And there was a comma (,) instead of a semi-colon (;) separating above two statements. I was surprised because, I always thought this should result in an error.
Below, I have written a short C program to show what I mean to ask.
#include<stdio.h>
#include<stdlib.h>
/*node structure definition*/
struct node
{
int data;
struct node *next;
};
/*----------main()------------*/
int main(int argc, char *argv[])
{
struct node *newNode = NULL;
newNode = malloc(sizeof(struct node));
/*Below is the interesting statement*/
/*LABEL: QUESTION HERE*/
newNode->data = 5,
newNode->next = NULL;
printf("data: %d\n",newNode->data);
while(newNode->next != NULL)
{
printf("Not Null\n");
}
return 0;
}
Please see below compilation and sample run of the above program.
Lunix $ gcc -Wall testComma.c -o testComma
Lunix $ ./testComma
data: 5
Lunix $
As you see, the program compiles and run with no issues.
Using comma (,) instead of semi-colon (;) should not result in an error here ? Why ?
I thought I knew what C statements are but looks like I don't! Could someone explain the reason why there is no error in this case?