8

The book that i am currently reading states that when you declare a function in c that accepts no arguments, but you do not use the keyword void "the function call can pass any arguments it wants". so i attempted this.

int number();

int main(void)
{
    int x =1;
    printf("%d",number(x));
}

int number()
{
    return x;
}

but it did not compile??? how does this work?

FutureSci
  • 1,672
  • 15
  • 25
  • Where is `number()` getting `x` from precisely ? Related to your actual question about differences in C between `func(void)` and `func()` see [this q&a](http://stackoverflow.com/questions/5929711/c-function-with-no-parameters-behavior?lq=1). – WhozCraig Aug 11 '13 at 00:00
  • What book is this? I'm guessing that the book is either just wrong, or-more reasonably-outdated. – Undefined Aug 11 '13 at 00:43

5 Answers5

11

This is an obsolescent feature1 from before C was standardized, decades ago.

Never use it.

In ancient C, a decade before you were born, you could declare a function with no parameters. When you called it, the compiler would promote the parameters to default types and pass them to the function. Essentially, the compiler figured out the parameter declarations from the arguments in the call.

The function should still be defined with parameters, and they should match the way the function is called.


1 “Feature” is the wrong word. It was the way things were done at the time, since better ways were not yet widely developed. In a new programming language, this characteristic would be considered a deficiency.

Eric Postpischil
  • 141,624
  • 10
  • 138
  • 247
5

In support of Eric Postpischil's answer I would like to quote from the C11 standard (6.11 Future language directions)

6.11.6 Function declarators

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

and

6.11.7 Function definitions

The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.

Emphasis is mine :)

0decimal0
  • 3,657
  • 1
  • 20
  • 37
1

The issue here is that your function number has no knowledge of x.
That is, when you have the function number return x, it doesn't have any x in scope to return, and so this is a compile error. If, instead, you wrote:

int number() {
    return 5;
}

it would compile fine.

qaphla
  • 4,527
  • 3
  • 17
  • 29
  • I guess what you are saying is that you if you pass arguments to such a function, you cannot access them inside the function? – FutureSci Aug 11 '13 at 00:03
  • Yes, exactly. I can't see much use for this, but I suppose if you had an array of function pointers, most of which took a specific set of parameters, and one of which took none, you could declare that one to take () instead of (void) to be able to iterate over that array with the same parameters each time. – qaphla Aug 11 '13 at 00:09
0

Well, if you want pass something to it without limit , you may need a a variable argument list.Here is an example:

  #include <stdio.h>
  #include <stdarg.h>
  int number(int , ... );

  int main(void)
  {
       int x =1;
       printf("%d",number(1,x));
  }
  int number(int n, ... )
  {
          va_list         ap;
          va_start(ap,n);
          int x = va_arg(ap,int);
          va_end(ap);
          return x;
  }

Or , if you just want to pass x but not use it.

 #include <stdio.h>
  int number();

  int main(void)
  {
      int x =1;
      printf("%d",number(x));
  }

  int number()
  {
     int x = 1;
     return x;
  }

It can compile and work. When you declare int number (); in C , you mean this function can be given unspecial type . however , you can't use it.

Lidong Guo
  • 2,627
  • 2
  • 14
  • 27
-2

Well I edited the whole answer after finding the answer.

What you want to do is number(void) not main(void). That's what you wanted and that will print any variable as a integer. If you pass the char "F" int number(void) will return the number 70, the int form of "F". In code:

int number(void e);

void main(){
    char C = 'F';
    printf("%d",number(C));
};

int number(void e){
    return e;
};

Note:

You must always pass an argument if you want a variable that's out of scope.

Jader J Rivera
  • 409
  • 2
  • 6
  • 13
  • I know about functions and parameters, my question is intentionally to deal with passing data to functions with no parameter list specifed. So NO, the number() should NOT have a type inside within the scope of this question. – FutureSci Aug 11 '13 at 00:07
  • @worlboss Then that's impossible to do my friend. Remember the computer is "dumb". You create the rules he will follow absolutely and unless you state the function has a parameter the program won't use it. In fact by calling a function with a parameter type that was not previously stated this will crash the program and in debug it wouldn't compile (if I'm correct) because the computer wouldn't know "what to do" (that's why you're here). – Jader J Rivera Aug 11 '13 at 00:11
  • Also, it's common to explicitly type main, either as main(void) or as main(char** argv, int argc). main() isn't any less correct, but nor is it any more. – qaphla Aug 11 '13 at 00:11
  • ohh!! Wait, I just read the question carefully. You're supposed to do number(void) not main(void). That's what you wanted and that will print any variable as a integer. If you pass the char "F" int number(void) will return the number 70, the int form of "F". – Jader J Rivera Aug 11 '13 at 00:15
  • @qaphla Yeah that's true, but I just find beauty in void main(){}; Not necessary but yeah. – Jader J Rivera Aug 11 '13 at 00:16
  • @JaderJRivera: OP explicitly states: "when you declare a function in c that accepts no arguments, _but you do not use the keyword void_ 'the function call can pass any arguments it wants'. – Undefined Aug 11 '13 at 00:42
  • @JakeWilson My bad that's true, but it seems this method was used way before C had a standard and void was yet to be invented. This type of code is certainly not used in our days and was rightfully removed in C99 (A'm I correct??). You can find more of the answer in [this other question](http://stackoverflow.com/questions/13950642/why-does-a-function-with-no-parameters-compared-to-the-actual-function-definiti) – Jader J Rivera Aug 11 '13 at 01:00
  • 2
    (1) This does not compile; `void e` is not a valid parameter declaration in a function definition. (2) `main` should be declared `int main(void)` or `int main(int argc, char *argv[])` or equivalent, not `int main()`. (3) “F” is not guaranteed to be 70. – Eric Postpischil Aug 11 '13 at 01:01