USER INPUT IN C

Maha

 

User Input In C

  • User input refers to the data that a user provides to a program during runtime. 
  • In C programming , This data is typically entered via the keyboard while the program is running.

For example, a program that calculates the area of a rectangle might prompt the user to enter the length and width of the rectangle of input. The program would then use these values to calculate the area and display on the result.

  • To receive user input in C, you can use the standard input/output library stdio.h, which provides functions such as scanf() and gets(). 
  • These functions allow you to read user input from the console and store it in variables or arrays for further processing.
  • User input is an important aspect of programming, as it allows programs to interact with users and respond to their input in a meaningful way. 
  • Hence, it is important to validate and sanitize user input to prevent security such as buffer overflow attacks or injection attacks.

Syntax:

#include <stdio.h>
int main() 
{
int a, b, sum;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}

Program:

#include <stdio.h>
int main()
{
 char name[50];
 int age;
 printf("Enter your name: ");
 scanf("%s", name);
printf("Enter your age: ");
 scanf("%d", &age);
printf("Hello, %s! You are %d years old.\n", name, age);
return 0;
}

Output:

Enter your name: jeeva
Enter your age: 17
Hello, jeeva! You are 17 years old.


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send