GATE 2015 COMPUTER SCIENCE & INFORMATION TECH. – CS – C Programming | Q. 11 (Session – 3)

Question Number : 11 MCQ
Consider the following C program segment.
#include <stdio.h>

int main()
{
    char s1[7] = "1234", *p;
    p = s1 + 2;
    *p = '0';
    printf("%s", s1);
}
What will be printed by the program?
(A) 12
(B) 120400
(C) 1204
(D) 1034

🔹 Understanding the Character Array s1[7]

The statement char s1[7] = “1234”; declares a one-dimensional character array named s1.

Since the array size is 7, it contains 7 character elements, from s1[0] to s1[6].

The string “1234” contains four characters. A string also needs a terminating ‘\0’ character. Therefore, the first five elements contain ‘1’, ‘2’, ‘3’, ‘4’ and ‘\0’.

Array Element Stored Character
s1[0] ‘1’
s1[1] ‘2’
s1[2] ‘3’
s1[3] ‘4’
s1[4] ‘\0’
s1[5] ‘\0’
s1[6] ‘\0’
📌 Why are s1[5] and s1[6] also ‘\0’?

The array has space for 7 elements, but the initializer “1234” provides only four characters plus the terminating ‘\0’. The remaining elements of a partially initialized array are automatically initialized to zero. For a character array, a zero value is represented by ‘\0’.

💡 One important point about s1:

s1 is the name of the character array. When the array name is used in an expression, it generally converts to the address of its first element. Thus, in this case, s1 refers to the address of s1[0], which contains ‘1’.

So, for understanding the program, we can think of s1 as pointing to the first character ‘1’. However, strictly speaking, s1 is an array name, not a pointer variable.

📌 Partial Initialization of an Array

Here, the array has space for 7 elements, but the initializer provides values for only some of the elements. The remaining elements are automatically initialized to zero.

For a character array, a zero value is represented by ‘\0’.

Let us understand this rule with two simple examples.

Example 1: Integer Array
int a[5] = {1, 2};

The array has 5 elements, but only the first two elements are initialized. Therefore, the remaining elements are automatically initialized to 0.

Array Element Value
a[0] 1
a[1] 2
a[2] 0
a[3] 0
a[4] 0
Example 2: Character Array
char a[5] = {‘A’, ‘B’};

The array has 5 elements, but only the first two elements are initialized. The remaining elements are automatically initialized to zero. For a character array, zero is represented by ‘\0’.

Array Element Stored Character
a[0] ‘A’
a[1] ‘B’
a[2] ‘\0’
a[3] ‘\0’
a[4] ‘\0’

🔍 Difference Between 0, ‘0’ and ‘\0’

In C, 0, ‘0’ and ‘\0’ are three different things. Understanding this difference is important when working with character arrays and strings.

📌 A Simple Example
#include <stdio.h>

int main()
{
    printf("%d\n", 0);
    printf("%d\n", '0');
    printf("%d\n", '\0');

    return 0;
}
Output:
0
48
0
Expression Meaning Value
0 Integer zero 0
‘0’ Character zero. Its character code in ASCII is 48. 48
‘\0’ Null character. Its value is zero. 0
💡 Remember:

0 is a number, ‘0’ is the character zero, whereas ‘\0’ is the null character.

Notice that ‘0’ and ‘\0’ are not the same. In ASCII, ‘0’ has the value 48, while ‘\0’ has the value 0.

The ‘\0’ character is especially important for strings because functions such as printf(“%s”, …) use it to identify the end of a string.

🔹 Understanding p = s1 + 2 and *p = ‘0’

First, consider the statement:

p = s1 + 2;

We already know that s1 represents the address of the first element of the array, s1[0].

Therefore, s1 + 2 means moving 2 character positions forward from s1[0].

So, s1 + 2 gives the address of s1[2]. The pointer p is therefore made to point to s1[2].

📌 After p = s1 + 2;
s1[0]
‘1’
s1[1]
‘2’
s1[2]
‘3’
↑ p
s1[3]
‘4’
s1[4]
‘\0’
s1[5]
‘\0’
s1[6]
‘\0’

Now consider the next statement:

*p = ‘0’;

The * operator dereferences the pointer. In simple terms, *p means the value stored at the memory location to which p points.

Since p points to s1[2], *p refers to s1[2].

Therefore, *p = ‘0’; changes the value of s1[2] from ‘3’ to ‘0’.

In short: p → s1[2] and therefore *p = ‘0’ changes s1[2] to ‘0’.
✅ Array After Executing Both Statements

After executing p = s1 + 2; and *p = ‘0’;, the array becomes:

Array Element Stored Character
s1[0] ‘1’
s1[1] ‘2’
s1[2] ‘0’
s1[3] ‘4’
s1[4] ‘\0’
s1[5] ‘\0’
s1[6] ‘\0’
s1 = “1204”

🔹 Understanding printf(“%s”, s1);

The statement

printf(“%s”, s1);

uses printf() to display the contents of the character array s1.

📌 What does %s mean?

%s is the format specifier used by printf() to print a string.

When %s is used, printf() starts reading characters from the address supplied as the corresponding argument and continues until it encounters the null character ‘\0’.

🔍 How does printf(“%s”, s1) read the array?

After executing the statements p = s1 + 2; and *p = ‘0’;, the array contains:

Element Character
s1[0] ‘1’
s1[1] ‘2’
s1[2] ‘0’
s1[3] ‘4’
s1[4] ‘\0’
s1[5] ‘\0’
s1[6] ‘\0’

printf() starts from s1[0] and prints:

‘1’ → ‘2’ → ‘0’ → ‘4’ → ‘\0’

When printf() reaches ‘\0’ at s1[4], it stops printing. Therefore, s1[5] and s1[6] are not printed.

💡 Important:

The character stored in s1[2] is ‘0’ (character zero), not ‘\0’ (null character).

Therefore, ‘0’ is printed as an ordinary character. The ‘\0’ at s1[4] is what tells printf(“%s”, s1) to stop.

✅ Final Output
1204

Hence, the program prints 1204. The correct option is (C) 1204.

0

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »