Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
Reading input in c programming

Reading Input in a C program

Posted on 29 December 202220 May 2023 By YASH PAL No Comments on Reading Input in a C program

To learn about the reading or receiving input from users in the c program we will take an example program and then understand it.

Let’s look at the below-given program. if we assumed the values of p, n, and r to be 1000, 3, and 8.5. then every time we run the program we would get the same value for simple interest. if we want to calculate simple interest for some other set of values we are required to make the relevant change in the program, and again compile and execute it.

Thus the program needs to be more general to calculate simple interest for any set of values without being required to make a change in the program. Moreover, if you distribute the EXE file of this program to somebody he would not even be able to make changes in the program Hence it is a good practice to create a program that is general enough to work for any set of values.

To make the program general the program itself should ask the user to supply the values of p, n, and r through the keyboard during execution. this can be achieved using a function called scanf(). this function is a counterpart of the printf() function. printf() outputs the values to the screen whereas scanf() receives them from the keyboard. this is illustrated in the program shown below.

/*Calculate the simple interest */
main()
{
	int p, n;
	float r,si;
	printf("Enter values of p, n, r");
	scanf("%d %d %f", &p,&n,&r);
	si = p * n * r / 100;
	printf("%f",si);
}

The first printf() outputs the message ‘ Enter values of p, n, r ‘ on the screen. here we have not used any expression in printf() which means that using the expression in printf() is optional.

Note that the ampersand (&) before the variables in the scanf() function is a must. & is an ‘Address of’ operator. it gives the location number used by the variable in memory. when we say &a, we are telling scanf() at which memory location should it store the value supplied by the user from the keyboard.

Note that a blank, a tab, or a new line must separate the values supplied to scanf(). note that a blank is created using a spacebar, a tab using the Tab key, and a new line using the Enter key. this is shown below.

Example – The three values are separated by blank

1000 5 15.5

Example – The three values are separated by a tab.

1000 5  15.5

Example – The three values are separated by newline

1000

5

15.5

Let’s take another example that receives an integer input from the user.

main()
{
	int num;
	printf("Enter a number");
	scanf("%d",&num);
	printf("Now I am letting you on a secret...");
	printf("You have just entered the number %d", num);
}

Read other tutorials

  • C programming interview questions/answers
  • C programming tutorials
C Programming Tutorials, Computer Science Tutorials Tags:c, computer science

Post navigation

Previous Post: Compiling C Programs
Next Post: DevOps for Java Applications in the Cloud: Tools and Techniques

Related Tutorials

programming languages for machine learning and data science Top Programming Languages For Machine Learning Computer Science Tutorials
Introduction to Statistics for Data Science: Building a Solid Foundation Computer Science Tutorials
How to Become a Successful Data Engineer in the Data Science Field – Complete Guide Computer Science Tutorials
Is Python a good language for Machine Learning/AI? Computer Science Tutorials
basics of boolean algebra Its Operators, Laws, and Examples Basics of Boolean Algebra: Its Operators, Laws, and Examples Boolean Algebra
x winodws in linux X Windows system in Linux Computer Science Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the … Read more
  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet, … Read more
  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis … Read more
  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges … Read more
  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle … Read more
  • Algorithms Tutorials
  • Basic Programming
  • Boolean Algebra
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the … Read more
  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet, … Read more
  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis … Read more
  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges … Read more
  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle … Read more

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme