C Language Concepts MCQs: 12 Solved Questions with Explanations

Solve 12 C concept MCQs on strings, macros, escape sequences, declarations and sequencing. Each answer names the rule and the wrong option it is built to catch.

KnowledgeGate Team

Exam prep & CS education

Updated 5 Aug 20268 min read

Company coding-aptitude rounds often use short C programs where one language rule decides the answer. A hidden null terminator, unsafe macro expansion, or cursor-control character can turn an easy-looking question into a trap.

Attempt each question on paper before reading its answer. Write out the array bytes or the expanded macro text first, because every wrong option here is built to reward a shortcut that looks reasonable.

Strings and the byte the compiler adds for free

A C string is a character array ending in \0. The terminator occupies one byte, sizeof sees it, strlen stops before it, and library functions depend on it.

Q1. Storage used by a string

Asked in: Hexaware 2023, CoCubes 2023.

How many bytes are occupied by the following string?

char str[] = "abcdef";

(a) 5 bytes

(b) 6 bytes

(c) 7 bytes

(d) 8 bytes

Answer: (c) 7 bytes. Six visible characters occupy six bytes. The compiler adds \0, so the total is 6 + 1 = 7 bytes. Option (b) misses the terminator. Open the worked solution in the Hexaware set.

Q2. Changing one character in an array

Asked in: Hexaware 2024, CoCubes 2024.

What will be the output of the following code snippet?

char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};
str[0] = 'y';
printf("%s", str);

(a) hello

(b) yello

(c) ello

(d) hyello

Answer: (b) yello. The initializer creates a mutable array. str[0] = 'y' replaces h, then %s prints through \0. Option (d) tempts anyone who reads the assignment as an insertion, but it overwrites in place, so the length does not change. A string literal reached through char *p must not be modified. Open the worked solution in the Hexaware set.

Q3. Concatenating two strings

Asked in: Hexaware 2025, CoCubes 2025.

What is the function to concatenate two strings?

(a) stradd()

(b) strcon()

(c) strjoin()

(d) strcat()

Answer: (d) strcat(). strcat(dest, src) appends the source at the destination's terminator. The destination needs enough space because strcat does not allocate it. The other names are not standard-library functions. Open the worked solution in the Hexaware set.

Q4. Comparing strings

Asked in: Hexaware 2024, CoCubes 2024.

What will the following code output?

char str1[] = "world";
char str2[] = "hello";
if(strcmp(str1, str2) > 0)
printf("str1 is greater");
else
printf("str2 is greater");

(a) str1 is greater

(b) str2 is greater

(c) Equal strings

(d) Compilation error

Answer: (a) str1 is greater. The first characters differ: w is 119 and h is 104, so strcmp returns a positive value and the comparison with 0 succeeds. Option (b) tempts you if you read a positive return as a verdict on the second string, but the sign describes the first argument. Compare the result against 0 rather than testing it for truth. Open the worked solution in the Hexaware set.

The preprocessor pastes text, it does not compute

#define performs textual substitution before compilation. Operator precedence still acts on the expanded text, and a later macro definition applies to later uses.

Q5. An unparenthesised macro

Asked in: TCS 2024.

#define prod(a,b) a*b
int main()
{
int x=3,y=4;
printf("%d",prod(x+2,y-1));
return 0;
}

(a) 10

(b) 20

(c) 15

(d) 0

Answer: (a) 10. The call expands to x+2*y-1, not (x+2)*(y-1). First 2*4 = 8, then 3 + 8 - 1 = 10. The trap answer 15 comes from (3+2)*(4-1) = 5*3. Write ((a)*(b)) to protect the arguments. Open the worked solution in the placement coding set.

Q6. Redefining a macro

#define a 10
int main()
{
#define a 50
printf("%d",a);
getchar();
return 0;
}

(a) 10

(b) 60

(c) 50

(d) error

Answer: (c) 50. A different redefinition without #undef requires a diagnostic. Compilers typically warn and apply the latest definition to later uses. Here a therefore expands to 50, not a hard error. Open the worked solution in the C Language practice set.

Output tricks with goto and cursor-control escapes

goto transfers control directly to a label. Escape sequences such as \n, \b, and \r move the terminal cursor instead of printing their letters.

Q7. Jumping over a statement

Asked in: TCS 2026.

What is the output of the following program?

void main()
{
printf("1");
goto xyz;
printf("2");
xyz:
printf("3");
}

(a) 1 2

(b) 23

(c) 1 2 3

(d) 13

Answer: (d) 13. The first call prints 1. goto xyz jumps straight to the label, so printf("2") never runs, and the call at the label prints 3. The two digits arrive with no space between them, which is what separates option (d) from options (a) and (c). A label needs a colon and not a semicolon: xyz; would be read as an ordinary expression statement instead of a jump target. Open the worked solution in the placement coding set.

Q8. Backspace and carriage return

Asked in: TCS 2026.

Predict the output of the given below code

int main()
{
printf("\new_c_question\by");
printf("\rTCS");
getchar();
return 0;
}

