C Program to Print Your Name

C Program to Print Your Name

A student has a two-word name. Reading only one whitespace-delimited word loses part of that name.

Read a bounded line with fgets, remove its stored newline and print it through a fixed %s format so spaces inside a name remain intact.

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 print a student name uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Run the program with Ravi Shah, including the space.

Print a Student Name: follow the data

  1. Given data

    A student has a two-word name. Reading only one whitespace-delimited word loses part of that name.

  2. Rule

    Read a bounded line with fgets, remove its stored newline and print it through a fixed %s format so spaces inside a name remain intact.

  3. Observed result

    Student: Asha Rao

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

Understand the operation

The 32-element buffer can hold at most 31 input characters plus a terminator. A successful read may contain a newline, which strcspn locates for removal.

The program detects an incomplete overlong line rather than pretending the retained prefix is the full name. A name is text; it should not become a printf format.

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>
#include <string.h>
int main(void) {
  char name[32];
  if (fgets(name, sizeof name, stdin) == NULL) return 1;
  if (strchr(name, '\n') == NULL && !feof(stdin)) { puts("Name is too long"); return 0; }
  name[strcspn(name, "\n")] = '\0';
  printf("Student: %s\n", name);
  return 0;
}

Example input

Asha Rao

Expected output

Student: Asha Rao
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
#include <string.h>
int main(void) {
  char name[32];
  if (fgets(name, sizeof name, stdin) == NULL) return 1;
  if (strchr(name, '\n') == NULL && !feof(stdin)) { puts("Name is too long"); return 0; }
  name[strcspn(name, "\n")] = '\0';
  printf("Student: %s\n", name);
  return 0;
}

The supplied line is Asha Rao followed by a newline.

The newline position becomes a terminator, so the output format displays both words and supplies one new newline.

Follow the worked calculation

  1. Start with the sample

    The supplied line is Asha Rao followed by a newline.

  2. Apply the rule

    The newline position becomes a terminator, so the output format displays both words and supplies one new newline.

  3. Check the result

    Student: Asha Rao

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

Compare printf, puts, fputs and platform-specific write

For already validated terminated text, printf("%s\n", name) adds an explicit newline, puts(name) adds one automatically, and fputs(name, stdout) writes only the supplied string. These are standard C stream operations.

The older page also used write. write belongs to the POSIX operating-system API rather than the ISO C library; it takes a file descriptor and a byte count, can write fewer bytes than requested, and requires platform-specific headers and error handling. It is not a portable replacement for puts. POSIX write contract.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
#include <string.h>
int main(void) {
  char name[32];
  if (fgets(name, sizeof name, stdin) == NULL) return 1;
  if (strchr(name, '\n') == NULL && !feof(stdin)) { puts("Name is too long"); return 0; }
  name[strcspn(name, "\n")] = '\0';
  printf("Student: %s\n", name);
  return 0;
}

Run the program with Ravi Shah, including the space.

Worked answer

The same source handles both words because fgets reads a line rather than stopping at the first internal space. The fixed output format keeps data separate from format instructions.

C17 · complete program

#include <stdio.h>
#include <string.h>
int main(void) {
  char name[32];
  if (fgets(name, sizeof name, stdin) == NULL) return 1;
  if (strchr(name, '\n') == NULL && !feof(stdin)) { puts("Name is too long"); return 0; }
  name[strcspn(name, "\n")] = '\0';
  printf("Student: %s\n", name);
  return 0;
}

Example input

Ravi Shah

Expected output

Student: Ravi Shah
Run in C compiler →

Recognize the important boundaries

This byte limit is not a promise about the number of user-perceived Unicode characters. Choose a text-encoding policy for international names.

An empty line is a separate application rule; if names are mandatory, reject the resulting empty string before saving.

Check your understanding

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

Why prefer a line read for a two-word name?
Choose one answer. You can try again.

Answer and explanation

It preserves the internal space within the bounded line.

Correct. A single %s input word would stop at whitespace.

Use the idea independently

Read a bounded line with fgets, remove its stored newline and print it through a fixed %s format so spaces inside a name remain intact.

Continue with bounded input contracts.

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 Read and Print an Integer →