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

How to use scanf and printf in c++

下次还敢
Release: 2024-05-01 14:03:15
Original
686 people have browsed it

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>
Copy after login

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>
Copy after login

printf

Usage:

<code class="cpp">int printf(const char *format, ...);</code>
Copy after login
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>
Copy after login

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!

Related labels:
c++
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template