C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 11)
11.
What will be the output of the program in Turbo C?
#include<stdio.h>
int main(int argc, char *argv, char *env[])
{
int i;
for(i=1; i<argc; i++)
printf("%s\n", env[i]);
return 0;
}
Discussion:
15 comments Page 1 of 2.
Ajit said:
1 decade ago
Till now I thought main () functn takes 2 arguments?.
Wikiok said:
1 decade ago
"argc": how many arguments are in the char *argv[] array. So argc can not be used for the environment array.
char *env[] : http://www.gnu.org/s/libc/manual/html_node/Program-Arguments.html (Only in UNIX).
char *env[] : http://www.gnu.org/s/libc/manual/html_node/Program-Arguments.html (Only in UNIX).
Babazka said:
1 decade ago
Wikiok is right. None of provided answers is correct. Also, argv should be declared as char *argv[] or char **argv, not char *argv.
Meenu said:
1 decade ago
Environment variable means?
Janu said:
1 decade ago
What is environment variables?
Vikas said:
1 decade ago
Environment variables are variables which are used for every program globally. For every program environment variables are exist.
Ashish said:
1 decade ago
Is it possible to write more than two argument in main function?
Amit Rahar said:
1 decade ago
First time seen that there can be more than two arguments in main function. yes we can access or use *env[] instead of *argv[] but both... I don't know. will try it manually on my laptop.
@Janu, Meenu ... these are the variables which are default for main function either passed or not. argc contains the no. of variables and the argv contains the list of the variables.
e.g. cp filename1 filename2
In this filename1 and filename2 are command line arguments or environment argument you can say.
Here int argc = 3. Because it counts the Program name too.
And char *argv[] = {cp, filename1, filename2} accessed liked an array in the program.
These are directly provided to the program from the command line (cmd in windows, terminal in linux).
@Janu, Meenu ... these are the variables which are default for main function either passed or not. argc contains the no. of variables and the argv contains the list of the variables.
e.g. cp filename1 filename2
In this filename1 and filename2 are command line arguments or environment argument you can say.
Here int argc = 3. Because it counts the Program name too.
And char *argv[] = {cp, filename1, filename2} accessed liked an array in the program.
These are directly provided to the program from the command line (cmd in windows, terminal in linux).
Ravindra said:
1 decade ago
Somebody clearly elaborate environment variables.
Deepak said:
9 years ago
Environment Variables:
When a program is executed, it receives information about the context in which it was invoked in two ways. The first mechanism uses the argv and argc arguments to its main function and is discussed in Program Arguments. The second mechanism uses environment variables and is discussed in this section.
The argv mechanism is typically used to pass command-line arguments specific to the particular program being invoked. The environment, on the other hand, keeps track of information that is shared by many programs, changes infrequently, and that is less frequently used.
The environment variables discussed in this section are the same environment variables that you set using assignments and the export command in the shell. Programs executed from the shell inherit all of the environment variables from the shell.
Standard environment variables are used for information about the user\'s home directory, terminal type, current locale, and so on; you can define additional variables for other purposes. The set of all environment variables that have values is collectively known as the environment.
Names of environment variables are case-sensitive and must not contain the character \'=\'. System-defined environment variables are invariably uppercase.
The values of environment variables can be anything that can be represented as a string. A value must not contain an embedded null character since this is assumed to terminate the string.
-> Environment Access: How to get and set the values of environment variables.
-> Standard Environment: These environment variables have standard interpretations.
When a program is executed, it receives information about the context in which it was invoked in two ways. The first mechanism uses the argv and argc arguments to its main function and is discussed in Program Arguments. The second mechanism uses environment variables and is discussed in this section.
The argv mechanism is typically used to pass command-line arguments specific to the particular program being invoked. The environment, on the other hand, keeps track of information that is shared by many programs, changes infrequently, and that is less frequently used.
The environment variables discussed in this section are the same environment variables that you set using assignments and the export command in the shell. Programs executed from the shell inherit all of the environment variables from the shell.
Standard environment variables are used for information about the user\'s home directory, terminal type, current locale, and so on; you can define additional variables for other purposes. The set of all environment variables that have values is collectively known as the environment.
Names of environment variables are case-sensitive and must not contain the character \'=\'. System-defined environment variables are invariably uppercase.
The values of environment variables can be anything that can be represented as a string. A value must not contain an embedded null character since this is assumed to terminate the string.
-> Environment Access: How to get and set the values of environment variables.
-> Standard Environment: These environment variables have standard interpretations.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers