2

Let us consider the following program:

#include<stdio.h>

int test(a,b,c)
{
    return a+b+c;
}

int main()
{
    printf("%d\n",test(1,2,3));
    printf("%d\n",test(1.5,2.2,2.3));
}

I didn't know that it is possible to specify functions in C without defining the types of its parameters. This is a feature of ANSI C? Can someone explain this to me?

I thought it was not possible to do that. However, my compiler can compile this program! In which situations I can do that?

Also, The program behaviour is a little weird. When I use integer values, the function does what we expect. However, when I use float parameters, the result is very different from what I would expect.

3
  • 1
    That's not valid C! Commented Nov 27, 2016 at 1:05
  • Ok. It's was I thought. However, my compiler (mingW) compiles it withour problems. Why? Commented Nov 27, 2016 at 1:17
  • Those 'float' arguments are all of type double; you'd have to write 1.5F to make them of type float. When there's no prototype for a function, as there isn't with test(), then all parameters undergo default promotions — integer types shorter than int are converted to int, and float values are converted to double. Using K&R-style function definitions these days is an extremely bad idea, but they are still allowed by the C11 standard. You should never write one yourself; you should aim to upgrade any you find in the code you work on. Commented Nov 27, 2016 at 3:39

1 Answer 1

3

If you compile this code using GCC, it will show a warnings like type of 'a' defaults to ‘int’, since C implicitly assumes type of arguments as int, when not specified.

The feature where you don't have to specify the type of an argument in a function was added in order to provide backwards compatibility with older versions of the language, the K&R version of the language. When GCC encounters an "old style" function, it conforms to the old K&R C behaviour which implies no warnings in this situation.

One possible reason why you are getting undefined results maybe because the calling code doesn't know that it needs to cast the arguments to int and hence passes float instead. Your function test then either reads registers/stack locations that floats weren't copied to or receives the bit representations of float arguments and treats them as an int. Either of these will result in incorrect values of a, b, c being received.

Note that if you explicitly specify the type of arguments like int test(int a, int b, int c), then it would type-cast the floats, and then pass them to the function, with output 5.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't know that C implicitly assumes type of arguments as int, when not specified!!! I've been writing codes in C for 10 years and I didnt know that!
Yeah, it exists. Please see this link also : stackoverflow.com/questions/13950642/… and mark the answer as accepted if it solved your issue :) @Zaratruta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.