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.
Explain
Explain why print a student name uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Run the program with Ravi Shah, including the space.
Print a Student Name: follow the data
Given data
A student has a two-word name. Reading only one whitespace-delimited word loses part of that name.
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.
Observed result
Student: Asha Rao
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
Start with the sample
The supplied line is Asha Rao followed by a newline.
Apply the rule
The newline position becomes a terminator, so the output format displays both words and supplies one new newline.
Check the result
Student: Asha Rao
Student: Asha Rao
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.
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.