Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language to input and output printf and scanf functions

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use C language to input and output printf and scanf functions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use C language to input and output printf and scanf functions.

Basic input and output

If you need to use input and output functions, you need to include a header file.

Output printf

Printf is the most commonly used output function, which can format variables of any basic data type.

Int printf (const char*format,...)

Format: "format control character" is a string enclosed in double quotation marks (format control character, also known as placeholder)

The data in the output list can be legal constants, variables, and expressions that correspond to the format in the format Control string.

Format controller table:

Format controller optional format:% [-] [0] [m] [l] *% d outputs% [0] md according to the actual length of integer data with the field width specified by m, if the number of digits of the data is less than m, the left end fills in the space; if m is preceded by "0", the left end is supplemented with 0. %-md output at the field width specified by m, left-aligned% ld output long int

Example:

/ / General printf ("hello world"); printf ("% lf", 3.1415); printf ("% d days in a week", 7); / / use optional format printf ("% d\ n", 52); printf ("% 3D\ n", 52); / / output is 3 characters wide, right-aligned by default, filling printf with spaces ("d\ n", 52) / / same as above, except that the padding character is changed from space to 0 /. You only need to add a minus sign after% to indicate left-aligned printf ("%-3D\ n", 52). Note:

When many students output, they will encounter a lot of problems, probably the following points:

Format characters, except X, E, G, all use lowercase letters, such as "% d" cannot be written as "% D".

Different types of data should use the corresponding type of format characters to describe their output form.

If you want to output double quotation marks, you should express them as escaped characters in format Control and enclose them in single quotation marks, namely "\".

To output the character "%", use two consecutive "%" in format Control. For example: printf ("% d%%", s)

When the number of format characters in format Control is less than the output items in the output table, the extra output items are not output.

When there are more formatters than output items, the result is an indefinite value.

Putchar

Sometimes, just to print a character on the monitor screen, using the powerful printf function, it is quite suspected of anti-aircraft gun shooting mosquitoes, C provides a library function putchar to complete this simple task.

Int putchar (int ch); / / use putchar ('A'); putchar (65); puts

And sometimes just to output a string, using the printf function is also a bit troublesome, you can use a library function provided by C puts, and the puts function will automatically wrap after the output string.

Int puts (const char*str); / / use puts ("hello world"); enter scanf

Scanf is the most commonly used input function, which can format variables of any basic data type.

Int scanf (const char* format,...)

Format: "format control character" is a string enclosed in double quotation marks

The data in the input list must be the address table of the legal variable (that is, the variable is preceded by a &) and correspond to the format in the format Control string.

If you enter more than one input, you must be separated by spaces in the format format control string.

Example: const double PI = 3.1415; int radius = 0; printf ("Please enter radius:\ n"); scanf ("% d", & radius); printf ("area of circle is:% lf", PI * radius * radius); notes for scanf function

The format string of the scanf function consists of the following three types of characters:

The 1 > format character (also known as a placeholder) is similar to the printf function, where the format string of the scanf function begins with% and ends with a format character, with additional characters inserted in the middle. The formatter tells the scanf function what data to read.

2 > White space characters can be spaces (entered with the space bar), tabs (entered with the Tab key), and new line characters (entered with the enter key).

3 > A non-blank character is a non-blank character except that the format description conforms to the blank character. Non-blank characters must also be entered when the user enters them.

Getchar

It is very convenient to enter a character.

Int getchar (); / / use int ch = getchar (); putchar (ch); gets_s

It is very convenient to enter a string.

Char* gets_s (char* _ Buffer,size_t _ Size); / / use char name [50] = "; gets_s (name,50); puts (name)

All right, the basic input and output of C language is introduced here, you can practice first, and then introduce complex examples.

At this point, I believe you "how to use C language input and output printf, scanf functions" have a deeper understanding, might as well to practical operation it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report