(a) new_c_questioy

(b) ew_c_questioy

(c) TCSc_questioy

(d) ew_c_questioy TCS

Answer: (c) TCSc_questioy. The opening \n is one escape, so the text is ew_c_question. \b moves onto the final n, which y overwrites to make ew_c_questioy. Then \r returns to column 0 and TCS overwrites ew_, leaving TCSc_questioy. Open the worked solution in the placement coding set.

Column trace for Q8 showing how backspace and carriage return overwrite characters to produce the output TCSc_questioy.

C declarations: empty parameter lists and old compiler extensions

One of the two rules below trips up C++ intuition, and the other survives only in a long-dead compiler extension. Check that a declaration is legal C before you start tracing the body.

Q9. An empty parameter list

In C, what is the meaning of following function prototype with empty parameter list

void fun()
{
/* .... */
}

(a) Function can only be called without any parameter

(b) Function can be called with any number of parameters of any types

(c) Function can be called with any number of integer parameters.

(d) Function can be called with one integer parameter.

Answer: (b). Bare () provides no parameter-type prototype, so the compiler has no declared argument count or types to check. Option (a) is tempting because in C++ empty parentheses do mean no arguments. Write (void) when you mean no arguments, which is what C23 finally made empty parentheses mean as well. Open the worked solution in the C Language practice set.

Q10. Pascal syntax inside C

int main()
{
int i=10;
void pascal f(int,int,int);
f(i++, i++, i++);
printf(" %d",i);
return 0;
}
void pascal f(integer :i,integer:j,integer :k)
{
write(i,j,k);
}

(a) 11 11 11

(b) 11 12 13

(c) Compiler Error

(d) 10 11 12

Answer: (c) Compiler Error. Some old compilers used pascal as a calling-convention extension, but it never made Pascal syntax valid C. integer :i is not a C parameter declaration, and write() is not a standard function. The program fails before i++ order matters. Open the worked solution in the C Language practice set.

Evaluation order, sequence points, and pseudocode

C orders evaluation only at particular boundaries. Two modifications to one object without the required sequencing make an observed output meaningless.

Q11. Two modifications without sequencing

Asked in: Infosys 2023.

What will be the output of the following code?

int main ()
{
int x = 4, y = 0;
int z;
z = (x++ + ++y + y++ , x++);
printf ("%d\n", z);
return 0;
}

(a) 5

(b) 0

(c) Compiler error

(d) Undefined behaviour, as the order of evaluation can differ

Answer: (d). The comma operator sequences its left expression before the right x++, but the fault is inside the left side. ++y and y++ both modify y, while + does not order its operands. The behaviour is undefined, even if one compiler prints 5. Open the worked solution in the Infosys set.

Q12. Reading decimal digits as octal

What will be the value of s if n = 127 is given as input to the following pseudocode?

Read n
i = 0, s = 0
Function Sample(int n)
    while (n > 0)
        r = n % 10
        p = 8^i
        s = s + p*r
        i++
        n = n / 10
    End While
    Return s
End Function

(a) 27

(b) 187

(c) 87

(d) 120

Answer: (c) 87. The loop removes decimal digits but weights them with powers of 8:

  1. First iteration: r = 7, p = 8^0 = 1, so s = 0 + 1*7 = 7.

  2. Second iteration: r = 2, p = 8^1 = 8, so s = 7 + 8*2 = 23.

  3. Third iteration: r = 1, p = 8^2 = 64, so s = 23 + 64*1 = 87.

As a separate check, reading 127 as an octal numeral gives 1*64 + 2*8 + 7 = 64 + 16 + 7 = 87, the same value. Read 8^i as exponentiation and not as C's bitwise XOR: the XOR reading gives 8*7 + 9*2 + 10*1 = 84, which is not on the option list. Open the worked solution in the C Language practice set.

How company tests use C language concepts

Short output programs hide one decisive token, such as the macro in Q5, goto in Q7, or cursor movement in Q8. String facts appear directly or inside tiny programs. Q6, Q9, and Q11 reward the language rule instead of a guessed trace. Q12 tests whether you can separate digit extraction from base weighting.

Build a one-page sheet titled “C rules that beat intuition”. Put the terminator byte, macro parentheses, \b and \r, () versus (void), and sequencing on it. Then rehearse those rules against the previous-year questions themselves. The case for practising PYQs instead of buying another generic question bank explains why that focused loop works.

The short version and your next step

  • Every string needs one hidden byte for \0.

  • Macros paste text, so parenthesise the full expansion and every argument.

  • \b and \r move the cursor without erasing the line.

  • Bare () does not declare a no-argument prototype in C. Use (void).

  • Multiple unsequenced modifications to one object cause undefined behaviour.

Work through the rest of the C Language Concepts questions inside the C Language course, which combines concepts, MCQs, and coding practice. If you are sitting company tests across several languages, the coding and DSA courses for placements cover C alongside the rest. For drills shaped like the Hexaware, TCS and Infosys questions above, start from the company-specific placement courses.