Basic I/O concepts in C language

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.hhead 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%dFor 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:

  • %cFor a character
  • %sFor a string
  • %fFor floating point numbers
  • %pFor pointer

There are a lot more.

We canprintf(), Like\nWe 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: