4

From Understanding Unix Programming, chapter 1.6, more01.c example:

int see_more(), reply;

I tried some similar code:

#include <stdio.h>

int main()
{
    int hey(), reply;
    return 0;
}

int hey()
{
    printf("Hello");
};

No error in log, but no Hello on console. Can anybody explain this?

Vicky
  • 12,934
  • 4
  • 46
  • 54
ggaaooppeenngg
  • 1,323
  • 3
  • 17
  • 27
  • Did you try to put a protoype after your include ? int hey(); – ZazOufUmI Sep 27 '13 at 07:44
  • This is incorrect or at least very unusual C syntax. I wouldn't rely on whatever the compiler decides to do with it. – Tobia Sep 27 '13 at 07:45
  • 3
    @Tobia Just because you have never seen it does not imply that the C standard does not unambiguously define what it means. – Pascal Cuoq Sep 27 '13 at 07:47
  • @PascalCuoq very well, so where does it allow function prototypes to be declared inside a function body and intermingled with variable declarations? – Tobia Sep 27 '13 at 13:04
  • @Tobia C99TC3 6.8.2 and 6.7:1 are relevant. Make it a question if you want to see someone give more detail. – Pascal Cuoq Sep 27 '13 at 13:24

3 Answers3

4

This will compile just fine. But all you're doing is declaring the function. This is the same as adding a (non-prototype) declaration at the top level.

int hey( );
//      ^ empty parens means it's not a prototype

You can call a function in a declaration if it's part of the initializer.

#include <stdio.h>

int main()
{
    int reply=hey();
    //        ^ here the function is called, even though this is a declaration,
    //          because the value is needed.
    return 0;
}
int hey(){
    return printf("Hello");
    // a function returning `int` ought to `return ` an int!
};

But normally to call a function you just place the call in a (non-declaration) expression statement.

#include <stdio.h>

int main()
{
    int reply; // declaring a variable
    int hey(); // declaring a function
    (void) hey();     // function call, casting return value to (void)
    return 0;
}
int hey(){
    return printf("Hello");
};

There is a restriction in some earlier compilers that only the last declaration can contain a function call. C99 (and most "modern" compilers) have relaxed this restriction and function calls can now be used in initializers with impunity.

IIRC splint the syntax-checker has this same restriction on function calls in initializers.


It may be considered bad style, but it's not necessarily incorrect to call a function without a prototype. To be sure, it removes the compiler's ability to check that the call make sense from a type point-of-view. But all you really have to do is don't screw it up.

Non-prototyped functions will default to the standard calling conventions, which means all integer args (char, short, int) promote to int and all float args promote to double. These promotions also apply to variadic functions using #include <stdarg.h> (and our beloved printf), so I consider it very useful to know how a non-prototyped function will be called.

I've got some "don't screw it up" code here that calls non-prototyped functions through a function-pointer. It all works and conforms to the standard (near as I can figure), but I have no idea how to prototype that function-pointer which may point to one of many stereotypical patterns. It wouldn't be correct to use a variadic notation (...), because it's not the same thing. There's just no appropriate way to prototype it, so the pointer is just declared void (*fp)();.

Community
  • 1
  • 1
luser droog
  • 18,988
  • 3
  • 53
  • 105
  • 3
    where's the prototype ? user2822466 seems to be new to C, so he may not have understood why hey() is never called. – lucasg Sep 27 '13 at 07:52
1

you are just declaring the function there, not calling it.

Like this:

int main()
{
  extern int hey(); // there is a function "hey" somewhere

  hey();
}
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
-1

int hey() method doesn't return any value. Try making it void hey().

Manikandan Sigamani
  • 1,964
  • 1
  • 15
  • 28
  • That's true (http://stackoverflow.com/questions/10079089/implicit-int-return-value-of-c-function), but it's not related to the question since the function is never called. – Danstahr Sep 27 '13 at 07:55