Hello today I will show you how to write a C program that prints your name, date of birth and mobile number. This is a simple program that demonstrates how to use variables, input and output in C.
First, let's look at the code of the program. We start by including the stdio.h header file, which provides us with the functions to read and write data. Then we define the main function, which is the entry point of our program. Inside the main function, we declare three variables to store the user input: name, dob and mobile. We use the char data type to store strings of characters, and we specify the size of each variable using square brackets. For example, name[50] means that name can store up to 50 characters.
Next, we use the printf function to print some messages on the screen and prompt the user to enter their name, date of birth and mobile number. The printf function takes a string as an argument and displays it on the screen. We use the \n character to create a new line.
After each message, we use the scanf function to read the user input and store it in the corresponding variable. The scanf function takes two arguments: a format specifier and a pointer to a variable. The format specifier tells the function what type of data to expect from the user input. For example, %s means a string of characters. The pointer to a variable is denoted by an ampersand (&) followed by the name of the variable. For example, &name means the address of the name variable in memory.
Once we have read all the user input, we print a blank line using printf("\n"). Then we print another message saying "Your details are:" followed by another blank line. Finally, we print the values of the variables using printf and concatenating them with strings using the plus (+) operator. For example, printf("Name: " + name + "\n") means print "Name: " followed by the value of name followed by a new line.
That's all for the code of the program. Now let's see how it works when we run it. To run the program, we need to compile it first using a C compiler. I have saved the program in a file called user_details.c, so I will use this command on a terminal:
gcc user_details.c -o user_details
This will compile the program and create an executable file called user_details. To run this file, I will use this command.
./user_details
This will execute the program and prompt me for input. I will enter some sample input like this:
Enter your name: Thor
Enter your date of birth (dd/mm/yyyy): 01/01/2009
Enter your mobile number (10 digits): 1234567890
After entering each input, I will press enter to confirm it. Then the program will print my details on the screen like this:
Your details are:
Name: Thor
Date of birth: 01/01/2009
Mobile number: 1234567890
C Program
#include <stdio.h>
int main()
{
// Declare variables to store user input
char name[50];
char dob[11];
char mobile[11];
// Prompt the user to enter their name, date of birth and mobile number
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your date of birth (dd/mm/yyyy): ");
scanf("%s", dob);
printf("Enter your mobile number (10 digits): ");
scanf("%s", mobile);
// Print the user input in a formatted way
printf("\nYour details are:\n");
printf("Name: %s\n", name);
printf("Date of birth: %s\n", dob);
printf("Mobile number: %s\n", mobile);
return 0;
}
Output
Enter your name: Thor
Enter your date of birth (dd/mm/yyyy): 01/01/2009
Enter your mobile number (10 digits): 1234567890
Your details are:
Name: Thor
Date of birth: 01/01/2009
Mobile number: 1234567890
Flowchart
- Start
- Declare variables to store user input: name, dob, mobile.
- Print "Enter your name: "
- Read name from user input
- Print "Enter your date of birth (dd/mm/yyyy): "
- Read dob from user input
- Print "Enter your mobile number (10 digits): "
- Read mobile from user input
- Print "\nYour details are:\n"
- Print "Name: " + name + "\n"
- Print "Date of birth: " + dob + "\n"
- Print "Mobile number: " + mobile + "\n"
- End
And that's it! We have successfully written and run a C program that prints our name, date of birth and mobile number. I hope you enjoyed it and learned something new.
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!