Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

User Input:

Sure, let's discuss taking user input in C using simple language:

What is User Input?

User input refers to data provided by the user during the execution of a program. It allows users to interact with the program, providing information that can be used by the program to perform certain tasks.

Taking User Input in C:

In C, you can use the scanf() function from the stdio.h library to take input from the user. This function reads input from the standard input stream (usually the keyboard) and stores it in variables.

Example:

Let's create a simple program that asks the user for their name and age, and then prints out a greeting with that information.

#include <stdio.h>

int main() {
char name[20]; // To store the user's name
int age; // To store the user's age

// Ask the user for their name
printf("What's your name? ");
scanf("%s", name); // Read the user's input and store it in the 'name' variable

// Ask the user for their age
printf("How old are you? ");
scanf("%d", &age); // Read the user's input and store it in the 'age' variable

// Greet the user
printf("Hello, %s! You are %d years old.\n", name, age);

return 0;
}

In this example

When you run this program, it will prompt you to enter your name and age, and then it will greet you with the entered information.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java