Here _argc is
global identifier which has defined in dos.h.it count toal number of argument
in command line.
(3)Reverse any string while string is passed throw command
line?
Answer:
#include<string.h>
#include<stdio.h>
int main(int count,char *str[]){
printf("%s",strrev(str[1]));
return 0;
}
(4) What will be output following
c code?
#include<dos.h>
#include<stdio.h>
int main(){
int i=0;
for(i=0;i<_argc;i++)
printf("\n%s",_argv[i]);
return 0;
}
//save file as arg.c
In command line
C:\tc\bin>arg usa india japan
Output:
Usa
India
japan
Explanation:
Here _argc,_argv is global identifier which
has defined in dos.h._arg count total number of argument in command line while _argv is array of string which
store all the argument in command line.
(5) Write a c program to create
dos command type.
Answer:
#include<stdio.h>
int main(int count,char * argv[]){
int i;
FILE
*ptr;
char *str;
char ch;
if(count==1){
printf("The
syntax of the command is incorrect.\n");
}
for(i=1;i<cout;i++){
ptr=fopen(argv[i],"r");
if(ptr==NULL){
printf("The
system cannot find the file specified.");
if(count>2)
printf("\nError
occurred while procesing : %s.\n",argv[i]);
}
else{
if(count>2){
printf("%s\n\n",argv[i]);
}
while((ch=getc(ptr))!=-1)
printf("%c",ch);
}
fclose(ptr);
}
return 0;
}
Save the above
file as open.c, compile and execute the go to command mode (current working
directory) and write: open xy.c (xy.c any file present in thatdirectory)
To run the open
command in all directories and drive you will have to give the path of current
working directory in command mode. Write:
C:tc\bin>PATH
c:\tc\bin
Now press enter
key. Now your open command will work in all directory and drive.
(6) Write a c program to create
dos command dir.
Answer:
#include<stdio.h>
#include<dos.h>
int main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a
= _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf("
%s\n", q.name);
a
= _dos_findnext(&q);
}
}
else{
printf("File not
found");
}
return 0;
}
Save the above
file as list.c, compile and execute the go to command mode (current working
directory) and write: list *.c
To run the list
command in all directories and drive you will have to give the path of current
working directory in command mode. Write:
C:tc\bin>PATH
c:\tc\bin
Now press enter
key. Now your list command will work in all directory and drive.
Image list
(7)How can we
display the entire environments vector by c program?
Answer:
#include<stdio.h>
int main(int count,char *arg[],char *argvect[]){
int i=0;
while(argvect[i]) {
printf("\n%s",argvect[i]);
i++;
}
return 0;
}
(8)Write a c
program which takes a string from command line with mainfunction
has no parameter and convert the stringin
uppercase?
Answer:
#include<dos.h>
#include<string.h>
#include<stdio.h>
int main(){
char str[15];
int i=0;
strcpy(str,_argv[1]);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nstring
in uppercase : %s",str);
return 0;
}
If you have any queries or suggestions in
above C Command line argument questions with solution, please share it.
Answer and explanation of questions are based
on turbo c 3.0 compilers. Answer and explanation may vary in other compilers.
1.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
char arr[7]="Network";
printf("%s",arr);
}
Choose all that apply:
(A)
Network
(B)
N
(C)
Garbage value
(D)
Compilation error
(E)
None of the above
Explanation:
Size of a character array should
one greater than total number of characters in any string which it stores. In c
every string has one terminating null character. This represents end of the
string.
So in the string “Network” , there
are 8 characters and they are ‘N’,’e’,’t’,’w’,’o’,’r’,’k’ and ‘\0’. Size of
array arr is seven. So array arr will store only first sevent characters and it
will note store null character.
As we know %s in prinf statement
prints stream of characters until it doesn’t get first null character. Since
array arr has not stored any null character so it will print garbage value.
20.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
char array[]="Ashfaq \0 Kayani";
char *str="Ashfaq \0 Kayani";
printf("%s %c\n",array,array[2]);
printf("%s %c\n",str,str[2]);
printf("%d %d\n",sizeof(array),sizeof(str));
}
Choose all that apply:
(A)
Ashfaq h
Ashfaq h
16 2
(B)
Ashfaq h
Ashfaq Kayani h
16 16
(C)
Ashfaq y
Ashfaq h
2 2
(D)
Compilation error
(E)
None of the above
Explanation:
A
character array keeps the each element of an assigned array but a character
pointer always keeps the memory address of first element.
As
we know %s in prints the characters of stream until it doesn’t any null character
(‘\0’). So first and second printf
function will print same thing in the above program. But size of array is total numbers of its
elements i.e. 16 byte (including ending null character). While size of any type
of pointer is 2 byte (near pointer).