Discussion :: Command Line Arguments - Yes / No Questions (Q.No.2)
|Deepika said: (Oct 22, 2010)|
|Anyone please help me. How is it works?|
|Sundar said: (Oct 22, 2010)|
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 = 1st argument value
argv = 2nd argument value
argv = 3rd argument value.
But in the given program, it is mentioned like
argv[argc] it means that argv.
This array holds the values upto argv only (argv - argv).
Therefore, argv 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.
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: (Mar 1, 2011)|
|Thank you somuch sundar. Its clear now.|
|Cool Buddy said: (Mar 18, 2011)|
|Parag said: (Jul 11, 2011)|
Nice ans. Thanks.
|Arun said: (Aug 3, 2011)|
|Good explanation sundar keep going.|
|Meenu said: (Dec 16, 2011)|
|Thank you sundar.|
|Deepika said: (Feb 14, 2012)|
|Excellent Sundar.. :)|
|Neha said: (May 23, 2012)|
|Thank you sundar.|
|Adarsh said: (Jul 21, 2012)|
|Subhendu Bera said: (Aug 22, 2012)|
|According to the C standard 2011 section 184.108.40.206.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 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.
|Jessie said: (Sep 4, 2012)|
|Thank you sundar;keep it up|
|Ramakri said: (Dec 4, 2012)|
|Is ** argv is same as *argv?
usually we use,
int main(int argc, char *argv);
|Anurag said: (Sep 3, 2013)|
|But what about argv? It is declared as char **argv.|
|Gangadhar said: (Sep 20, 2013)|
|Always argv[argc] is NULL(shall be a null pointer).
argv and argc are how command line arguments are passed to main() in C and C++.
argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.
The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.
FOR MORE INFO:
A pointer is said to be a null pointer when its right value is 0. Remember, a null pointer can never point to valid data.
To set a null pointer, simply assign 0 to the pointer variable. For example:
ptr_c = ptr_int = 0;
Here ptr_c and ptr_int become null pointers after the integer value of 0 is assigned to them.
NOTE: NULL pointer is different from null character '\0'.
|Shantanu Joshi said: (Sep 30, 2015)|
Now I understand what exactly has happened.
|Nehal said: (Mar 24, 2016)|
|@Sundar- It gives segmentation when we are trying to use %s instead of %d. And still I am not getting why it prints 0 for %d?|
|Mani said: (Nov 27, 2016)|
|Thank you @Sundar.|
|Adi said: (Jun 12, 2017)|
But it is not mentioned about the no of arguments in the question. Anywhere. Then how? Please explain.
|Mounika said: (Jun 16, 2017)|
If number of arguments are 4 , argv wont exist since 4 arguments are stored in argv, argv, argv, argv.
If number of arguments are 5 ,argv wont exist since 5 arguments are stored in argv to argv.
So if there are n arguments , argv[n] always contain same value i.e, null or garbage value.
|Hema said: (Mar 26, 2018)|
|Nice explanation Thanks @Sundar.|
|Matan said: (Jul 14, 2018)|
|At the end of argv you have NULL terminator.
Let's say you have three arguments in the command line so argc=3.
then argv will be 4 element size and argv will be '\0'. just like a string.
|Manai said: (Jul 15, 2019)|
|Nice explanations, Thanks All.|
|Mohit said: (Aug 25, 2020)|
|Is "&" a pointer variable in c?|
|Suraj said: (Feb 1, 2021)|
|Thank you @Sundar.|
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.