Quiz di C: Preprocessor & Macros

The C preprocessor rewrites your code before the compiler sees it, which makes macros powerful and easy to get wrong. This quiz covers missing parentheses, arguments evaluated twice, # and ##, conditional compilation and the idioms that keep macros safe.

Medio 10 domande ~6 minuti Superato con il 80% C17

Scegli una modalità

Stai giocando come ospite: accedi per salvare XP e serie.

Domanda di esempio

Domanda 1 di 10

What does this print?

#include <stdio.h>

#define SQUARE(x) x * x

int main(void) {
    printf("%d\n", SQUARE(2 + 3));
    return 0;
}
Mostra la risposta

D. 11 Macros substitute text, so this becomes 2 + 3 * 2 + 3, which is 2 + 6 + 3 = 11. Write #define SQUARE(x) ((x) * (x)).

Cosa copre questo quiz

  • Why macro bodies and parameters need parentheses
  • Arguments that are evaluated more than once
  • Stringizing with # and token pasting with ##
  • #ifdef, #undef and predefined macros
  • Include guards and the do { ... } while (0) idiom

Domande frequenti

Serve un account?

No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.

Posso rifarlo?

Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.