Why it prints I value as 1, can someone please explain?
#include<stdio.h>
int main(i)
{
printf("i = %d\n", i);
}
output i = 1.
Why it prints I value as 1, can someone please explain?
#include<stdio.h>
int main(i)
{
printf("i = %d\n", i);
}
output i = 1.
C
is interpreting i
as type int
- if you don't declare a variable, its default type is int
. By coincidence, main
is used to being called as int main(int argc, char **argv)
, so your i
(which is now an int
) fits into that first parameter. main
will allow you to call it with only one argument, but this is technically undefined behavior - don't do it.
The first value, argc
, is a number detailing how many command line arguments were given. (The second is the strings of those arguments.) In your case, only one command-line argument was given, that being the name of the executable (probably ./a.out
).
Try running your code with ./a.out some strings here
- you'll notice different values printing.
There are two or three different little mysteries here. Not sure which one you're wondering about, so I guess I'll answer all three.
How did you get away with declaring int main(i)
without a type for i
?
How did you get away with declaring main
as accepting one parameter, not the conventional two?
Where did the value 1 come from when you ran it?
Answers:
(1) Once upon a time, a rather long time ago by now, in what pretty much counts as the dawn (or at least the morning) of C, the function definition syntax was different. You didn't say things like
int main(int argc, char *argv[])
{
...
}
Instead, you said
int main(argc, argv)
int argc;
char *argv[];
{
...
}
And that was when "implicit int
" was all the rage, too. So if you just wrote
int main(argc, argv)
char *argv[];
{
...
}
the compiler said "Okay, there's a parameter argv
and it's a char **
, and there's a parameter argc
and it's a... oh, you didn't say, so I'll just quietly assume int
."
And since C has always been pretty big on backwards compatibility, many compilers are still accepting the older syntax today, even though it's been officially obsolete for a while now.
(2) Also, in your case you completely left off the argv
part. main
is supposed to have two arguments, that's the way the system is calling it when your program starts up, but you only declared it as accepting one. So why did it work?
Strictly speaking the behavior is undefined when a function gets called with a different number of arguments than it expects, but it's the kind of thing you can often get away with, especially in the case that there were more arguments passed than expected. So although the system passed a second, argv
argument that your program didn't expect, the extra argument was just quietly discarded.
(3) And then, where did the 1 come from? Well, that's due to what main
's conventional argc
and argv
arguments actually mean. C's main
definition is based on the command-line invocation. Normally people type things like
programname somefilename someotherargument
and then hit RETURN. This invokes a program named programname
, with two command-line arguments somefilename
and someotherargument
. So how are those passed in to main
?
Well, argc
is a count of how many of them there were, and argv
is an array (v is for "vector") of the argument strings themselves. And the argument list always includes the program name itself. So if you invoke
programname somefilename someotherargument
then argc
comes in as 3 and argv
comes in as the array
{ "programname", "somefilename", "someotherargument", NULL }
But if you invoke something with no arguments, then argc
still comes in as 1. So that's where the 1 you saw came from.