C program to input user name and age and display the output

How It Works:

1. Include the stdio.h header file
The stdio.h header file provides functions such as printf() and scanf(), which are used for displaying output and accepting input.

2. Create variables
The program creates a character array to store the user's name and an integer variable to store the age.

3. Accept the user's name
The statement scanf("%49s", name) reads the user's name. The %49s format limits the number of characters read to help prevent writing beyond the size of the character array.

4. Accept the user's age
The statement scanf("%d", &age) reads an integer value and stores it in the age variable.

5. Display the name and age
The printf() function displays the values stored in the name and age variables.


What Will You See When You Run It?

When you run the program, it asks the user to enter a name and age.

Example:

Enter your name: Sankalan
Enter your age: 30
Your name is: Sankalan
Your age is: 30


How to Input a Full Name in C?

The %s format specifier used with scanf() reads input only until the first whitespace character. For example, if the user enters:

Kailas Warade

scanf("%49s", name) reads only Kailas.

To read a full name containing spaces, you can use fgets().

Example:

#include <stdio.h>
int main()
{
  char name[50];

  printf("Enter your full name: ");
  fgets(name, sizeof(name), stdin);

  printf("Your name is: %s", name);

  return 0;
}

Output:
 Enter your full name: Kailas Warade
 Your name is: Kailas Warade


Frequently Asked Questions

1. How do you input a name in C?
You can use a character array and scanf() to accept a single-word name.

2. How do you input age in C?
Use an integer variable and the %d format specifier with scanf().

3. How do you print your name in C?
You can use the printf() function with the %s format specifier.

4. How do you input a full name with spaces in C?
You can use fgets() to read a full name containing spaces.


Conclusion:

This simple C program demonstrates how to accept a user's name and age and display them using scanf() and printf(). It is a useful beginner example for understanding variables, character arrays, user input and output in C.

If you need to read a name containing spaces, fgets() is a better choice than using %s with scanf().


Java Topics wise FAQ    SQL Topics wise FAQ


Other C Programs
1.Write a program in C to find area of circle using single-line comments.
2.Write a C program to find addition of two numbers with multi-line comments to write down the purpose of every steps.
3.Write a program in C that describe a complex mathematical formula with nested comments.
4.Write a C program to demonstrate the declaration and initialization of variables of different data types.
5.write a program code in c to swap a values of the two variable without using a third variable.
6.Write a program in c to find area of rectangle by accepting variable value length and width.
7.Write a program that accepts user input for employee name and salary, and then display them using those variables.
8.Write a program in C to demonstrate and decide the size of int, float, char, and double data types.
9.Write a program in C to convert temperature from Celsius to Fahrenheit using proper data types.
11.Write a program in C that accepts any two numbers from user and calculate addition, multiplication, subtraction and division (+, *,-, /) and show the result.
13.Write a program in C that asks the user to enter the marks obtained in three subjects. Read the marks using scan. Calculate the average marks and display the student details.

Other Topic:-->> Dynamic memory FAQ.  ||  Operators Assignments in C.