3,417 views
7 7 votes

Which one among the following definitions of string str could cause problem when passed as the first argument to printf function?

  1. char str[] = "Hello World";
  2. char str[12] = "Hello World";
  3. char *str = "Hello World";
  4. char str[] = {'H','e','l','l','o',' ','W','o','r','l','d'};

4 Answers

Best answer
16 16 votes

We are asked,

Which one among the following definitions of string str could cause a problem when passed as the first argument to printf function?

Option D. char str[] = {'H','e','l','l','o',' ','W','o','r','l','d'};   // It is just a character array but not string. 

Now if we add a null character '\0' at the end of all characters in the above array then it will be string str= Hello World.

And in the rest options, the compiler can automatically append a null character (\0) to the end of the array and treat the character array as a character string.

• selected by
0 0 votes
answer (d) is correct becuse d is a character array not a string but all 3 are string declration
0 0 votes

In C a string is defined as an array of characters termined by the NULL terminator, ie, \0.

In Options A, B and C, the compiler would implicitly add a NULL terminator at the end.

In Option D, we're defining the whole character array ourselves and we didn't add the NULL terminator explicitly, which is required here.

0 0 votes
#include <stdio.h>
int main(){
    char Str1[] = "Hello World";
    char Str2[12] = "Hello World";
    char *Str3 = "Hello World";
    char Str4[] = {'H','e','l','l','o',' ','W','o','r','l','d'};
    printf("%s\n",Str4);
    printf("Sizeof Str1 : %d \t Strlen Str1 : %d\n",sizeof(Str1),strlen(Str1));
    printf("Sizeof Str2 : %d \t Strlen Str2 : %d\n",sizeof(Str2),strlen(Str2));
    printf("Sizeof Str3 : %d \t Strlen Str3 : %d\n",sizeof(Str3),strlen(Str3));
    printf("Sizeof Str4 : %d \t Strlen Str4 : %d\n",sizeof(Str4),strlen(Str4));
}

Output : 

Hello World
Sizeof Str1 : 12 	 Strlen Str1 : 11
Sizeof Str2 : 12 	 Strlen Str2 : 11
Sizeof Str3 : 8 	 Strlen Str3 : 11  
Sizeof Str4 : 11 	 Strlen Str4 : 11  

You will Not get any Error that's correct But, If you see the String4 sizeof(Str4).  i.e 11 which means the compiler is not adding the '\0' in Str4.

Sizeof (Str3) is 8 because its pointer type variable (i.e Sizeof(pointer) = 8 Byte

For all remaining String from Str1, Str2, Str3 the compiler Implicit added the '\0'     

• edited by
Answer:
Position:
Show:

Related questions

6 6 votes
1 1 answer
3.2k
3.2k views
Arjun asked Oct 18, 2016
3,210 views
Consider the following incomplete C function for reversing a singly linked list.node* reverse(node* trav){ if(trav->next) __________________ else { head - next = null; he...
5 5 votes
2 answers 2 answers
2.2k
2.2k views
Arjun asked Oct 18, 2016
2,197 views
What will be the output of the following code?#include <stdio.h #include <string.h int main() { struct mystruct{ char *name; unsigned int age; }; struct mystruct st1 = {"...
9 9 votes
2 answers 2 answers
2.3k
2.3k views
Arjun asked Oct 18, 2016
2,340 views
int foo(int n) { if(n 10000) return 1; int sum = 0, i; for( i = 0; i < n; i++) { sum += i; } return sum; }The value returned by the above function is$\Theta\left(n^2\rig...
1 1 vote
4 4 answers
5.2k
5.2k views
Arjun asked Oct 18, 2016
5,236 views
Which of the following statements is true regarding C language?S1: C is a functional languageS2: C is a declarative languageS3: C is a procedural languageS4: C is a struc...