How to perform input/output using printf and scanf in C
C is a small language, yesCDoes not contain any input/output (I/O) functions.
Of course, this is not unique to C. The core of the language usually has nothing to do with I/O.
For the C language, the C standard library provides us with input/output through a set of functions defined in the C language.stdio.h
head File.
You can import the library with the following command:
#include <stdio.h>
At the top of your C file.
This library provides us with many other functions:
printf()
scanf()
sscanf()
fgets()
fprintf()
Before describing these functions, I want to take a moment to discussI/O stream.
We have 3 types of I/O streams in C:
stdin
(Standard input)stdout
(Standard output)stderr
(Standard error)
Using the I/O function, we can always handle the stream. Streams are high-level interfaces that can represent devices or files. From a C point of view, there is no difference between reading from a file or reading from the command line: it is an I/O stream anyway.
This is one thing to remember.
Some functions are designed to be used with specific streams, such asprintf()
, We use to print characters tostdout
. Use its more general counterpartfprintf()
, We can specify the stream to be written.
Since i started talkingprintf()
, Let us introduce it now.
printf()
printf()
It is one of the first functions to be used when learning C programming.
In its simplest form of usage, you pass it a string literal:
printf("hey!");
The program will print the contents of the string to the screen.
You can print the value of the variable, which is a bit tricky because you need to add a special character (placeholder), which changes according to the type of the variable. For example, we use%d
For signed decimal integers:
int age = 37;
printf(“My age is %d”, age);
We can use commas to print multiple variables:
int age_yesterday = 36;
int age_today = 37;
printf(“Yesterday my age was %d and today is %d”, age_yesterday, age_today);
There are other format specifiers, such as%d
:
%c
For a character%s
For a string%f
For floating point numbers%p
For pointer
There are a lot more.
We canprintf()
, Like\n
We can use it to make the output create new lines.
scanf()
printf()
Used as an output function. I now want to introduce an input function, so we can say that we can do all I/O things:scanf()
.
This function is used to get the value from the user running the program from the command line.
We must first define a variable that will hold the value obtained from the input:
int age;
Then we callscanf()
It has 2 parameters: the format (type) of the variable and the address of the variable:
scanf("%d", &age);
If you want to get a string as input, remember that the string name is a pointer to the first character, so there is no need&
The preceding characters:
char name[20];
scanf("%s", name);
This is a small program that uses bothprintf()
withscanf()
:
#include <stdio.h>
int main(void) {
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("you entered %s", name);
}
Download mine for freeC manual
More clang tutorials:
- Introduction to C programming language
- C variables and types
- C constant
- C operator
- C conditional
- How to use loops in C
- Introduction to C arrays
- How to determine the length of an array in C
- Introduction to C strings
- How to find the length of a string in C
- C pointer introduction
- Iterate over the array in C
- Boolean in C
- Introduction to C functions
- How to use NULL in C
- Basic I/O concepts in C language
- Double quotes and single quotes in C
- How to return a string from a C function
- How to solve the implicit declaration library function warning in C language
- How to check character value in C
- How to use `printf()` to print percent characters in C language
- C conversion specifiers and modifiers
- How to access command line parameters in C
- Range of variables in C
- Can functions be nested in C?
- Static variables in C
- C global variables
- Typedef keyword in C language
- C enumeration type
- C structure
- C header file
- C preprocessor