Open In App

C Program To Print Your Own Name

Last Updated : 10 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Printing your own name means displaying your name on the computer screen. In this article, we will learn how to print your own name using a C program.

Examples

Input: name = “Rahul”
Output: Rahul
Explanation: The program prints “Rahul” to the screen.

Input: name = “Vikas”
Output: Vikas
Explanation: The program prints “Vikas” to the screen.

The simplest way to print something is to use the printf() function. You can provide your name in the form of string to printf() function and it will print it on the output screen.

Syntax of printf

printf("your_name_here")

Program to Print Your Own Name Using printf

C
// C Program to Print Your Own Name using printf
#include <stdio.h>

int main() {
  
    // Printing your name "Rahul" on the output screen
    printf("Rahul");

    return 0;
}

Output
Rahul

Other Different Ways to Print Your Own Name in C

Apart from the printf function, we can also print our name on the screen using other methods provided in C:

We can use scanf() function to take the name as input from the user and store it in a character array. We can then use printf function to print the name on the screen.

Syntax of scanf()

scanf("%s", charArr);

Program to Print Your Own Name by Taking it as Input

C
// C Program to Print Your Own Name using scanf and printf
#include <stdio.h>

int main() {
  
    // Defining string (character array) assuming 100
    // characters at max
    char name[100];

    // Taking input from the user
    printf("Enter Your Name: ");
    scanf("%s", name);

    // Printing your name to the screen
    printf("Your Name: %s\n", name);

    return 0;
}


Output

Enter Your Name: Rahul
Your Name: Rahul

The puts() function is another function that is used to print the given string to the output screen. It is also defined inside the <stdio.h> header file.

Syntax of puts()

puts("name");

Program to Print Your Own Name Using puts()

C
// C Program to Print Your Own Name using puts
#include <stdio.h>

int main() {
  
    // Print the name "Vikas" on the output screen
    puts("Vikas");

    return 0;
}

Output
Vikas

The fputs() function is used for file output. We can redirect this function to standard output buffer “stdout” to print your name on the console screen.

Syntax

fputs("name", stdout);

Program to Print Your Own Name Using fputs()

C
// C Program to Print Your Own Name using fputs
#include <stdio.h>

int main() {
  
    // Print the name "Robert" on the screen
    fputs("Robert", stdout);

    return 0;
}

Output
Robert

The fprintf() function is used also for file output. It is similar to the printf function, but we need to specify the output buffer in the function which in this case is stdout.

Syntax

fprintf("name", stdout);

Program to Print Your Own Name Using fprintf()

C
// C Program to Print Your Own Name using fprintf
#include <stdio.h>

int main() {
    // Print the name "Robert" on the screen
    fputs("Robert", stdout);

    return 0;
}

Output
Robert

The write() function performs the write system call to the operating system which is used to write some data to some specific file. We can use this print your name by directly writing it to the output buffer.

Syntax of write()

write(STDOUT_FILENO, "name", strlen("name"));

STDOUT_FILENO is the file descriptor for the stdout buffer. Its value is 1 by standard.

Program to Print Your Own Name Using Write System Call

C
// C Program to Print Your Own Name using write system call
#include <unistd.h>
#include <string.h>

int main() {
  
    // Define the name string
    char name[] = "Gabriel";

    // Print the name using the write system call
    write(STDOUT_FILENO, name, strlen(name));

    return 0;
}

Output
Gabriel


Next Article

Similar Reads