15.10 – Quiz on Strings and Command line arguments

by subbu on September 9, 2014

Questions and answers on Strings

1.What would be the output of following program?

#include<stdio.h>
int main()
{
char x[]="codingfox";
int i;
for(i=0;x[i]!='\0';i++)
   printf("%c",i[x]);
return 0;
}
Show Answer
Output:
codingfox

Individual characters of an array can be referred either by using x[i] or i[x] because the inner representation of x[i] is *(x+i) which can be also written as *(i+x)

2. What would be the output of following program?

#include<stdio.h>
int main()
{
char x[]="G";
printf("%c",x[0]);
printf("\n%s",x);
return 0;
}
Show Answer
Output:
G
G

reference to character and a string

Here x[0] prints the first character and “x” prints the total string that is “G”

3. What would be the output of following program?

#include<stdio.h>
int main()
{
char x[]="coding";
printf("%s",&x[2]);
printf("\n%s",x);
printf("\n%s",&x);
printf("\n%c",x[2]);
return 0;
}
Show Answer
Output:
ding
coding
coding
d

reference to a string

&x[2] gives the address of ‘d’, hence prints the string from ‘d’ to ‘\0’ that is “ding”. Both “x” and “&x” gives the address of string, hence prints “coding”. x[2] gives a single character “d”

4. What would be the output of following program?

#include<stdio.h>
int main()
{
char x[]="hello";
char *y;
int i;
y=x;
for(i=0;*y!='\0';i++,y++)
    printf("%c",*y);
return 0;
}
Show Answer
Output:
hello

Multiple references to a string
Here y is another reference to the array, accessing individual characters of character array using pointer arithmetic using y

5. What would be the output of following program?

#include<stdio.h>
int main()
{
printf("%c","Hello World"[4]);
return 0;
}
Show Answer
Output:
o

“Hello World” is stored in the memory as a character array along with ‘\0’ and gives the address of first character. Adding 4 to the address of “H” gives the address of “o” and the value at that would be “o”

6. What would be the output of following program?

#include<stdio.h>
int main()
{
int i;
for(i=0;"Hello World"[i]!='\0';i++)
    printf("%c","Hello World"[i]);
return 0;
}
Show Answer
Output:
Hello World

7. What would be the output of following program?

#include<stdio.h>
int main()
{
printf("%d\n%d\n%d",sizeof('x'),sizeof("x"),sizeof(5));
return 0;
}
Show Answer
Output in Turbo C
2
2
2

Output in Linux gcc
2
2
4

‘x’ is a character constant takes 1 byte. “x” is a string, terminates with ‘\0’, hence takes 2 bytes. In C language any integer constant is treated as int type, hence takes 2 bytes in case of Turbo C and 4 bytes in case of gcc.

8. What would be the output of following program?

#include<stdio.h>
int main()
{
printf("%d",sizeof(""));
return 0;
}
Show Answer
Output:
1

“” has a single character ‘\0’

9. What would be the output of following progrm?

#include<stdio.h>
int main()
{
char *p="codingfox";
p[0]='x';
p[1]='y';
printf("%s",p);
return 0;
}
Show Answer
Output:
xydingfox

Questions and Answers on Command line arguments

#include<stdio.h>
int main(int argc, char *argv[])
{

}

In the above structure, argc gives the total number of arguments that main() accepts from the command line

The minimum value of argc is 1 because at least the file name is send as the first argument when user doesn’t give any arguments.

1. What would be the output of following program without giving any arguments?

#include<stdio.h>
int main(int argc,char *argv[])
{
printf("%d",argc);
return 0;
}
Show Answer
Output:
1

2. What would be the output of following program without giving any arguments?

#include<stdio.h>
int main(int argc,char *argv[])
{
printf("%s",argv[argc-1]);
return 0;
}
Show Answer
Output in Turbo C
c:\turboc3\source\Demo.exe

Output in Ubuntu Linux gcc
./demo
The first argument argv[0] is the name of program

3. What would be the output of following program without giving any arguments?

#include<stdio.h>
int main(int argc,char *argv[])
{
while(argc)
    printf("%s",argv[--argc]);
return 0;
}
Show Answer
Output in Turbo C
c:\turboc3\source\Demo.exe

Output in Ubuntu Linux gcc
./demo

The first argument argv[0] is the name of program

4. What would be the output of following program without giving any arguments?

#include<stdio.h>
int main(int argc,char *argv[])
{
while(*argv!=NULL)
   printf("%s\n",*(argv++));
return 0;
}
Show Answer
Output:
Output in Turbo C
c:\turboc3\source\Demo.exe

Output in Ubuntu Linux gcc
./demo

Previous post:

Next post: