Ahmed Safwat Ahmed Safwat - 13 days ago 7
C Question

using printf in a function returns void

when I ran this code I got an error says "parameter name omitted", I doubt it's because I'm using printf which returns int type inside a void return function.

*NOTE: I was writing this code to examine will the pointer change the value of a global variable successfully or not ?

#include <stdio.h>

int a = 5 ;

void call(int)
{
printf("%d\n",a);
}

int main()
{
int* p = &a;
call(a);
*p = 6 ;
printf("%d\n",a);
printf("%d\n",*p);
call(a);
}

Answer

It seems that the compiler considers warnings like errors. In fact this function definition

void call(int)
{
    printf("%d\n",a); 
}

is valid. However the compiler warns you that the parameter name is omitted though the function is called with an argument.

Indeed the function does not use its parameter. So you can define it either like this (a preferable definition in C)

void call( void )
{
    printf("%d\n",a); 
}

or like this

void call()
{
    printf("%d\n",a); 
}

In this case you have to call the function like

call();

and the function will rely on the global variable a.

Take into account that if you exchange the function definitiom and the definition of the global variable like this

void call( void )
{
    printf("%d\n",a); 
}

int a = 5 ; 

then the code will not compile.

Thus it would be better if the function did not deal with global variables. SO it is better to define the function like

void call( int a )
{
    printf("%d\n",a); 
}

and call it like

call(a);
Comments