Header Ads

Input &Output:scanf & printf:C Programming Language - Part 3.2

scanf & printf



We have already seen how we can get input using the help of getchar on a single character computer. Now we will see how many other data, such as strings, numbers, decimal numbers, and other data on computers in single character, take input on the computer. The "scanf" function is used to take any value on the computer, number, string, and so on. Again the "printf" function can be used to show a value like putchar. Output could be shown on a single character computer with putchar, but with the "printf" function, multiple data can be shown concurrently on the console.

"Scanf" Function Usage Rules

1
Scanf (control string, argument1, argument2, ...... ..argumentn);
Here's the format of the control string that will take any kind of data input. Also called placeholder. And after the input by the argument, the data will be stored on the computer (where no address of memory address is stored).

In such a program, Scanf is used as follows

1
2
Char name;
Scanf ("% c", & amp; amp; amp; name);
We have a variable called name, we have. From the input device, the value of this variable is on the computer. And scanf function has been used for it.

Here control string is being c. Each control string has to start with a% sign. So the control string here is written by% c. Here c means that the data item is a single character. There are many more such control strings. A little later we will know about them.

A complete program with scanf

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main (void)
{
    Float gpa;
    Printf ("Enter your SSC GPA:");
    Scanf ("% f", & gpa);
    Printf ("You SSC GPA:% f \ n", gpa);
    Return 0;
}
Rules of using "printf" function
1
Printf (control string, argument1, argument2, ...... ..argumentn);
Here is the format which will give data output to control string. And by the argument, each output data is released.

In such a program, "printf" is used as follows:

1
2
Char name;
Printf ("% c", name);
Here a name is named Character variable. Now think that the value of the name is on the computer, we will find out its output. So printf ("% c", name); By expressing it on the Output device. Here and control string being c. Scanf's die printf and every control string has to start with a% sign. So the control string here is written by% c. Here c means that the data item is a single character. Scanf and printf's control string are the same. The control string or placeholder of scanf implies what kind of input it will take, and what the output string will mean by the control string of printf.

We used to write many programs using printf. Write another

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main (void)
{
    Int roll;
    Printf ("Can you remember your roll no in class one? \ NIf yes, please enter:");
    Scanf ("% d", & roll);
    Printf ("Your roll was:% d \ n", roll);
    Return 0;
}
During data input output, we write a character after the perspective sign, as a variable placeholder. These are called Conversion Characters. Below is a list of the conversion characters used in C programming

Meaning of Control String
% C This is used for single character input or output.
% D is used for signed decimal integers.
% E e is used for signage floating point in notation.
% F is used for signed floating point.
% G is used for signed point floating point in scientific notation.
% I Signed is used for decimal integers.
% O Unsigned is ablated for octal integration data
If you use this, the String Data item will output.
% U is used for unsigned decimal integers.
% X Unsigned is used for hexadecimal data.
A value can be positive, can be negative. If we are negative, then we should ideally put minus sign before. A number is positive or negative, signifying it is signed value. Unsigned value, if not mentioned, is called. If there is no sign before the number, we do not suppose that positive number. In addition to the above conversion characters there are many more conversion charts. These can be used in one way or another. For example, in small hands, it can be written in big hands. In the small hand you will print the lowercase e, the upper case E will print the upper e There are also lf, .5f, or such. .5f means printing up to 5 cells after decimal. We'll learn these very slowly.


Now we see a small program on using scanf and printf

1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main (void)
{
    Char name [80];
    Scanf ("% s", & name);
    Printf ("You enter:% s", name);
    Return 0;
}
Here is a character array named after name (array will be discussed later), which can hold up to 80 characters (actually 79 other Null characters, which will be discussed later). After that its value will be taken from the input device by scanf function. Under scanf,% s implies that it will take a String Input. Then printf ("% s", name); By the output shown on the screen.


Now we will know the details of using scanf and printf. I will try both together To learn Because the use of two is almost the same. Data input is taken by one, data output is provided with another.


Earlier we gave a single value input and output. We can do two values ​​simultaneously with input or output. Input will be written as follows:

1
Scanf ("% d% d", & amp; amp; num1, & amp; amp; amp; amp; num2);
Where we can input the two integer values. Write for output as follows:

1
Printf ("% d% d", num1, num2);
Here we print the value of variables named num1 and num2. When we get the output, we get an integer. A complete program

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
Int main (void)
{
     
    Int num1, num2;
     
    Scanf ("% d% d", & num1, & num2);
    Printf ("% d% d / n", num1, num2);
     
    Return 0;
}
If we want, we can have an integer, character, float value input and print as an output. See the following program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
Int main (void)
{
    Char name [50];
    Int id;
    Float gpa;
     
    Printf ("Enter your name, id and gpa: \ n");
     
    Scanf ("% s% d% f", & name, & id, & gpa);
    Printf ("Name:% s ID:% d GPA:% f \ n", name, id, gpa);
     
    Return 0;
}


No comments

Theme images by sebastian-julian. Powered by Blogger.