C Language - Strings
You Are Here :: Home > C Language > Strings - General Questions
25.
main()
{
char *p="GOOD";
char a[ ]="GOOD";
printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p));
printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a));
}
A.
sizeof(p) = 2, sizeof(*p) = 1, strlen(p) = 4
sizeof(a) = 5, strlen(a) = 4
B.
sizeof(p) = 2, sizeof(*p) = 2, strlen(p) = 4
sizeof(a) = 5, strlen(a) = 4
C.
sizeof(p) = 4, sizeof(*p) = 2, strlen(p) = 4
sizeof(a) = 5, strlen(a) = 4
D.
sizeof(p) = 2, sizeof(*p) = 4, strlen(p) = 4
sizeof(a) = 5, strlen(a) = 4
Answer: Option A
Explanation:
sizeof(p) => sizeof(char*) => 2
sizeof(*p) => sizeof(char) => 1
Similarly, sizeof(a) => size of the character array => 5
When sizeof operator is applied to an array it returns the sizeof the array and it is not the same as the sizeof the pointer variable. Here the sizeof(a) where a is the character array and the size of the array is 5 because the space necessary for the terminating NULL character should also be taken into account.
26.
What will be output if you will compile and execute the following c code?
#include
int main(){
char *str;
scanf("%[^\n]",str);
printf("%s",str);
return 0;
}
A.
It will accept a word as a string from user.
B.
It will accept a sentence as a string from user.
C.
It will accept a paragraph as a string from user.
D.
Compiler error
Answer: Option B
Explanation:
Task of % [^\t] is to take the stream of characters until it doesn't receive new line character '\t' i.e. enter button of your keyboard.
« previous