2

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.

Sunil Rathod
  • 53
  • 1
  • 5
  • 1
    You have not initialized `i`, so it will print a random (undefined) value. – daShier Oct 18 '19 at 20:05
  • 4
    First argument to `main` function is `argc`, so your `i` shows "1 argument passed" from command line. When application is run, 1st argument is a path to executable containing the `main` function: https://stackoverflow.com/q/3024197/6352710. If you declare second argument, you'll be able to print the path. – ahwayakchih Oct 18 '19 at 20:11
  • 3
    It's not undefined at all: main will be passed the number of command line arguments and a list of them. The first argument is the command name itself, so argv (or in this case i) will be 1. – Lee Daniel Crocker Oct 18 '19 at 20:18

2 Answers2

6

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.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • 1
    I don't think the implicit type has anything to do with the way `main()` is normally called. This is an old-style function definition, where types aren't specified in the parameter list. And the default type of undeclared variables is `int`. – Barmar Oct 18 '19 at 20:09
  • @Barmar I did not know that, I assumed it was converting it - I'll look for a reference for that behavior and link it in the question. – Nick Reed Oct 18 '19 at 20:10
1

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.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103