If fun does not take any arguments, say so:
void fun(void);
What you have says fun may take any arguments. The first invocation
should be used to determine what they are.
Not quite. The old-style form declares the function to take
_unspecified_ argument types, but they must be compatible with an
old-style definition: fixed in number (not variadic) and of types
after the default promotions i.e. not char, short, or float. All
invocations should be consistent, but the compiler is not required to
check; if it does, it will probably start with the first.
struct stu_dbase {
char name[50];
int reg_no;
}stu[] ={
{"bala",100},
{"sam",101},
{"balasam",102}
};
This "statement" serves two purposes. It declares the type struct
stu_dbase AND it defines an object of this type. The object is named
stu and happens to be an array of three struct.
Right. Nit: except officially declarations in C are are syntactically
separate from statements. In C89/90 the contents of a block, including
a function body, is zero or more declarations followed by zero or more
statements. In C99, where declarations can be interleaved with
statements, the contents are zero or more block-item's each of which
is either a declaration or a statement.
In C++, which allowed interleaving from the start, statements
syntactically include declarations, and block contents is zero or more
statements. In Fortran declarations are a form of statement, but there
are restrictions on the ordering of statement types which require all
declarations in a subprogram to precede all executable statements. In
PL/I declarations are a form of statement and may be anywhere -- even
after uses of the entities they declare.
The object is defined outside of any function and therefore
has external linkage and static duration. This is frequently referred
to as a global object.
Because it is at file scope therefore it has static duration. In
general objects declared at file-scope can have either internal or
external linkage; this is external because internal was not specified
with 'static' either here or previously.
However, the type is "known" only in the compilation unit
(source module) in which it appears.
Right.
{
int i;
for(i=0;i<3;i++)
printf("Reg_no:%d\tName\n",stu.reg_no,stu.name);
You have two arguments after the format string. Your format string
only has one conversion specification. This invokes undefined
behavior. You probably want a %s before the \n.
Not UB -- excess arguments to any variadic function including printf
are ignored; as long as the evaluation in/preceding the function call
is well-defined, as this is, there is no UB. It is usually including
in this case wrong = not what was wanted, but not UB.
- David.Thompson1 at worldnet.att.net