Why do these programs work, and why do I not get a "semicolon missing" error? With this question i want to ask that when i can skip semicolons. As far as i know semicolon is sentence terminator. Is it correct to write these type of statements where we use comma instead of semicolon. In program1 there's a negation then printing and then getchar() in one line without semicolon and using comma. similarly in program 2 negation,assignment,printf and getchar() all are used. How much line we can write using comma and not using semicolon.
program1:
#include <stdio.h>
int main()
{
int i = 0xAA;
~i, printf("%X\n", i),getchar();
return 0;
}
program 2:
#include <stdio.h>
int main()
{
int i = 0xAA;
i=~i, printf("%X\n", i),getchar();
return 0;
}
Because it is not missing.
It is because the comma is an operator in C. According to the second edition of The C Programming language:
Be aware though, that it also says:
A common example of forgetting this is explained here.
So both programs are correct (though only in the second one the inverted value of
i
is printed).