Sunday, January 4, 2009

Command Line Argument Question with Answers



What is command prompt?
Ans: Command prompt is text mode interface by which user can enter the command from keyboard. It is non-graphical interface. These commands invoke the specific program . 
What is command line argument?
Ans: With help of main function in c we can invoke the program. main function is called by operating system. In main function has the parameter. They are:
1. Argument counter
2. Argument vector
3. Environment vector
It is written as :
main (int argument_counter,char *argument_vector[ ],char *environment_vector[ ])
Argument counter: It is integer type data type. It always count the number of argument written to execute the any command and store that number.
Example:
void main (int argument_counter )
{
printf(“Total number of argument :\t%d”,argument_counter);
}
Save this program .Let file name is count.c and compile and execute the program then
open run in window then write cmd and press enter. Now go to your current working directory (generally it is c:\tc\bin)


Now if you will count command and argument like India,pak etc then out put will be total number of argument including count. 
Argument vector: It is array which contain address of char data type i.e address of all argument.
Example:
void main(int argument_counter,char *argument_vector[])
{
int i;
for(i=0;i
{
printf("%s\n",argument_vector[i]);
}
}
Save this program as arg_vect.c. now write the command in command prompt.


It is displaying all the argument including arg_vect.
How to create dos command in c?
Write a c program to create open command which is similar to type command in dos operating system?
Ans:
#include
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 India.c 
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.
Write a c program to create list command which is similar to dir command in dos operating system?
Ans:
#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.

In same way you can create cls ,copy con etc commands.
Environment vector: It is array which contain address of char data type i.e address of all environment vector.
Example:
void main(int argument_counter,char *argument_vector[],char *enviroment_vector[ ])
{
int i=0;
while(enviroment_vector[i])
{
printf("%s\n",enviroment_vector[i]);
i++;
}
}
Its output will be list of all environment vectors.


1. Point out error, if any, in the following program
main()
{
int i=1;
switch(i)
{
case 1:
printf("\nRadioactive cats have 18 half-lives");
break;
case 1*2+4:
printf("\nBottle for rent -inquire within");
break;
}
}
Ans. No error. Constant expression like 1*2+4 are acceptable in cases of a switch. 
2. Point out the error, if any, in the following program
main()
{
int a=10,b;
a>= 5 ? b=100 : b=200;
printf("\n%d",b);
}
Ans. lvalue required in function main(). The second assignment should be written in parenthesis as follows:
a>= 5 ? b=100 : (b=200);
9. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind() <>
Ans. True
13. In the following code, is p2 an integer or an integer pointer?
typedef int* ptr
ptr p1,p2;
Ans. Integer pointer

14. Point out the error in the following program
main()
{
const int x;
x=128;
printf("%d",x);
}
Ans. x should have been initialized where it is declared.
16. What is the difference between the following declarations?
const char *s;
char const *s;
Ans. No difference

17. What is the difference between the following declarations?
const char *const s; char const *const s;
Ans. No difference
22. Point out the error in the following program
main()
{
int a=10;
void f();
a=f();
printf("\n%d",a);
}
void f()
{
printf("\nHi");
}
Ans. The program is trying to collect the value of a "void" function into an integer variable.

23. In the following program how would you print 50 using p?
main()
{
int a[]={10, 20, 30, 40, 50};
char *p;
p= (char*) a;
}
Ans. printf("\n%d",*((int*)p+4));
27. Point out the error in the following program
main()
{
const char *fun();
*fun()='A';
}
const char *fun()
{
return "Hello";
}
Ans. fun() returns to a "const char" pointer which cannot be modified
25. For the following C program
int x(char *a)
{a=(char *) malloc(10*sizeof(char));
*a="hello";
}
main()
{char *a="new";
x(a);
printf("%s",a);
}
The output is
a) Hello
b) New
c) Hello new
d) Run time error

Ans. (b) 

9.
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}

What are the number of syntax errors in the above? 

Ans: None.

10. 
void main()
{
int i=7;
printf("%d",i++*i++);
}

Ans: 56

11. 
#define one 0
#ifdef one 
printf("one is defined ");
#ifndef one
printf("one is not defined ");

Ans: "one is defined"


12.
void main()
{
intcount=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=∑
*temp=count;
printf("%d %d %d ",count,*temp,sum);
}

