string question with solution in c programming language



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

{

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.


1 comment:

Mucharla Govardhan Gopi said...

u need not initialize a variable in order to reserve space for it. It is sufficient if u just use it once in the program, Example

main()
{
int a[10];
}
This program will print size of the array as 20

main
{
int a[10];
printf("%d", sizeof(a));
}