2

Why is this Code with integer declaration in the middle of nowhere (in-between function definition), not throwing an error?

1) Why is it syntactically correct.
2) What is the use of doing so.?


#include <stdio.h>  
void func(int, int);
int main()
{
     int a, b;
     a = 10;
     b = 20;
     func(a, b);

     return 0;
}
void func(i, j)
int i,j;                  //why does this doesn't throw error.
{
     printf("a = i = %d\nb = j = %d\n", i, j);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Joses Paul
  • 75
  • 6
  • As answered below, it's syntactically correct because it is valid -- it's the original way things were done. There isn't any use of using this syntax now though unless you happen to be using a K&R style compiler that does not support ANSI C. – mah Feb 19 '16 at 17:35
  • And don't **EVER** provide a prototype for a function defined in K&R style. To find out why, change `int` to `char` in your function prototype and also in the definition. – Andrew Henle Feb 19 '16 at 17:49

2 Answers2

3

TL;DR - K&R style.

In your code

void func(i, j)
int i,j; {

does not throw an error, because it was a (the only) valid syntax, once upon a time.

Currently it is not invalid, but not used anymore.

You can read more about K&R style syntax here, if you want.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

This

void func(i, j)
int i,j; //declare types of arguments
{
  //function body
}

is called the K&R syntax. It's obsolete but still used in some C projects like bash.

Community
  • 1
  • 1
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142