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.
Gpriya said:
3 years ago
Yes, we can pass more than 2 arguments in the main function and 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.
Nitesh said:
5 years ago
Environment Variable means : User's Home Directory List.
Output:
ALLUSERSPROFILE=C:\ProgramData
CommonProgramFiles=C:\Program Files\Common Files
HOMEDRIVE=C:
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 42 Stepping 7, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=2a07
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
Output:
ALLUSERSPROFILE=C:\ProgramData
CommonProgramFiles=C:\Program Files\Common Files
HOMEDRIVE=C:
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 42 Stepping 7, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=2a07
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
PUBLIC=C:\Users\Public
SESSIONNAME=Console
SystemDrive=C:
Jhethalal said:
5 years ago
Environment variables means?
Lona said:
5 years ago
@Sona Deepak has explained it clearly.
Sona said:
6 years ago
Environment variables means?
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.
Ravindra said:
1 decade ago
Somebody clearly elaborate environment variables.
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).
Ashish said:
1 decade ago
Is it possible to write more than two argument in main function?
Vikas said:
1 decade ago
Environment variables are variables which are used for every program globally. For every program environment variables are exist.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers