compiler construction - Why do I have to specify data type each time in C? -
as can see code snippet below, have declared 1 char
variable , 1 int
variable. when code gets compiled, must identify data types of variables str
, i
.
why need tell again during scanning variable it's string or integer variable specifying %s
or %d
scanf
? isn't compiler mature enough identify when declared variables?
#include <stdio.h> int main () { char str [80]; int i; printf ("enter family name: "); scanf ("%s",str); printf ("enter age: "); scanf ("%d",&i); return 0; }
because there's no portable way variable argument functions scanf
, printf
know types of variable arguments, not how many arguments passed.
see c faq: how can discover how many arguments function called with?
this reason there must @ least 1 fixed argument determine number, , maybe types, of variable arguments. , argument (the standard calls parmn
, see c11(iso/iec 9899:201x) §7.16 variable arguments ) plays special role, , passed macro va_start
. in word, can't have function prototype in standard c:
void foo(...);
Comments
Post a Comment