C Command line argument questions with solution


Command line arguments in c example 




(1)What will be output of following c code?

#include<stdio.h>
int main(int count,char *argv[]){
    int i=0;
    for(i=0;i<count;i++)
         printf("\n%s",argv[i]);
    return 0;
}

//save file as arg.c
In command line
C:\tc\bin>arg c question bank
Output:
c
question
bank





(2) What will be output of following c code?

#include<dos.h>
#include<stdio.h>
int main(){
    printf("%d",_argc);
    return 0;
}

//save file as countarg.c
In command line
C:\tc\bin>countarg a1 a2 b1 b2 (press enter)
Output:
5

Explanation:
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 that directory)
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 main function has no parameter and convert the string in 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.

12 comments:

Anonymous said...

nice

Anonymous said...

super man

Anonymous said...

that was exciting one......this rigorousness is not provided everywhere....

Anonymous said...

program to read an integer in the range 1 to 9999 as command-line argument, convert and print the value in words.

Sanjay kumar said...

HI
PLEASE UPLOAD MORE COMMAND LINE C PROGRAM.

Anonymous said...

super

punit sharma said...

program that accepts user name from user and password by using command line arguments.
user name should be declare in array in the program.if we entered wro0ng username it have to give ,username is wrong on output screen?

Ram said...

this web site is to good for the interview perspective...and we can get depth knowledge and concept............Ram maheswari.........Banglore

Ram said...

plz add more gud concept and more programming question....it will be useful for us............

Rajineekar K said...

i am not getting the bin folder in c :\tc\ , why plz tell me

Zubair Baig said...

C Programming Tips And Tricks...

Vist This Link..
https://youtu.be/TBKVuM9W8RM

kishore shiva said...

For the 1st question, output:
arg
c
question
bank

because arg[0] = arg
arg[1] = c
arg[2] = question
arg[3] = bank