in Programming in C recategorized by
1,187 views
1 vote
1 vote
In which of the following case(s) character array must end with null char?
  1. char c[] = "GATE";
  2. char c[] = {'2', '0', '2', '3'};
  3. char c[4] = "GATE";
  4. char c[16] = "2023";
in Programming in C recategorized by
1.2k views

4 Comments

1. I didn't say "it will result in runtime error"; I said "it may result in runtime error".

2. Did you run those programs in all possible compilers?

3. "printf will print until array is finished or it encounters an '\0' " this is not correct.

4. If string is not "null terminated" then printf behaviour is undefined.

0
0
2
2
I got this now. Thanks.
1
1

2 Answers

6 votes
6 votes

Answer: A, D

Option A: 

char c[] = "GATE";

Since size of array is not mentioned so ‘\0’ must be at the end of the string.

 

Option B: 

char c[] = {'2', '0', '2', '3'};

We initialized the array so array size will be 4.

 

Option C: 

char c[4] = "GATE";

Size of array is provided so this array will store 4 characters. No ‘\0’ at the end.

 

Option D:

char c[16] = "2023";

Since size of the array is 16 and we only initialized with 4 characters compiler will add ‘\0’ to all the remaining positions.

by

2 Comments

in option d, is null character added to all remaining places because array is storing char? in int array 0 is stored to places, so i had doubt if null will only be added after string and rest of positions will have 0.
0
0

@Godlike NULL char have ascii value 0. Since it is char array then it we be treated as NULL char.

If it was array of int then we would treat it as 0.

2
2
0 votes
0 votes

1.) char a[] = “GATE” : NULL IS THERE

2.) char a[] = {‘1’,’2’,’3’} : NULL IS NOT THERE : Bcoz size is 3 not 4

3.) size is 4 no space for null.

4.) We have null after ‘3’ till end of array.

edited by

4 Comments

@Udhay_Brahmi you can simply verify this using any c compiler right 

0
0

@Udhay_Brahmi bro there seems to be an other guy with user name as jiren and u tagged him and not me 😅

please use square bracket first and then underscore then start with capital J

like 

0
0
😂😂 ok
0
0
Answer:

Related questions