Write a Program to Display your Name in C [Updated 2024]

To write a Program to Print your Name in C, we have the Print/printf() function in C Language.

You just have to write your Name inside the Print/printf() function and you will be able to Print or Display your Name.

my.c

#include<stdio.h>  
void main()  
{ 
	printf("Name: John");
}

Results

Name: John

As you can see in the example,
We can use C Language's printf() function to Print any String or Number.

Steps to Print Name in C:

1) First Open Your C Compiler
(For Example Code::Blocks)
2) Create a New File And Save that File With a name.
(for example myname.c)
3) Write the code As shown Above.
4) Save and Run your C Program.
5) Done.
Now you will be able to see your Name there.

Youtube: Write a Program to Display your Name in C [Updated 2024]
Play Youtube Video

C Code explanation in detail

#include<studio.h>
By using #include<studio.h>, we are including all the Standard Input-Output Header files.

So #include going to include
stud standard Input Output
.h Header file.


void main()
Here main() is our Main funtion, where we are using void because this function is not going to return any value.
We can also use int main() or char main() depending upon the type of value we are going to return.


printf()
By using printf(), we are Printing our Name or a String.
Where printf() function will take the value as input and Display it as a Output.

Write a Program to Display your Name in C [Updated 2024]
Ad

Need a Personal Tutor?

I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.

Less then 5$/hSee Plans100% Free Trial + Money Back Guaranty

FAQ: Write a Program to Display your Name in C

Write a C Program to input name and Display

In order to Input a Name and Display it in C, we need to use two very Important C Functions,
printf() and scanf().

Where by using printf() function we will print things like "Write your name", to tell our users to Enter their name.

Then by using scanf() function we will store their enterned name inside a C Variable myname.
By using scanf("%s", &myname), we are telling our compiler that we are expecting String "%s" value and we want to store that String value inside our variable "&myname".

At the end we will Display/Print the Name using printf() funtion.
Here we are using "%s" as our String Placeholder and printf() function will print our myname variable's value there.

my.c

#include<stdio.h>  
void main()  
{
	char myname[20];
	printf("Enter your Name: ");
	scanf("%s", &myname);
	printf("Your name is: %s", myname);
}

Results

Enter your Name: John
Your name is: John

Write a c program that will ask for a person’s name and his/her game score. then it will ask for a second person’s name and score. the program will print the winner’s name and also print by how many points that person won by comparing the scores.

First we need 2 String Variables for storing names of Player1 and Player2.
Then we need 3 Integer variables for storing scores of player1 and player2 and also third integer variable for storing the Score GAP.
After that we will get the Names and Store them inside our Variables using scanf().
After that we will use if conditions for comparing the Scores and at the End we will Display the Winner's name with the Score GAP.

score.c

#include<stdio.h>  
void main()  
{
  char player1[20];  
  char player2[20]; 
  int p1Score, p2Score, scoreGap;  
  printf("Player 1 Enter Your Name : "); 
  scanf("%s",&player1);
  printf("Player 1 Enter Score : "); 
  scanf("%d",&p1Score);
  printf("Player 2 Enter Your Name : "); 
  scanf("%s",&player2);  
  printf("Player 2 Enter Score : "); 
  scanf("%d",&p2Score);
  
  if(p1Score > p2Score){
      scoreGap = p1Score - p2Score;
      printf("%s WON by %d", player1, scoreGap);
  }else if(p1Score < p2Score){
      scoreGap = p2Score - p1Score;
      printf("%s WON by %d Score", player2, scoreGap);
  }else if(p1Score == p2Score){
      printf("Its a TIE");
  }
  
}

Results

Player 1 Enter Your Name : John
Player 1 Enter Score : 200
Player 2 Enter Your Name : Lisa
Player 2 Enter Score : 210
Lisa WON by 10

Write a C program to Print Name and Address

To print your Name and Address we can use Print Function (printf()).
Just write your name inside The C Print Function [printf("Name and Address");]
and you are DONE.

nameaddress.c

#include<stdio.h>  
void main()
{
  printf("My Name is John");
  printf("\nMy Address is : 21 Jump Street NY");    
}

Results

My Name is John
My Address is : 21 Jump Street NY
Ad

Need a Personal Tutor?

