- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
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
Loading comments...