3
#include<stdio.h>
int slogan();
int main()
{
    slogan(slogan());
    return 0;
}
int slogan()
{
    printf("onlyme\n");
}

My doubt is, the slogan function has no argument list in its prototype, then how come it accepts the function call as its argument?

yizzlez
  • 8,757
  • 4
  • 29
  • 44
Ayesha Gupta
  • 145
  • 4
  • 13
  • 1
    There is no function within function. The code is identical to `a = slogan(); slogan(a)` – Val May 24 '14 at 12:26

3 Answers3

8

In c the empty parameter list does not mean function that takes no arguments. It means function with unspecified number of arguments

To declare a function that takes no arguments write :

int func(void);
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • and what if the function return type is void?? in that case it gives an error..reason?? – Ayesha Gupta May 24 '14 at 12:30
  • Expanding on this: the `int slogan();` is not a *prototype*, so the compiler does not do any argument checking. You have to personally verify that what you call it with is compatible with how the function definition actually looks. If there is a mismatch (as in OP's code) the behaviour is undefined. – M.M May 24 '14 at 12:31
  • @AyeshaGupta it is not permitted to work with `void` values. Trying to use the return value of a function that returns `void` causes a compiler error. (Some compilers have an extension where you can pass a `void` value to a function that takes no parameters, or return a `void` value, but you cannot rely on this behaviour in general) – M.M May 24 '14 at 12:32
  • i didnot understand.. #include void slogan(); void message(); int main() { slogan(message()); return 0; } void slogan() { printf("only me\n"); } void message() { printf("in message\n"); } in this case it gives an error..why?? – Ayesha Gupta May 24 '14 at 12:34
  • 1
    @AyeshaGupta `message()` returns `void`, so you cannot have any code that uses the return value . Trying to use the return value as a function parameter counts as using the return value. – M.M May 24 '14 at 12:35
  • @AyeshaGupta what the other guys said. Also consider that void is not a type per se (you cannot declare a variable of type void) and so cannot be used in a context where a variable is expected (roughly speaking, there's much more to it) – Nikos Athanasiou May 24 '14 at 12:36
  • @Nikos `void` is certainly a type. You can't declare variables of it because the language rules say you can't. It's called an *incomplete type*; other incomplete types include `struct S;` – M.M May 24 '14 at 12:37
  • @MattMcNabb That why I say "not a type **per se**" and "there's much more to it". Other incomplete types are `double a[]` but laying down a list wouldn't help OP clarify the question at hand. After providing the initial arguments one can research if so pleases – Nikos Athanasiou May 24 '14 at 12:42
  • 1
    My point is that incomplete types are still types (and so they are types per se). – M.M May 24 '14 at 12:50
  • @MattMcNabb Ok, I'll justify you being thorough on this. I'll try to do the same just in case oversimplification confused the OP : There are 3 kinds of types : **object types, function types and incomplete types**. `void` is an incomplete type and up to C99 was not an object type. After C11 incomplete types became a subset of object types. – Nikos Athanasiou May 24 '14 at 13:27
6

Because in C,

int slogan();

declares a function without saying anything about its arguments. This is not a prototype declaration at all, it's an old-style K&R declaration. The prototype declaration for a function taking to arguments is

int slogan(void);

The former form exists for backward compability with pre-1989 C, when you couldn't provide argument information in the prototype at all.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

Look at First answer here(and second) The first answer will give you an Accurate explanation of a functions declaration

Section 6.11.6 Function declarators(K&R C)

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

Community
  • 1
  • 1
YonBruchim
  • 101
  • 6