Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 883
  • Last Modified:

What is argc?

What is "argc" and why is the value is 3 when i type in this: a.out abc de?

#include <stdio.h>
int main (int argc, char *argv[])
{
  int i,n;
  char *p;
  printf("argc is: %d\n", argc);

  for(i=0;i<argc;i++)
  {
    for(p=argv[i],n=0; *p;n++,p++);
    printf("\n%s %d",argv[i],n);
  };
}
0
xp5
Asked:
xp5
  • 3
  • 3
1 Solution
 
Infinity08Commented:
>> What is "argc" and why is the value is 3 when i type in this: a.out abc de?

argc contains the number of tokens, passed on the command line. That is the name of the executable, and the arguments passed. In this case, the name of the executable is a.out, and there are two arguments abc and de.

You can find the arguments in the argv array in C string format (character arrays).

So :

        argv[0] contains "a.out"
        argv[1] contains "abc"
        argv[2] contains "de"
0
 
abithCommented:
actually there are three parameter for main.
signature is :
int main(int argc, char * argv[], char *envp[])
argc tells you how many command line parameters are passed with having current exe name as first param,
argv tells you what are the command line parameters passed (including current file exe name)
envp tells you what are the environment values set. you can see the values by iteration upto null element in that array.

all are optional, and if you want to specify, order should be followed.
0
 
peetmCommented:
>>actually there are three parameter for main.
>>signature is :
>>int main(int argc, char * argv[], char *envp[])

Actually - there aren't - the ISO standard says that main() has two valid signatures

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent) or in some other implementation-defined manner.

That last bit allows certain 'extensions' but any of those are implementation specific, i.e., there's no requirement for a compiler to allow anything but the standard two definitions of main().
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Infinity08Commented:
>> actually there are three parameter for main.

No, there are not. The only standard way of defining main in C is either :

        int main(void) { }

or :

        int main(int argc, char *argv[]) { }

(or equivalent). Not all of them are optional as you say (it's either both or none), and any deviation from this standard is platform specific and thus non-portable.
0
 
abithCommented:
>>Not all of them are optional as you say (it's either both or none),
pls, re-look at my post
   >>all are optional, and if you want to specify, order should be followed.
which means if you want to read argc, you should specify int as first param, and this should be second.

>>any deviation from this standard is platform specific and thus non-portable
Are you really sure that this will become non-portable ?? because, main is simply a function, the only spl is, this is the entry point for a executable, so return value should be understandable by caller, so return type should be specific, but i dont think about the parameters.

Anyway,
What you people are saying is standards. So i agree as per standard, you both Infinity08 & peetm are correct. What i meant was, there are three and their's details. (even though third one is not the iso/ansi/.. standard)
0
 
Infinity08Commented:
>> pls, re-look at my post

Yes. And you said : "all are optional". That's not true. You can't have argc without argv (or vice versa).


>> Are you really sure that this will become non-portable ??

It always has been unportable. There's only a few compilers/systems that support it, so you should use it with care (and preferably not use it at all).
Note that on other systems, they might add a different third parameter, or a fourth, fifth, etc. That's the whole point of something being un-portable : you can not rely on it.


>> What you people are saying is standards.

Of course we are speaking about the standards, because that's the only thing we can rely on. All the rest is highly platform specific.

And when you correct me by saying "actually there are three parameter for main.", then that's simply not true. It might be true that some systems support that third parameter, but you can't guarantee that xp5's system does. That's the reason you have to stick to the standards, and if you talk about something non-standard, then you mention that explicitly : "this is only true on certain systems".
0
 
abithCommented:
>>You can't have argc without argv (or vice versa).
thats why i mention order should be followed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now