type of argc

S

subramanian100in

The C standard specifies that argc value should be non-negative. Given
this, is there any reason for keeping the type of argc in main( ) as
int instead of unsigned int ?

For learning purpose I am asking this.

Thanks
 
E

Eric Sosman

The C standard specifies that argc value should be non-negative. Given
this, is there any reason for keeping the type of argc in main( ) as
int instead of unsigned int ?

Yes: It would break any existing code that controls the
processing of its arguments by decrementing argc until it
becomes negative. The change therefore carries a risk whose
magnitude is hard to estimate, but which is certainly greater
than zero.

Taking a risk can be justified if the expected benefit
outweighs it, but what is the expected benefit? As far as
I can see, the only gain is the ability to handle more than
32766 command-line arguments with a 16-bit int, or more than
2147483646 with a 32-bit int. IMHO, the value of this gain
would be tiny indeed.

You may wonder why argc wasn't an unsigned int to begin
with, which would have made the "it breaks existing code"
problem disappear. Keep in mind that C was not invented
overnight in one atomic act of creation, but evolved along
the way. The earliest versions of C had no unsigned types
at all, so what we now call signed int was the natural choice
for counters of all kinds. When unsigned types were added,
there was -- then as now -- no compelling reason to break
existing perfectly good code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top