Why should we specify the data type of each formal argument separately?

If we can declare more than one variable, for example:

int i, j, k; 

then why do I get an error when I write in formal arguments:

 void fun(int i, j, k) 

instead:

 void fun(int i, int j, int k) 
+6
source share
1 answer

Because it is not like the C syntax. Parameter declarations differ from variable declarations in several ways, for example

 void fun(int i, double x); 

vs.

 int i, double x; // syntax error 

Although the syntax could be extended to allow the form you tried (which is allowed, for example, Go with its func fun(i, j, k int) , the standard committee decided not to do this, apparently because it would confusing in the face of old-style ("K & R", pre-1989), which is still supported in ANSI C89 / ISO C90 for backward compatibility.

 void fun(i, j) // K&R syntax: implicitly int i, int j { } void fun(i, p) int *p; // int i implicit! { } // What this? double i? double *i? Mixed K&R/ANSI syntax with implicit int i? void fun(double *x, i) { } 
+16
source

Source: https://habr.com/ru/post/949924/


All Articles