Monday, 2 July 2012

C Command line argument questions




(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 mainfunction 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;
}



0 comments:

Post a Comment

newer post older post Home