C Program to Print Your Own Name - printf, scanf, puts, fputs & write

Print Name Using printf()

#include <stdio.h>
int main() {
    printf("John Doe\n");
    return 0;
}
John Doe
        

The simplest way to display your name is by supplying a string literal to printf(). When executed, this prints the exact characters you provide.

Print Name After User Input

#include <stdio.h>
int main() {
    char name[50];
    printf("Enter your name: ");
    scanf("%49s", name);
    printf("Your name is %s\n", name);
    return 0;
}
Enter your name: Alice
Your name is Alice
        

This version allocates an array of characters, prompts for input and captures a single word using scanf(). The name is then printed back with another call to printf().

Print Name Using puts()

#include <stdio.h>
int main() {
    puts("John Doe");
    return 0;
}
John Doe
        

The puts() function, defined in <stdio.h>, outputs the given string followed by a newline automatically.

Print Name Using fputs()

#include <stdio.h>
int main() {
    fputs("John Doe", stdout);
    return 0;
}
John Doe
        

fputs() writes a string to the specified file stream; when the stream is stdout it produces output on the screen.

Print Name Using write()

#include <unistd.h>
int main() {
    write(1, "John Doe", 9);
    return 0;
}
John Doe
        

The write() function is a low‑level system call that writes raw bytes to a file descriptor; 1 corresponds to the standard output file descriptor.

There are many ways to print a name in C. Passing a string literal to printf() is the most direct approach. For interactive programs you can store the user’s name in a character array using scanf() and then echo it back. The puts() function prints a string followed by a newline, while fputs() writes a string to a specified stream. Finally, the write() system call writes a given number of bytes to a file descriptor such as standard output.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...