agrc - It is an integer value contains the number of arguments passed to main function through command line.
Lets assume argc = 3.
The 3 arguments will be stored in the two dimensional array argv as given below.
argv[0] = 1st argument value
argv[1] = 2nd argument value
argv[2] = 3rd argument value.
But in the given program, it is mentioned like
argv[argc] it means that argv[3].
This array holds the values upto argv[2] only (argv[0] - argv[2]).
Therefore, argv[3] may be contain null value or garbage value. While converting this value as integer (because in printf statement "%d" specified), it will give 0 as output.
Therefore, the program's output will NOT change due to the number of arguments passed.
Note:
If you use %s instead of %d in the printf statement, it may print (null) or any garbage value depends upon the platform. There is a chance for abnormal program termination too.
Hope this will help you. Have a nice day!
Karthi said:
(Tue, Mar 1, 2011 01:40:45 PM)
Thank you somuch sundar. Its clear now.
Cool Buddy said:
(Fri, Mar 18, 2011 09:02:52 AM)
Sundar rocks.
Parag said:
(Mon, Jul 11, 2011 06:10:13 AM)
@Sundar
Nice ans. Thanks.
Arun said:
(Wed, Aug 3, 2011 01:48:17 PM)
Good explanation sundar keep going.
Meenu said:
(Fri, Dec 16, 2011 10:40:33 AM)
Thank you sundar.
Deepika said:
(Tue, Feb 14, 2012 11:56:09 AM)
Excellent Sundar.. :)
Neha said:
(Wed, May 23, 2012 01:36:08 PM)
Thank you sundar.
Adarsh said:
(Sat, Jul 21, 2012 05:13:51 PM)
Nice explanation.
@sundar.
Subhendu Bera said:
(Wed, Aug 22, 2012 07:18:19 PM)
According to the C standard 2011 section 5.1.2.2.1 paragraph "argv[argc] shall be a null pointer." If it is printed using %d then it will print 0.
@sundar your statement "Therefore, argv[3] may(**) be contain null value or garbage value(**)....." is wrong it will only contain null value. There is no scope of garbage and it is irrespective of compiler. If it "may be" null or gurbage then output would vary with every execution.