Printf and Scanf Functions in C

In this tutorial we will learn about printf and scanf functions in C Programming. The C Programming supports several built-in functions using C Standard Library. The two special functions we’re interested in this post are printf and scanf. These two functions are declared into “stdio.h” header file, which we will include as first line of every C Program. The stdio.h header defines variable types, several macros, and various functions for performing input and output. Instead of putting more text, I rather prefer to jump onto example code:

Printf: Display Message on Screen

Here in this example we’re going to output text to print message on screen. We have used inbuilt standard library function called printf.

Scanf: Getting User Input

In this program the statement scanf(“%d”, &number); gets user input. This input is any integer value. %d is a format specifier to display the value of type integer. & is symbol called ampersand or address operator. This means &number used to hold the address of variable number. We’ll get into “&” symbol in details in our pointer lesson. Just use this in your programs and will explain it in future.
The statement printf(“You Entered: %d”, number); is simply uses printf function to display the value or number that user have entered. Now here is an output from the program:

output of scanf in c programming
Output of Scanf in C Programming

Comments in C

Almost all C Programs made up of hundreds of lines of code. There are several situations where we need certain part of code not to be executed at run-time so we comment them out. This means that code no longer execute as part of C Program. There are two ways to comment code in C Language; one is Single Line Comment and Multi Line Comment.

Example: Single Line Comment
Example: Multi Line Comment

The compiler will assume that everything after the /* symbol is a comment until it reaches the */ symbol, even if it spans multiple lines within the C program.

A good programmer always recognized by how one can document code so that programs become more readable and structured. The use of good commenting style help to save lot of time in long run. Especially for others who would want to review your code and contribute to your work. Here is example code:

Now we know how input and output works. The printf and scanf functions in C Programming will be used very frequently in future lessons. Since printing text on screen and getting user input seems to be very simple task. But as we a move along in series we will write cool programs which will make more sense. In next lesson we will learn about Variable and Data Types in C Programming.

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Leave a Reply

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


Register | Lost your password?