MCQs
The strcmp return an int value that is
if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0
From the above statements, that the third statement is only correct.
Clearly, we know first three statements are correct, but fourth statement is wrong. because
we can use pointer on strings.
Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";
1: There is no difference in the declarations and both serve the same purpose.
2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing
to a non-const pointer.
3: The pointer p can be modified to point to another string, whereas the individual characters within
array a can be changed.
4: In both cases the '' will be added at the end of the string "Sanjay".
No answer description available for this question.
This program converts the given string to upper case string.
In printf("%u %s`setminus`n", &"Hello", &"Hello");.
The %u format specifier tells the compiler to print the memory address of the "Hello1".
The %s format specifier tells the compiler to print the string "Hello2".
Hence the output of the program is "1022 Hello2".
printf("%c`setminus`n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".
Hence the output is 'e'.
Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters
and initialized with a string "Hello".
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters
and initialized with a string "Hello".
We have use strcmp(s1,s2) function to compare strings.
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address
of both variable is not same. Hence the if condition is failed.
Step 4: At the else part it prints "Unequal".
gets() gets collects a string of characters terminated by a new line from the standard
input stream stdin.
The gets(str1) read the input string from user and store in variable str1.
The gets(str2) read the input string from user and store in variable str2.
The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other
negative or positive values. So the value of i is "unpredictable integer value".
printf("%dn", i); It prints the value of variable i.
The function printf() returns the number of charecters printed on the console.
Step 1: char a[] = '�'; The variable a is declared as an array of characters and it initialized
with "�". It denotes that the string is empty.
Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero).
Hence the if condition is failed.
In the else part it prints "The string is empty".
Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is declared
as an pointer to the array of 6 strings.
Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));
sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'
strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';
Hence the output of the program is 24, 5
Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5.
Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform),
the output will be 24, 5 (because the size of pointer is 4 bytes).