To write a Program to Print your Name in C, we have the printf() function in C Language.
You just have to write your Name inside the printf() function and you will be able to Print or Display your Name.
myname.c
#include<stdio.h>
void main(){
printf("Name: John");
}
Results
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.
By using #include
So #include going to include
std standard Input Output
.h Header file.
Here main() is our Main function, 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.
By using printf(), we are Printing our Name or a String.
Where printf() function will take the value as an Input and Display it as an Output.
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 entered name inside a C Variable myname.
By using scanf("%s", &myname), we are telling our compiler that we are expecting a 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 the printf() funtion.
Here we are using "%s" as our String Placeholder and printf() function will print our myname variable's value there.
name.c
#include<stdio.h>
void main()
{
char myname[20];
printf("Enter your Name: ");
scanf("%s", &myname);
printf("Your name is: %s", myname);
}
Results
To print your Name and Address we can use printf() Function.
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
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.
center.c (Only works with TurboC++)
#include<stdio.h>
void main()
{
gotoxy(40,13);
printf("Rosie");
getch();
}
center.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();
}
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("
%s", mynames[i]);
}
}
Results
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 a Max 20 Character Length.
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.
nameandage.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
nameandage.c [Get User Inputs]
#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