Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a C program printing to sdout all its arguments (including argv[0]), on

ID: 3753520 • Letter: 2

Question


2. Write a C program printing to sdout all its arguments (including argv[0]), one per line, followed by all its environment variables (again, one per line) You can access environment variables using the following variant for your main function int main(int argc, char *argv[], char* env[]) The environment variables are stored as an array of strings in the variable env. This array is terminated by an element whose value is NULL. Save the source code of this program in printargsandenv.c If you run the code of this C program in the folowing way: ./printargsandenv argument1 argument2 the output shuld be similar to: ./printargsandenv argumenti argument2 XDG VTNR- ORBIT SOCKETDIR-/tmp/orbit-antoniob XDG SESSION ID c1 VIRTUALENVWRAPPER SCRIPT-/usr/share/virtualenvwrapper/virtualenvwrapper.sh XAUTHORITY-/home/antoniob/.Xauthority COLORTERM-gnome-terminal -/printargsandenv OLDPWD-/home/antoniob/git (I omitted many lines. Also, the values of your environment variables will likely be different.)

Explanation / Answer

//program code

#include<stdio.h>

int main(int argc,char *argv[],char* env[]) //definition of main function #include<stdio.h>

{

int i=0; // intializing i with 0

printf("%d ",argc); // printing first argument value

printf("%s ",argv); // printing second argument value

while(env[i]!=NULL) // comparing environment value will NULL

{

printf("%s ",env[i]); // // printing environment value

i++; // INCREMENT i by 1 to get the next environment value

} // end of while

} // end of main

Sample ouput according to my system

1
á_V
ALLUSERSPROFILE=C:ProgramData
APPDATA=C:UsersjoyAppDataRoaming
CLASSPATH=C:Program FilesJavajdk-10.0.2in;C:Program FilesJavajre-10.0.2
bin;.
CommonProgramFiles=C:Program FilesCommon Files
CommonProgramFiles(x86)=C:Program Files (x86)Common Files
CommonProgramW6432=C:Program FilesCommon Files
COMPUTERNAME=JOY-PC
ComSpec=C:Windowssystem32cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=Usersjoy
LOCALAPPDATA=C:UsersjoyAppDataLocal
LOGONSERVER=\JOY-PC
NUMBER_OF_PROCESSORS=4
OS=Windows_NT
Path=C:Program Files (x86)Dev-CppMinGW64in;C:Program Files (x86)Common Fi
lesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem
;C:WindowsSystem32WindowsPowerShell 1.0;C:Program FilesIntelWiFiin;C:
Program FilesCommon FilesIntelWirelessCommon;d:Program FilesMATLABR2017a
untimewin64;d:Program FilesMATLABR2017ain;C:Program FilesJavajdk-10.0
.2in;C:Program FilesJavajre-10.0.2in;C:UsersjoyAppDataLocalPrograms
PythonPython37Scripts;C:UsersjoyAppDataLocalProgramsPythonPython37;C:
Program FilesIntelWiFiin;C:Program FilesCommon FilesIntelWirelessCommo
n
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3a09
ProgramData=C:ProgramData
ProgramFiles=C:Program Files
ProgramFiles(x86)=C:Program Files (x86)
ProgramW6432=C:Program Files
PSModulePath=C:Windowssystem32WindowsPowerShell 1.0Modules
PUBLIC=C:UsersPublic
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:Windows
TEMP=C:UsersjoyAppDataLocalTemp
TMP=C:UsersjoyAppDataLocalTemp
USERDOMAIN=joy-PC
USERNAME=joy
USERPROFILE=C:Usersjoy
VS100COMNTOOLS=C:Program Files (x86)Microsoft Visual Studio 10.0Common7Tools

windir=C:Windows
windows_tracing_flags=3
windows_tracing_logfile=C:BVTBinTestsinstallpackagecsilogfile.log

--------------------------------
Process exited after 0.03275 seconds with return value 0
Press any key to continue . . .

//program code

#include<stdio.h>

int main(int argc,char *argv[],char* env[]) //definition of main function #include<stdio.h>

{

int i=0; // intializing i with 0

printf("%d ",argc); // printing first argument value

printf("%s ",argv); // printing second argument value

while(env[i]!=NULL) // comparing environment value will NULL

{

printf("%s ",env[i]); // // printing environment value

i++; // INCREMENT i by 1 to get the next environment value

} // end of while

} // end of main

Sample ouput according to my system

1
á_V
ALLUSERSPROFILE=C:ProgramData
APPDATA=C:UsersjoyAppDataRoaming
CLASSPATH=C:Program FilesJavajdk-10.0.2in;C:Program FilesJavajre-10.0.2
bin;.
CommonProgramFiles=C:Program FilesCommon Files
CommonProgramFiles(x86)=C:Program Files (x86)Common Files
CommonProgramW6432=C:Program FilesCommon Files
COMPUTERNAME=JOY-PC
ComSpec=C:Windowssystem32cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=Usersjoy
LOCALAPPDATA=C:UsersjoyAppDataLocal
LOGONSERVER=\JOY-PC
NUMBER_OF_PROCESSORS=4
OS=Windows_NT
Path=C:Program Files (x86)Dev-CppMinGW64in;C:Program Files (x86)Common Fi
lesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem
;C:WindowsSystem32WindowsPowerShell 1.0;C:Program FilesIntelWiFiin;C:
Program FilesCommon FilesIntelWirelessCommon;d:Program FilesMATLABR2017a
untimewin64;d:Program FilesMATLABR2017ain;C:Program FilesJavajdk-10.0
.2in;C:Program FilesJavajre-10.0.2in;C:UsersjoyAppDataLocalPrograms
PythonPython37Scripts;C:UsersjoyAppDataLocalProgramsPythonPython37;C:
Program FilesIntelWiFiin;C:Program FilesCommon FilesIntelWirelessCommo
n
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3a09
ProgramData=C:ProgramData
ProgramFiles=C:Program Files
ProgramFiles(x86)=C:Program Files (x86)
ProgramW6432=C:Program Files
PSModulePath=C:Windowssystem32WindowsPowerShell 1.0Modules
PUBLIC=C:UsersPublic
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:Windows
TEMP=C:UsersjoyAppDataLocalTemp
TMP=C:UsersjoyAppDataLocalTemp
USERDOMAIN=joy-PC
USERNAME=joy
USERPROFILE=C:Usersjoy
VS100COMNTOOLS=C:Program Files (x86)Microsoft Visual Studio 10.0Common7Tools

windir=C:Windows
windows_tracing_flags=3
windows_tracing_logfile=C:BVTBinTestsinstallpackagecsilogfile.log

--------------------------------
Process exited after 0.03275 seconds with return value 0
Press any key to continue . . .

NOTE:when you run this program on your system, you may get different environment values
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote