84

A lot of programs use standard names for a number of arguments and arrays of strings. The prototype of main function looks like: int main(int argc, char *argv[]);. But would I break something if I choose custom names for these variables?

E.g. int main(int n_of_args, char *args[]);

In the context of the compiler, everything is fine. These variables are local for main function, so they may have any names. And the simple code builds and runs perfectly. But these names may be used by preprocessor. So is it safe to rename these arguments?

PS Personally I find these names bad, because they look very similar and differ in only one letter. But EVERYONE uses them for some kind of reason.

LeopardSkinPillBoxHat
  • 28,915
  • 15
  • 75
  • 111
yanpas
  • 2,155
  • 1
  • 17
  • 26
  • 22
    Yes, completely safe. – David Hoelzer Apr 29 '16 at 18:56
  • Yes, it is totally safe to do so !!! – Destructor Apr 29 '16 at 18:57
  • 55
    As all the answers say, yes, it's safe. But please don't do that. Everyone knows what `argc` and `argv` are. `n_of_args` and `args` might be clearer to someone who doesn't know C or C++ -- but that's not your audience. – Keith Thompson Apr 29 '16 at 19:07
  • 4
    You **can** do this, it is not enough reason to do this. Everybody knows what those are, and they do expect them to be like that. – SergeyA Apr 29 '16 at 19:48
  • 10
    If question is precisely "would I break something if I choose custom names", than the precise answer is "yes, you would break the well settled tradition" ;) – Frax Apr 29 '16 at 20:33
  • 13
    Just flip them! .. and right from the start you're laying a foundation for job security :) – yano Apr 29 '16 at 21:23
  • Sure, go ahead and rename them so you'll discover how it feels to be the most hated programmer in the shop. – Carey Gregory Apr 30 '16 at 04:00
  • I once worked for a company where the house style was `int main(int ac, char **av)` for no reason other than terseness. – zwol Apr 30 '16 at 15:01
  • 2
    The compiler will completely understand you, but no one else will. – Jeremy West May 27 '16 at 20:36
  • While you can rename them to anything you like, the names reflect the *argument count* and *argument vector* (thus `argc` & `argv`) which makes it a bit difficult to invent names more descriptive that convey that intent and still limit the length of the variables names to the same 4-character size... – David C. Rankin Dec 13 '17 at 06:39
  • @DavidC.Rankin OP's suggested `n_of_args` doesn't limit the length to four characters. I'm not sure why that would be a good thing, anyway. – Kyle Strand Dec 14 '17 at 20:25
  • @KyleStrand I don't know how you concluded my comment suggests an absolute 4-character limit on any of the names. If it is unclear, it was a statement regarding naming efficiency not one of name length limitation `:)` – David C. Rankin Dec 14 '17 at 20:41
  • @DavidC.Rankin I didn't think you meant an absolute limit; it's just that you specifically mentioned that it would be "difficult" to stay under that limit with new descriptive names, which gave me the impression that you think it's *desirable* to do so. My point is that (based the suggested alternatives) OP clearly doesn't agree that it's desirable to keep the names that short, so it's unclear why it matters (in this context) that it would be difficult to keep them short. – Kyle Strand Dec 14 '17 at 20:45
  • To each his own, `int main (int supercalifragilisticexpialidocious, char **antidisestablishmentarianism) { ... }` is fine... and if you like typing -- more power to you, I don't. – David C. Rankin Dec 14 '17 at 21:43
  • The name is bad? em, check std::basic_streambuf. There is a function `setg()`, it has a parameter `__gnext`, in the function body a line code does `_M_in_cur = __gnext;` noticed the variable names? Those are real bad names~ – r0n9 Dec 14 '17 at 22:25
  • The names are bad, you're right. Call them `argument_count` and `arguments`, or something else that makes sense. Convention isn't helpful here whatsoever. – iono Jul 15 '22 at 15:41
  • @r0n9 Those aren't "real" bad names; they're *also* bad names ;) – iono Jul 15 '22 at 15:42

10 Answers10

123

Yes, it is safe, so long as you use valid variable names. They're local variables, so their scope doesn't go beyond the main function.

From section 5.1.2.2.1 of the C standard:

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 being said, using anything other than argc and argv might confuse others reading your code who are used to the conventional names for these parameters. So better to err on the side of clairity.

dbush
  • 205,898
  • 23
  • 218
  • 273
39

The names argc and argv were actually mandated by the C++ standard prior to C++11. It stated:

All implementations shall allow both of the following definitions of main:

int main ()

and

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

and went on to discuss the requirements on argc and argv.

So technically, any program using different names was not standard-conforming, and the compiler was allowed to reject it. No compiler actually did so, of course. See this thread on comp.std.c++, or section 3.6.1 of this C++03 draft standard.

This was almost certainly a mere oversight, and was changed in C++11, which instead says

All implementations shall allow both

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

as the type of main (8.3.5). In the latter form, for purposes of exposition, the first function parameter is called argc and the second function parameter is called argv,…

Nick Matteo
  • 4,453
  • 1
  • 24
  • 35
29

Sure you can rename these parameters safely as you like

int main(int wrzlbrnft, char* _42[]) {
}

Names are written in sand. They don't have any influence on the finally compiled code.


The only thing that matters is, that parameter types of declaration and definition actually match.

The signature of the main() function is intrinsically declared as

int main(int, char*[]);

if you need to use them in an implementation actually you'll need to name them. Which names are used is actually irrelevant as mentioned before.

Yun
  • 3,056
  • 6
  • 9
  • 28
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
14

