C Hello World Program

C Hello World Program

A classroom’s first practice program needs one readable greeting line without any numeric calculations.

puts writes a valid terminated string and then appends a newline. printf gives explicit control over formatting and whether a newline is included.

Before this exercise, review storing values and reading and checking input. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why greeting program uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use Hill School as the destination in the greeting.

Greeting Program: follow the data

  1. Given data

    A classroom’s first practice program needs one readable greeting line without any numeric calculations.

  2. Rule

    puts writes a valid terminated string and then appends a newline. printf gives explicit control over formatting and whether a newline is included.

  3. Observed result

    Hello, River School!

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

The stdio.h declaration tells the compiler how puts is called. Its string argument is text data; the function appends the line ending.

Use printf("%s\n", message) when the same output must be described through a formatting contract. Do not pass untrusted text as the format itself.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  puts("Hello, River School!");
  return 0;
}

Expected output

Hello, River School!
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  puts("Hello, River School!");
  return 0;
}

main passes the school greeting string to puts.

puts writes its characters and then a newline; return zero terminates the program successfully.

Follow the worked calculation

  1. Start with the sample

    main passes the school greeting string to puts.

  2. Apply the rule

    puts writes its characters and then a newline; return zero terminates the program successfully.

  3. Check the result

    Hello, River School!

    Hello, River School!
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  puts("Hello, River School!");
  return 0;
}

Use Hill School as the destination in the greeting.

Worked answer

Changing only the literal text changes the visible greeting. The newline remains supplied by puts rather than a manually included escape.

C17 · complete program

#include <stdio.h>
int main(void) {
  puts("Hello, Hill School!");
  return 0;
}

Expected output

Hello, Hill School!
Run in C compiler →

Recognize the important boundaries

A printf format built directly from user-controlled text can interpret unintended conversions. Use a fixed format and supply the text as an argument.

Standard output can fail; a robust application should handle the return values required by its output-delivery contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Does puts append a newline?
Choose one answer. You can try again.

Answer and explanation

Yes.

Correct. It writes the string followed by a newline.

Use the idea independently

puts writes a valid terminated string and then appends a newline. printf gives explicit control over formatting and whether a newline is included.

Continue with source and executable basics.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Print Your Name →