-9

I am expecting error in the following code but after running output is 5 can any one tell why.

#include <stdio.h>
void main()
{
    int k = m();
    printf("%d", k);
}
void m()
{
    printf("hello");
}

because return type is void but when we declare it above main then it is giving error.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Aman
  • 9
  • 7
  • 3
    It's just Undefined Behaviour, which is all that needs to be said. – Paul R Oct 30 '14 at 21:32
  • keep in mind you are coding C. – Jason Hu Oct 30 '14 at 21:32
  • 1
    To the OP, be careful when asking C questions here. You must do your homework before, or else you will get down-voted into oblivion. – Ryan Oct 30 '14 at 21:33
  • 1
    I think the OP is being slaughtered, I am yet to see a good comment on what is wrong with his question. – user3885927 Oct 30 '14 at 21:37
  • 2
    Not sure why response is so unnecessarily harsh. Yes, undefined behavior. Does OP know what is it? Does he/she know why? What should he/she keep in mind regarding coding in C? – Chris Oct 30 '14 at 21:37
  • Here, check this out: http://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c Without a prototype the compiler will infer parameters allowing you to call it an yield undefined behaviour. I hazzard a guess that if you put void m(void); at the top before the main function you would go back to an error. – Chris Oct 30 '14 at 21:41
  • yaa i know that it is not a way to declare a function but still on running it is giving output – Aman Oct 30 '14 at 21:42
  • 1
    most of the downvotes are probably for `void main` – M.M Oct 30 '14 at 21:46
  • Would not be surprised. There seems to be a disturbing down-vote trend going on though.. disheartening. – Chris Oct 30 '14 at 21:47
  • 1
    It is because there is a certain level of quality that is expected of C++ question. This was way below the threshold. – Ryan Oct 30 '14 at 22:01
  • Definitely below quality threshold of SW developer with 20 years exp. What really is the quality threshold of a student? A hobbyist? An enthusiastic teen or tween? Are the later to really be held to the same expectation as us 'long-in-the-tooth' professional developers? – Chris Oct 30 '14 at 22:25

1 Answers1

6

This program is invalid (further explanation of why follows below).

In C89 when you call m(), or in C99 when you start the program at all, undefined behaviour is caused. This means anything can happen. To put it another way, the compiler only has to cope with correct programs; and if you make a mistake then you can get junk results happening (this is not just limited to invalid output).

In C89 this code actually does not require any compiler diagnostics1. It just causes undefined behaviour. (Helpful compilers may give you a warning anyway.)

This is because the line int k = m(); causes implicit declaration of a function m() returning int and taking unspecified arguments. However, the actual function body of m returns void. This means that if m is ever called, then undefined behaviour is triggered.

In practical terms, what you may be experiencing is that the main function looks in a particular register for the value returned from m, but the function body of m did not set that register, and it happened to contain 5 by chance. Do not rely on this behaviour.

Since you did not use function prototypes, the compiler is not required to diagnose this error. If you did use a function prototype (e.g. void m(void)), or if you move void m() to be before main() then the compiler is required to give a diagnostic. (If you then go on to run your program anyway , ignoring this message, then the entire behaviour of the program is undefined).

In C99 , implicit declaration of functions was removed, and the line int k = m(); must give a diagnostic.


1 requires a diagnostic means that the code has an error according to Standard C, and the compiler must report a message to you. The compiler may choose to categorize this message as "warning" and produce an executable anyway, however the code is still incorrect in this scenario and should be fixed.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • The main problem isn't the order of declaration. – Etheryte Oct 30 '14 at 21:34
  • 1
    @Nit well - there's two major problems, I'll fix my post – M.M Oct 30 '14 at 21:35
  • 1
    but it is giving output 5 that's why i am surprise .it should give error – Aman Oct 30 '14 at 21:36
  • 2
    No, it does not need to give an error. The result is undefined. Anything can happen. In this case it so happens that the value 5 is what you get (this is in fact the return value from printf, which is left in the return register when returning from m()) – perh Oct 30 '14 at 21:44
  • OP says *"it is giving output 5 ... it should give error".* There is no run-time error because although the value assigned to `k` is undefined, there are no illegal values for an integer, and so `printf("%d", k)` cannot fail either. Compare this with using `*k`, a pointer. The assignment of any value (an address) will again be legal, but when you try to access the memory at that address with `printf("%d", *k)` that is when it can cause an error by trying to access memory that is not available to the program. – Weather Vane Oct 31 '14 at 00:09
  • @WeatherVane there may be illegal values for an integer, and programs with undefined behaviour may fail. What you are describing is specific to particular system types – M.M Oct 31 '14 at 00:39
  • @Matt McNabb this is a C program. In C an integer may be any value that the allotted storage space can hold. No such value can cause a *direct* error. That was my point. – Weather Vane Oct 31 '14 at 17:36
  • @WeatherVane there can be trap representations for integer (e.g. negative zero in a one's complement system). Those are rare these days . – M.M Oct 31 '14 at 19:34
  • That is for when an out of range value from of a different type is assigned. No value was assigned - it was undefined by the void function but assumed by the calling function. The OP asked why there was not a (runtime) error, and I added something that had not been said. In standard C no value of an `int` that has an undefined value will cause a *direct* runtime error, so please don't confuse it. – Weather Vane Oct 31 '14 at 19:52
  • @WeatherVane In standard C: when it is undefined behaviour, anything can happen . You added things that are not part of standard C ("the value assigned to k", "undefined value", and "there are no illegal values for an integer"). – M.M Oct 31 '14 at 19:57
  • No. In C an undefined `int` is no different from random `int`. It simply cannot be an invalid value that creates an error if you just print it. It can only cause an error if you try to use it to reference something else. That was why I stressed *direct* runtime error. – Weather Vane Oct 31 '14 at 20:15
  • @WeatherVane In standard C there is no such thing as "undefined int", "random int" (unless you mean the result of `rand()`), or "runtime error" or "direct runtime error". There is *indeterminate value*, which can cause *undefined behaviour* if you just print it. When UB occurs, anything can happen. OP's program could cause his CPU to explode. OP's program doesn't feature any indeterminate values (an example of that would be `int x;`). IDK what you are trying to stress when you say "direct". – M.M Oct 31 '14 at 20:19
  • Rubbish. You know exactly what I mean. – Weather Vane Oct 31 '14 at 20:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64065/discussion-between-weather-vane-and-matt-mcnabb). – Weather Vane Oct 31 '14 at 20:48