Yes. It is safe, it looks weird, but it won't break anything.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
sheikh_anton
  • 3,422
  • 2
  • 15
  • 23
7

Yes, it is safe to use different names.

Personally, I wouldn't recommend it, though, as the traditional argc and argv are so widely known and familiar to every other C programmer who might ever work with your code. In the long run, using your own, special, different names will cause far more confusion and/or frustration among your readers than it will ever save you because you like your names better.

"When in Rome, do as the Romans do."

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Yet there are few quite obvious scenarios calling for using different names, a notorious one is initializing GLUT, `glutInit(&argc, argv)`, where the arguments have to be declared and initialized differently, in order not to let GLUT eat up command line arguments, unless we want so. [SO link](http://stackoverflow.com/questions/14410404/glutinit-arguments) – user3078414 Apr 29 '16 at 23:16
  • @user3078414 Interesting example, but I don't see how it says anything about what the variables have to be named. Per the examples in that other question, we could as easily write `int dummyargc = 1; char *dummyargv[1] = {(char*)"Something"}; glutInit(&dummyargc, dummyargv);`. – Steve Summit Apr 29 '16 at 23:24
  • Thanks, @steve-summit. Some API documentation happens to be misguiding, therefore this thread is most helpful in highlighting the `argc argv` naming convention, as is your contributing answer. I just put my comment as example of preferably using whichever different names. Here's the [SO link](http://stackoverflow.com/questions/14410404/glutinit-arguments/36948999#36948999) – user3078414 Apr 29 '16 at 23:35
5

Yes you can rename them as you want. They are simply function parameter names, nothing more.

orbitcowboy
  • 1,438
  • 13
  • 25
3

I feel that everyone has covered the technical c++ rules well and good: Answer is yes. Let's put aside tradition and the fact that this 1 particular function is special and iconic which contains valid points to not change on this basis.

Often times, I feel the philosophy of the choices are rarely discussed and thus wanted to offer a perspective on this matter as I feel it to be important to the reason why this was asked to begin with.

This question to me involves a choice in expressing english in code in general. You seem to be bothered by short hand descriptions, in particular, if the short hand lands similar looking text. In your example though, changing argn to n_of_args only accomplishes the changing of one type of short hand into another form of shorthand with no real value addition: clarification or other visible properties.

The word 'number' has been replaced by a letter 'n'.

If you are changing a short hand name via the philosophy of anti short hand, then something like this may seem more appropriate:

main( int argumentCount, char ** argumentVector )

I always think about two things: Naming things by what they are and/or by their implied usage. Calling it an argumentVector is redundant to me since the property of being a vector is implied by the double indirection **. Thus, a better long hand for how I would write code is: ** arguments.

Some would say the variable named argumentCount is declared as an int and a Count can not be negative but you can have a negative int {unsigned is better}.

Again, what it is and how it is used comes to play in this interpretation. If it is a Count, then I would assume it would never be negative. After all, how can you have a Count of -2 apples. I would say, you OWE two apples. If it is a Number, then I would expect a negative case to be possible. This is why the additional word 'of' is likely important to you. That, and perhaps a number as referred to by a collection implies a specific item rather than a property of the collection itself. Ie: argumentsNumber = 5 implies a specific argument but not numberOfArguments.

main( int maxArgumentsIndex, char ** arguments ).

This removes ambiguity. Calling it an index removes negative case ambiguity and also describes what it is, and additionaly how to use it. It also implies by the english wording that a max is an absolute and would feel weird writing code that modifies this value (it should be const). 'arguments' makes sense here since it is plural, describes what it is, and how it should be used already. Even interpreting this way can be dangerous as an Index is -1 of a Count/NumberOf. 5 arguments yields a maxIndex of 4!!

Any other function and I would completely use:

void function( const unsigned int maxArgumentsIndex, const char ** arguments )

Not all situations merit long hand descriptors. In fact, some times a short hand yields more readability, in particular, in the case of writing math classes such as a Vec3f, Matrix, Quaternion, etc... I will almost always try to match the math language rather than the linguistic one. float x, y, z vrs. float xComponent and the like.

I understand all of this is a style choice, but being conscious of the choices will really help in the long run. I guarantee seasoned programmers get bothered when arrays are not written in plural form, but then again, main is a special prose of existence ;)

JMan Mousey
  • 271
  • 2
  • 13
2

As per C Standards, Yes you can rename, Nothing going to impact. As i understood, in C Language, the default Keyword/types/token names were defined with purpose/usage, so in the same way it is defined names

argc --> argument count

argv --> argument vector

which is also make sense in terms of usage, So you can change to any name expect Reserved names

In GCC, the program execution start with the function name main it doesn't depends on his parameters.

When you write standalone program for micro controllers, you no need to bother about the name main instead you can define your own name and change the Assembly entry_point to point your function. It depends on controller compiler and availability of pre-define controller source code. I've did this in Freescale controller under Code-warrior.

My Note:

It's better to follow the common standards/code style to make code more visible and readable

ntshetty
  • 1,293
  • 9
  • 20
0

It is safe as far as the compiler is concerned.

The only problem this can cause is confusion. People who read your code will expect those two variables to have their standard names. You could even do something like this:

int main(int foo, char ** bar)
{
    int argc;
    float argv;

But I don't think I need to tell how bad practice this would be.

klutt
  • 30,332
  • 17
  • 55
  • 95
-1

If you don't like the variable names, why not substituting them with the macro #define :

#define yourCounterName argc
#define yourVectorName  argv 

You will not take any risk and produce a "clean solution".

Fifi
  • 3,360
  • 2
  • 27
  • 53