Hi,
I have written a program just to check the behaviour of the compiler.

i expected an error in function call statement.

but its not generating any error.

the declaration of function we include to check the type of args and no of args and return value of function.

but its not working as i expected.

please provide comments on this.

float get_float(int num);
int main( void )
{
        float f;
        f = get_float(12.34);
        printf("%f", f);
        f = 12.34;
        f = get_float(f);
        printf("%f", f);

        return 0;
}

float get_float( int num)
{
        return 12.32;
}

but its not working as i expected.

What were you expecting...my crystal ball's on the fritz and the Amazing kreskin is busy right now..

commented: i'm always partial to sarcasm. ;) +6

What were you expecting...my crystal ball's on the fritz and the Amazing kreskin is busy right now..

compiler uses the function declaration to know
1. type of values it can take as argument,
2. number of arguments it is allowed to pass and ofcourse with respective type.
3.the return type of the function.

now this is what i am expecting :
when compiler saves the above info to make sure that call is valid, why cant it check when passing the different type values.

as declaration in line 1 says float get_float(int num); accepting / passing int value.

but the calls in line no 5 and line 8 are using float argument.

Because C is fairly lax about types and if it has an automatic conversion from the type given to the type required then it will silently do it for you. C knows how to convert float to int so it just happens.

This was one of the issues fixed in C++ which is much more strongly typed, compile the code you have given as C++ and you get the following diagnostic messages

bytes.c:6: warning: passing `double' for converting 1 of `float get_float(int)'
bytes.c:9: warning: passing `double' for converting 1 of `float get_float(int)'

Because C is fairly lax about types and if it has an automatic conversion from the type given to the type required then it will silently do it for you. C knows how to convert float to int so it just happens.

This was one of the issues fixed in C++ which is much more strongly typed, compile the code you have given as C++ and you get the following diagnostic messages

bytes.c:6: warning: passing `double' for converting 1 of `float get_float(int)'
bytes.c:9: warning: passing `double' for converting 1 of `float get_float(int)'

so i can say that when passing arguements in function, c compiler will do promotions and demotions to the data. but it will not genereate any error / warning for mismatching type in function declaration and function call.


it will only generate errors in below cases:

when return type in the declaration does not match with the return type is definition.

when the number of arguments deos not match in prototype and definition.

so i can say that when passing arguements in function, c compiler will do promotions and demotions to the data. but it will not genereate any error / warning for mismatching type in function declaration and function call.

No you can say when passing arguments into a function the c compiler may do promotions and demotions to the data for certain types that it has knowlege of how to promote/demote and that it is not required to generate any diagnostic message to indicate that such a conversion was done.


Remember

  • The c compile can only automatically convert some types not all types. Pointers are a case where it can do the conversion but many compilers produce a warning.

  • Just because the standard doesn't require the compiler to emit a diagnostic message for a given situation doesn't mean that all compilers wont emit a warning.
  • The compiler is never required to emit wanrings or errors, only diagnostic messages.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.