STRING QUESTIONS AND ANSWER IN C


(1) Without using any semicolon (;) in program write a c program which output is: HELLO WORLD?
Answer:
void main()
{
if(printf("HELLO WORLD"))
{
}
}
(2)What will be output of following code?
void main()
{
char a[5];
a[0]='q';
a[1]='u';
a[2]='e';
clrscr();
printf("%s",a);
getch();
}
Output: garbage
Explanation: %s is used for string but a is not a string it is only array of character since its last character is not null character.
If you will write a[3]=’\0’; then output will be que.
(3) Write a scanf statement which can store one line of string which includes white space.
Answer:
void main()
{
char a[30];
clrscr();
scanf("%[^\n]",a);
printf("%s",a);
getch();
}
(4) Write a c program in which a scanf function can store a paragraph at time?
Answer:
void main()
{
char a[30];
clrscr();
scanf("%[^\t]",a);
printf("%s",a);
getch();
}
Note: Paragraph will end when you will press tab key.
(5)In the following program
void main()
{
extern char a[30];
}
How much array a is reserving memory space?
Answer:
It is not reserving any memory space because array a has only declared it has not initialized .Any not initialized variable doesn’t reserve any memory space.


No comments: