34

Why does this program below not show any error ?

int main (void) {
    "ANGUS";
    1;
    3.14;
    return 0;
}
8
  • 12
    They are just statements without effect and have not broken any rules.
    – Stan
    Commented Aug 24, 2011 at 9:01
  • 2
    +1 for a well posed question though. Commented Aug 24, 2011 at 9:07
  • 3
    Side question: is memory allocated for the string "Harsha"? Commented Aug 24, 2011 at 9:56
  • I've wondered the same myself. Since it does nothing, it is almost certainly a typo in the code, so why doesn't the compiler complain. One I've seen before was of the form a;b++; where the middle semicolon was meant to be a dot. It happened that the b variable existed independantly, as well as a member of the structure. No error but the program didn't behave as expected!
    – asc99c
    Commented Aug 24, 2011 at 9:58
  • 2
    @Chris: even with the most basic optimization options, the result of compiling a source file with or without the "Harsha"; line is exactly the same. With no optimization whatsoever, the construct follows the rules for string literals: "6.4.5/5 ... array of static storage duration" and there will be 7 bytes allocated for it.
    – pmg
    Commented Aug 24, 2011 at 10:12

7 Answers 7

27

Each of those statements are expressions that evaluate to a value, which is then discarded. Compare it to if you called a function that returned an int, a char or a float, without using the return value. That's also an expression that evaluates to a value.

It is not uncommon to have functions that return values that the caller may or may not be interested in, like for example where printf("%d", 9001) returns the number of characters printed. A caller can use the number returned, or they can just ignore it.

If the compiler complained whenever you ignored a return value, you'd have a very noisy build log. Some compilers however diagnose (if sufficient warning flags are enabled) such pointless side-effect-less uses of literals and variables.

3
  • Add an example and get a +1 from me ... oh, never mind: +1 anyway ... but add the example :)
    – pmg
    Commented Aug 24, 2011 at 9:14
  • 11
    printf("Hello, world!\n"); returns the value 14
    – pmg
    Commented Aug 24, 2011 at 9:16
  • 2
    @pmg, I like how you added your own example two minutes later... :p
    – johnny
    Commented Aug 30, 2011 at 18:21
16

It's perfectly valid for a statement in C to be just a value that gets discarded. Most people don't realise that this is exactly what happens when they code up things like:

x++;
printf ("Hello, world\n");

The former is actually an expression which just happens to have the side effect of incrementing the variable after "returning" it.

The latter function call actually returns a value (the number of characters printed) which is also discarded.

From a certain viewpoint, that is no different from the statements:

42;
3 * 12;

other than the fact that they have no side effects which make them useful.

In fact, even x = 1 is an expression where the result is discarded. It is this that makes x = y = z = 0 possible since this is effectively:

(x = (y = (z = 1)));

All of this is detailed in C99, section 6.8.3 Expression and null statements which says, in part:

The expression in an expression statement is evaluated as a void expression for its side effects, such as assignments, and function calls which have side effects.

9

It is perfectly valid for a statement to consist of just an expression. Nothing is achieved by doing so in your examples, but it's perfectly valid all the same.

5

Since in c it's a valid statement but when you use java it will give you compile time error.

3
  • 9
    In English, u is spelled you Commented Aug 24, 2011 at 9:03
  • 1
    thanks for the edit... i will keep this in mind no shortcuts specially for "you" next time...
    – amod
    Commented Aug 24, 2011 at 9:24
  • 1
    +1, because it bears mentioning that while this is valid C syntax (as everyone here is asserting ad nauseum), it is not valid syntax in many other languages (i.e. Java & C#).
    – Kirk Woll
    Commented Aug 30, 2011 at 20:39
4

Why should it? The first three lines in you main don't do anything and return 0; is a valid expression.

If you use gcc to compile the program try to enable all warnings (with -Wall parameter). This would print warning: statement with no effect [-Wunused-value]

3
  • 1
    In gcc, -Wall does not enable all warnings: it enables "warnings about constructions that some users consider questionable" (from gcc documentation)
    – pmg
    Commented Aug 24, 2011 at 9:13
  • 1
    @Mr.32 pmg is right, -Wall does not enable all warnings but only "warnings about constructions that some users consider questionable" Commented Aug 24, 2011 at 10:42
  • return 0; is a statemwnt, not an ezpression. Commented Sep 21, 2011 at 7:48
2

Because it is valid. A string or a number is a valid expression and an expression followed by semi-colon is a statement (also informally known as instruction).

3
  • 1
    by comma? is the ";" symbol named comma?
    – osgx
    Commented Aug 24, 2011 at 9:03
  • and not an instruction but "statement" (according to ISO standard )
    – osgx
    Commented Aug 24, 2011 at 9:28
  • It's called a "statement expression". Commented Sep 21, 2011 at 7:47
0

It is also valid in C++ and it does have a hackish use in Visual Studio where you wish to "Find All References" for an enum, you can type the enum on a line by itself terminated with a semicolon and you can use the context menu to find the references.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.