Can I use a function in variable declarations in C? int f (), i;

From Understanding Unix Programming, Chapter 1.6, more01.cExample:

int see_more(), reply;

I tried a similar code:

#include <stdio.h>

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

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

There is no error in the log, but not Helloon the console. Can anyone explain this?

+4
source share
3 answers

It will be just fine. But all you do is declare a function. This is the same as adding an ad (not a prototype) at the top level.

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

You can call a function in a declaration if it is part of an 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 usually, to call a function, you simply place the call in the expression of the expression (without a declaration).

#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");
};

, . C99 ( "" ) , .

IIRC splint .


, . , , . , , .

, , args (char, short, int) int, float double. #include <stdarg.h> ( printf), , .

, . ( ), , , . (...), . , void (*fp)();.

+4

, .

:

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

  hey();
}
+1

int hey() . void hey().

-1

All Articles