I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.

Less then 5$/hSee Plans100% Free Trial + Money Back Guaranty

Write a program to print your name at the center of the screen

To Print your name at the center of the screen.
We have to use C Language’s gotoxy() function.
Where we need to provide X and Y digits to Perfectly align our Name at the Center of the Screen.

my.c (only works with turbo c++)

#include<stdio.h>  
void main()
{
  gotoxy(40,13);  
  printf("Rosie");  
  getch();
}

my.c (works with codeblock)

#include <stdio.h>
#include <windows.h>
void main()
{
    COORD c;
    c.X = 40;
    c.Y = 16;
 
    SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE), c);
 
    printf("John");
    getch();
}

Write a C Program to Print Names using Array

If you have Multiple Names Stored inside an Array, then you can Loop through it and Print every name One-By-One.

array.c

#include <stdio.h>
void main()
{
    char mynames[3][20] = {"John", "Rick", "Emily"};
    int arrayLength = sizeof(mynames)/sizeof(mynames[0]);
    for(int i=0; i<arrayLength; i++){
        printf("\n%s", mynames[i]);
    }
}

Results

John
Rick
Emily

In the Example above, we are using char mynames[3][20] to create an Array in C.
Where are specifying the Array and Character Length.
Where [3][20] means that our Array will include 3 Values with Max 20 Character Length.

Write a C Program to Display Name and Age

To print Name and Age we have to deal with Characters and Integer values.
Where for storing Character Values we have CHAR data type
and for integers with have INT data type.
And for address/format specifier we have %s for String and %d for Integers.

nameage.c

#include<stdio.h>
int main()
{
    char name[20];
    int age;
    printf("Enter your Name\n");
    scanf("%s",&name);
    printf("\nEnter your Age\n");
    scanf("%d",&age);
    printf("\nYour name is = %s",name);
    printf("\nYou are %d Years OLD",age);
}

Results

Enter your Name
John
Enter your Age
22
Your name is = John
You are 22 Years OLD

But if you don’t want to get and store the Age and Name then you can insert or give the AGE and NAME manually.

nameagr.c

#include <stdio.h>
int main()
{
    char name[20] = "John";
    int age = 25;
    printf("\nYour name is = %s",name);
    printf("\nYou are %d Years OLD",age);
}

Results

Your name is = John
You are 25 Years OLD

Which function is used to print a message on the terminal or command prompt in c?

The Function you are looking for to Print Messages on Terminal or Command prompt in C is printf().
Where you can Print your Message as a String like this Example.

message.c

#include <stdio.h>
int main()
{
    printf("This is my Message");
}

Results

This is my Message
Ad

Need a Personal Tutor?

I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.

Less then 5$/hSee Plans100% Free Trial + Money Back Guaranty

Write a program that asks the user for their name and greets them with their name in c

To greet your users, we need to Collect their Names first.
So we will create one variable and store the user name inside our Variable userName.
After that we will display our Greeting Message with User Name.

greet.c

#include <stdio.h>
void main()
{
    char userName[20];
    printf("Enter Name: ");
    scanf("%s", &userName);
    printf("Hello, %s. Welcome to our App", userName);
}

Results

Enter Name: John
Hello, John. Welcome to our App

Write a C program that reads a person's name in the following format: first name, then middle name, and last name

In that case, we need 3 variables in C to store First Name, Middle Name, and Last Name.
After that we need to ask our users to enter their First Name, Middle Name, and Last Name.
At the end we will display their Full Name.

fullname.c

#include <stdio.h>
void main()
{
    char firstName[20], middleName[20], lastName[20];
    printf("First Name: ");
    scanf("%s", &firstName);
    printf("Middle Name: ");
    scanf("%s", &middleName);
    printf("Last Name: ");
    scanf("%s", &lastName);
    printf("Hello, %s %s %s", firstName, middleName, lastName);
}

Results

First Name: Emily
Middle Name: Rose
Last Name: Smith
Hello, Emily Rose Smith
Ad

Need a Personal Tutor?

I am here to help you with your Programming journey. HTML, CSS, JavaScript, ReactJS, NextJS, C, C++, C#, SQL and more.

Less then 5$/hSee Plans100% Free Trial + Money Back Guaranty
Logo