Ans: 20 20 20


13. There was question in c working only on unix machine with pattern matching.


14. what is alloca()
Ans : It allocates and frees memory after use/after getting out of scope

15. 
main()
{
static i=3;
printf("%d",i--);
return i>0 ? main():0;
}

Ans: 321

16. 
char *foo()
{
char result[100]);
strcpy(result,"anything is good");
return(result);
}
void main()
{
char *j;
j=foo()
printf("%s",j);
}

Ans: anything is good.

17.
void main()
{
char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
char **p;
p=s;
printf("%s",++*p);
printf("%s",*p++);
printf("%s",++*p);
}
Ans: "harma" (p->add(dharma) && (*p)->harma)
"harma" (after printing, p->add(hewlett-packard) &&(*p)->harma)
"ewlett-packard" 
1.
void main()
{
int d=5;
printf("%f",d);
}
Ans: Undefined

2.
void main()
{
int i;
for(i=1;i<4,i++)
switch(i)
case 1: printf("%d",i);break;
{
case 2:printf("%d",i);break;
case 3:printf("%d",i);break;
}
switch(i) case 4:printf("%d",i);
}
Ans: 1,2,3,4 

3.
void main()
{
char *s="\12345s\n";
printf("%d",sizeof(s));
}
Ans: 6

4. 
void main()
{
unsigned i=1; /* unsigned char k= -1 => k=255; */
signed j=-1; /* char k= -1 => k=65535 */
/* unsigned or signed int k= -1 =>k=65535 */
if(i
printf("less");
else
if(i>j)
printf("greater");
else
if(i==j)
printf("equal");
}
Ans: less

5.
void main()
{
float j;
j=1000*1000;
printf("%f",j);
}

1. 1000000
2. Overflow
3. Error
4. None 
Ans: 4 

6. How do you declare an array of N pointers to functions returning
pointers to functions returning pointers to characters?

Ans: The first part of this question can be answered in at least
three ways:
Q7. #define MAN(x,y) (x)>(y)?(x):(y)
{
inti=10;j=5;k=0;
k= MAX(i++,++j)
printf(%d %d %d %d,i,j,k)
}
Ans. 10 5 0
Q3. struct list{
int x;
struct list *next;
}*head;
the struct head.x =100
Is the above assignment to pointer is correct or wrong ?
Ans. Wrong


Q4.What is the output of the following ?
int i;
i=1;
i=i+2*i++;
printf(%d,i);
Ans. 4

Q5. FILE *fp1,*fp2;
fp1=fopen("one","w")
fp2=fopen("one","w")
fputc('A',fp1)
fputc('B',fp2)
fclose(fp1)
fclose(fp2)}
a.error b. c. d.
Ans. no error. But It will over writes on same file.
7. For the following C program
#define AREA(x)(3.14*x*x)
main()
{floatr1=6.25,r2=2.5,a;
a=AREA(r1);
printf("\n Area of the circle is %f", a);
a=AREA(r2);
printf("\n Area of the circle is %f", a);
}
What is the output?
Ans. Area of the circle is 122.656250
Area of the circle is 19.625000
15. 
main()
{
static i=3;
printf("%d",i--);
return i>0 ? main():0;
}

Ans: 321
9.
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}

What are the number of syntax errors in the above? 

Ans: None.

10. 
void main()
{
int i=7;
printf("%d",i++*i++);
}

Ans: 56

11. 
#define one 0
#ifdef one 
printf("one is defined ");
#ifndef one
printf("one is not defined ");

Ans: "one is defined"


12.
void main()
{
intcount=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=∑
*temp=count;
printf("%d %d %d ",count,*temp,sum);
}

Ans: 20 20 20
53. For the following C program
void main()
{
unsigned char c;
for(c=0;c!=256;c=c+2)
printf("%d",c);
getch();
}


(a) 127
(b) 128
(c) 256
(d) infinitely
Ans: (d)
57. For the following program
int i;
i=2;
i++;
if(i==4)
{printf(i=4);
}
else
{printf(i=3);
}
Output of the program ? 

a) 4 
b) 3
c) unpredictable
d) none

Ans. (b)
58. What is FAT?.

a) File Allocation Table
b) File Access Table
c) FDD Allocation Table
d) None of the above

No comments:

Blog List