Karnataka KSEAB SSLC JTS Programming in ANSI C study materials
Karnataka KSEAB SSLC JTS Programming in ANSI C study resources

The Name That Would Not Fit

Karnataka KSEAB SSLC JTS Programming in ANSI C · Put your knowledge to work in context.

All case studies

The Name That Would Not Fit

A student in Hubballi is writing a program for a school fest registration desk that stores a participant's name in char name[10]; and then compares it with the string "KSEAB". The name typed at the keyboard is Rajarajeswari, which has 13 characters. The program uses scanf("%s", name); and later if (name == "KSEAB") printf("Match");. On one computer it prints garbage characters after the name; on another it crashes only when the comparison line runs. The student also writes char *tag = "Karnataka"; then tag[0] = 'k'; and the program terminates with a runtime error. Finally, printf("%d", strlen(name)) prints a number that keeps changing between runs, while sizeof(name) always prints 10. The teacher wants these four symptoms connected to arrays, string termination and pointer storage.

1. Explain the garbage output and the crash when Rajarajeswari is entered. What is the safe way to read the name?

Read the worked answer

char name[10] has room for 9 characters plus the terminating '\0'. scanf("%s", name) has no width limit, so typing Rajarajeswari (13 characters) writes 14 bytes into a 10-byte array. The extra bytes overwrite whatever follows name in memory - other local variables, saved registers, or a stack canary - which explains the garbage text on one machine and the crash on another.

This is undefined behaviour, so "it works on my computer" is not evidence of correctness. Safe options:

fgets(name, sizeof name, stdin);   /* strip newline afterwards */
/* or */
scanf("%9s", name);               /* 9 chars + '\0' */

With fgets, remember that the newline may be stored; remove it with name[strcspn(name, "\n")] = '\0'; if it matters. The array size must always include space for the terminator.

2. Why does if (name == "KSEAB") not compare the text? Rewrite it correctly and explain what is actually being compared.

Read the worked answer

In name == "KSEAB", both operands are addresses, not contents. name decays to a pointer to its first element, and the string literal is a separate array stored elsewhere. The comparison asks "are these the same address?", which is essentially always false, so Match is not printed even when the text is identical.

The correct test compares characters up to the null terminator:

if (strcmp(name, "KSEAB") == 0)
    printf("Match");

strcmp returns 0 when the strings are equal, a negative value when the first is lexicographically smaller, and a positive value when it is larger. The crash "only when the comparison line runs" is consistent with earlier overflow damage: the comparison is simply the next place where the corrupted memory is touched.

3. Explain the runtime error from tag[0] = 'k', and why strlen(name) changes while sizeof(name) stays 10.

Read the worked answer

char *tag = "Karnataka"; makes tag point at a string literal, which normally lives in read-only memory. tag[0] = 'k' tries to modify that literal through the pointer; this is undefined behaviour and commonly produces a segmentation fault. If modification is needed, copy the characters into an array:

char tag[] = "Karnataka";
tag[0] = 'k';

strlen(name) changes because the earlier overflow destroyed the terminating '\0'. strlen scans memory until it happens to find a zero byte, so the count depends on whatever bytes lie beyond the array. sizeof(name), by contrast, is fixed at 10 at compile time because it is the size of the array object, not the length of the string stored in it. Confusing sizeof with strlen is a common source of off-by-one errors.