Home >Backend Development >C++ >How to use scanf and printf in c++

How to use scanf and printf in c++

下次还敢
下次还敢Original
2024-05-01 14:03:15706browse

The scanf and printf functions in C are used to read data from standard input and write data to standard output. scanf usage: int scanf(format, ...), where format is the data format to be read, ... is the variable address to be read; printf usage: int printf(format, ...), where format is The data format to be written,... is the variable to be written.

How to use scanf and printf in c++

Usage of scanf and printf in C

scanf and printf are two commonly used standard input/output functions in C. They are used to read data from standard input and write data to standard output respectively.

scanf

Usage:

<code class="cpp">int scanf(const char *format, ...);</code>

Among them:

  • format: Specifies the format string of the data to be read.
  • ...: The address of the variable to be read.

Format specifier:

  • ##%d: Integer
  • %f : Floating point
  • %c: Character
  • %s: String

Example:

<code class="cpp">int age;
char name[50];
scanf("%d %s", &age, name);</code>

printf

Usage:

<code class="cpp">int printf(const char *format, ...);</code>
Where:

  • format: Specifies the format string of the data to be written.
  • ...: Variable to be written.

Format specifier:

    ##%d
  • : Integer
  • %f
  • : Floating point
  • %c
  • : Character
  • %s
  • : String
Example:

<code class="cpp">int age = 25;
printf("姓名:%s,年龄:%d\n", name, age);</code>

The above is the detailed content of How to use scanf and printf in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn