Skip to content

Exercise 2 of 10 · Variables

Name Tag

What you will make

A bordered name-tag card with your own name across three of its lines, every one of them fed by a single line of code.

The one new idea: A variable is declared with a type, and holds one value under a name

Real programs mention the same value again and again — a name, a price, a filename. Storing it once under a name means you fix it in one place instead of hunting through every printf call that uses it.

Go straight to the code ↓

What a variable is, in C

C will not let you make a variable without saying what kind of value it will hold. char name[] says two things at once: char is the type of value stored — one byte apiece, the type C uses for a run of text — and [] says there will be more than one of them, laid out in a row, with the compiler counting how many from whatever you initialise it with.

C
char name[] = "Sam";

That line makes an array of four bytes: the letters S, a, m, and one invisible byte with the value zero that marks where the text ends. From then on, writing name anywhere in your code hands that whole piece of text back to you, and %s inside a printf call is the placeholder that knows how to print it.

Think of a labelled shelf built to hold exactly this delivery. You stock it once. Anyone who reaches for it afterwards gets whatever is sitting there right now. Restock it with something else and every later reach finds the new thing — you never have to go and tell each printf call separately.

One detail decides everything here: the quotes.

  • printf("name") prints the four letters n, a, m, e. Quotes mean this exact text.
  • printf("%s", name) prints whatever name is holding. No quotes around the variable means look this name up.

A worked example

A different card, so the answer to this exercise is not given away:

C
#include <stdio.h>

int main(void) {
    char city[] = "Pune";

    printf("Weather report\n");
    printf("City: %s\n", city);
    printf("Have a good day in %s\n", city);
    return 0;
}

Running it prints:

Output
Weather report
City: Pune
Have a good day in Pune

The value "Pune" is written once, at the top, and used on two later lines through %s. Change that first line to char city[] = "Nagpur"; and both of those lines say Nagpur instead. The printf calls themselves never need editing — they only ever ask %s to print whatever city is holding right now.

Your turn

The editor already prints a name-tag card: a border, a header, a greeting, an owner line and a signature. Run it first, before changing anything, and you will see the card appear with the placeholder in it.

Now find the short line near the top:

C
char name[] = "YOUR NAME";

Replace YOUR NAME with your own name, keeping the quotes, and press Run again.

Three lines of the card change at once, because all three printf calls ask the same variable, through %s, for its value. You edited one line and the card followed.

Try it a second time with a nickname. That is the whole idea of a variable, and it is worth feeling rather than just reading.

If something goes wrong

The most common slip is dropping the quotes off your value, as in char name[] = Sam;. The compiler then treats Sam as a name it is supposed to already know about, finds nothing declared under it, and reports an undeclared identifier before a single line runs.

The other one is quoting the variable by accident inside a printf call, as in printf(" Welcome, %s\n", "name");. Your card then greets someone called name on every row, because quotes turn it into ordinary text rather than a lookup.

Neither is damage. Read the line the message points at, put the quotes back where they belong, and run it again.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Taking the quotes off the value, writing char name[] = Sam;
Without quotes the compiler reads Sam as the name of something in the program — a variable or a function — rather than as text. It has never heard of anything called Sam, so it stops with an error about an undeclared identifier, and nothing runs.
Quoting the variable inside the printf calls, e.g. printf(" Welcome, %s\n", "name");
%s prints whatever piece of text it is handed, and "name" in quotes is the four letters n-a-m-e rather than a lookup. The card would greet someone literally called name on every line instead of using the value you stored.
Spelling the variable differently in one place, such as Name instead of name
C matches names letter for letter, including capitals, so it treats Name and name as two completely different things. The compiler stops on the line that uses the one it has never declared, reporting an undeclared identifier.
Forgetting the %s in one of the printf calls, e.g. printf(" Welcome, ", name);
A printf call with no placeholder in its string ignores any extra arguments after it — name is simply never used, and that row of the card prints with nothing after the label.

Longer explanation: read the full lesson. Want a blank editor instead? Open the C playground.