How to display information and read user input in C programming language | applied electronics engineering

Latest

How to display information and read user input in C programming language

By Applied Electronics - Friday, March 10, 2017 No Comments
Without interaction with users, program and programming is useless. In C programming language there are many ways to read user input and in this tutorial we will explain how to display information and read user input in C programming language. Using C users are able to interact with program due to I/O library that the C compiler provides. These libraries contains functions that allows user to interact with the Operating System that the user is using.

In general, in C programming language we can classify two types of I/O functions which are 1) Formatted I/O and Unformatted I/O.


The difference between formatted and unformatted I/O is that when using the formatted functions the user is able to specify the way the data should be collected from the user and the way these should be displayed on the screen.

Most commonly C users use the printf( ) and scanf( ) formatted functions. The following is an example of printf( ) usage in a C program.


The output of this program is,


The printf( ) function has the following syntax,


The format string in the example above is all within the double quotes, here, Average = %d\nPercentage = %f

The %d, \n and %f are called format specifier. They are used to print out the value stored in the variables specified after the format string in the list of variables(seperated by commas).

%d means integers, %f means float and \n means new line. See the list of format specifiers below and their meaning.


Optimally we can specify the number of decimal values called field width specifiers using the following format specifier rule.


As mentioned above, the printf( ) is a formatted I/O function. So it allows users to specify how the output should be printed out on the display. Other format specifiers are the escape sequences. In the above example \n is an escape sequence. There are other escape sequence like the tab escape sequence, also called tab character, which when used creates a tab spacing at the place where it is used.


Now we will show how to read user input in C programming language. The mostly used I/O function that user use to read inputs from users in C programming language is the scanf( ) function, which is a formatted I/O function.

The syntax of scanf( ) is as follows.



Notice that the same format string is used as in case of printf() function. But now instead of variable names in printf( ), the address of the variable is used. In C programming language the address is specified using the & symbol. So for example if you want to read some user data then you would like something like this,


All format specifier used in printf( ) are valid for scanf( ).

An example code of scanf is the following,


So in this tutorial you learned the basic of how to display information and read user input in C programming language. In the next tutorial we give some example of unformatted I/O functions.

Tags:

No Comment to " How to display information and read user input in C programming language "