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.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
Zaratruta
  • 1,261
  • 2
  • 14
  • 21
  • 1
  • Ok. It's was I thought. However, my compiler (mingW) compiles it withour problems. Why? – Zaratruta Nov 27 '16 at 01: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. – Jonathan Leffler Nov 27 '16 at 03:39

1 Answers1

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.

Jarvis
  • 8,020
  • 3
  • 23
  • 51
  • 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! – Zaratruta Nov 27 '16 at 02:06
  • 2
    Yeah, it exists. Please see this link also : http://stackoverflow.com/questions/13950642/why-does-a-function-with-no-parameters-compared-to-the-actual-function-definiti and mark the answer as accepted if it solved your issue :) @Zaratruta – Jarvis Nov 27 '16 at 02:07