Home > Software design >  I'm having problems with a “too few arguments to function” error in my c program
I'm having problems with a “too few arguments to function” error in my c program

Time:11-27

I typed this program from Boris Allen's book C programming: principles & practice on pg18 using Dev-C 5.11, its supposed to produce the following output text:

Size of whoCount is 2
Size of newCount is 2
Size of Printf() is 2

I don't know how to fix this error.

error: too few arguments to function 'printf'.

(The compiler also mentions something with a function in 'main.)

Here is the program:

#include <stdio.h>

main()
{
    int whoCount, newCount;
    printf("Size of whoCount is %d\n",
      sizeof(whoCount));
    printf("Size of newCount is %d\n",
      sizeof(newCount));
    printf("Size of printf() is %d\n",
      sizeof(printf()));
}

It's supposed to work but I don't know why it does not. It's an example program from the book C programming: principles & practice on pg18. The previous examples worked flawlessly but I'm stuck with this one giving me an error: too few arguments to function 'printf'. I am new to programming and I don't know how to fix it so any help will be greatly appreciated.

CodePudding user response:

printf() the compiler thinks that you want to call printf and zero parameters is definitely wrong.

  • sizeof(&printf) gives the size of the function pointer
  • sizeof(printf("hello")) gives the size of the function return type
  • sizeof(printf) is invalid in standard C (it violates 6.5.3.4) but many compilers support it as an extension.

Your code invokes undefined behaviour as you use the incorrect format to print the size_t type. Also your main function definition is wrong. function main must return int

int main(void)
{
    int whoCount, newCount;
    printf("Size of whoCount is %zu\n",
      sizeof(whoCount));
    printf("Size of newCount is %zu\n",
      sizeof(newCount));
    printf("Size of printf is %zu\n",
      sizeof(printf));
    printf("Size of &printf is %zu\n",
      sizeof(&printf));
    printf("Size of printf(\"hello\") is %zu\n",
      sizeof(printf("hello")));
}

https://godbolt.org/z/nY1obxEP8

printf in sizeof(printf("hello")) is not called and sizeof only determines the type of this expression.

It is not possible to get the size of the printfs code using sizeof operator.

CodePudding user response:

For starters the function main without parameters shall be declared like

int main( void )

From the C Stanfdard (5.1.2.2.1 Program startup)

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

The type of an expression with the sizeof operator is size_t. So to output the value of such an expression you have to use the conversion specifier zu instead of d as for example

printf("Size of whoCount is %zu\n",
  sizeof(whoCount));

To write a call of printf you have to specify at least one argument even if the expression with the call of printf is not evaluated as for example

printf("Size of printf() is %zu\n",
  sizeof(printf( "Hello World!")));

The function is declared like

int printf(const char * restrict format, ...);

Pay attention to that in this call

printf("Size of printf() is %zu\n",
  sizeof(printf( "Hello World!")));

there will be in fact outputted the value of the expression sizeof( int ) because the return type of the function printf is int. The string "Hello World!" will not be outputted because the expression sizeof(printf( "Hello World!")) is not evaluated. The compiler just determines the type of the expression.

Also bear in mind the you may not use a function as an operand of the sizeof operator. That is you may not write sizeof( printf ). From the C Standard (6.5.3.4 The sizeof and alignof operators)

1 The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member

  • Related