C string interview questions and answers



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


7 comments:

harry potter said...

meticulousy done

Jyotirmaya Ranbirsingh said...

sir please explain how //scanf("%[^\n]",a); & scanf("%[^\t]",a); works ?

Anonymous said...

ddd

Anonymous said...

very good...

Anuj said...

output of second question will be que

Harikrishnan V said...

Takes in characters till new line/tab is pressed

Ananomous said...

what does the extern keyword do?