Thursday, December 25, 2008

Command line Argument Questions with Answer

Q.1 What will be output?

void main(int count,char *argv[])

{

int i=0;

for(i=0;i

printf("\n%s",argv[i]);

}

//save file as arg.c

In command line

C:\tc\bin>arg c programming language

Output:

c

Programming

Language

Q2. What will be output?

#include "dos.h"

void main()

{

printf("%d",_argc);

}

//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.

Q3.Reverse any string while string is passed throw command line?

Answer:

#include"string.h"

void main(int count,char *str[])

{

printf("%s",strrev(str[1]));

}

Output:


Q4. What will be output?

#include"dos.h"

void main()

{

int i=0;

for(i=0;i<_argc;i++)

printf("\n%s",_argv[i]);

}

//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.

Q5. How can create dos command type in c language?

Answer:

#include “stdio.h”

void 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

{

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);

}

}

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.


Q6. How can we create dos command in c program :dir ?

Answer:

#include

#include

void 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");

}

}

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

Q7.How can we display the entire environments vector by c program?

Answer:

void main(int count,char *arg[],char *argvect[])

{

int i=0;

clrscr();

while(argvect[i]) {

printf("\n%s",argvect[i]);

i++;

}

getch();

}

Output:

Q8.How can take a string from command line with main

function has no parameter and convert the string

in uppercase?

Answer:

#include"dos.h"

#include"string.h"

void 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);

}

Output:



No comments:

Blog List