C serial 21 printf with return value, print long string and scanf


1、 Printf() return value

  • Printf() function has a return value, which usually returns the number of printed characters. If there is an output error, printf() returns a negative value (different versions will have different values)
int D21_1_prntval(void) {
	int bph2o = 212;
	int rv;
	rv = printf("%d F is water's boiling point.\n", bph2o);
	printf("The printf() function printed %d characters.\n", rv);

	return 0;


2、 Print long string

  • If the statement is too long, you can use (space, tab, newline) to isolate it, and the compiler will ignore them,But you can’t use this method inside double quotes
  • Please use \ n for line breaking. You can’t press enter directly.
  • There are three ways to break a string:
int D21_2_longstrg(void) {
	printf("Here's one way to print a ");
	printf("long string.\n");
	printf("Here's another way to print a \
long string.\n");
	printf("Here's the newest way to print a "
		"long string.\n");

	return 0;


  • Note: in method 2, you can’t indent casually, because once indented, it will be included in the string; In method 3, as long as white space (line feed, space, indentation) is used between two double quotes, the compiler will consider the first and second strings to be one.

3、 Using scanf ()

  • There are many input functions in C language, but scanf () function is the most common. Contrary to printf (), it will read various types of data and convert them into corresponding types. Scanf () function uses pointers to variables. Pointers are not introduced at present. Let’s remember two things first:
  • Read the value of the basic variable and add &; before the variable name; If you read a string into a character array, you don’t need to add &
  • Let’s start with a simple demonstration
#pragma warning(disable:4996)
int D21_3_input(void) {
	int age;    // variable
	float assets; // variable
	char pet[30];  // Character array for storing strings

	printf("Enter your age,assets, and favourite pet.\n");
	scanf("%d %f", &age, &assets);// Here to use&
	scanf("%s", pet);// Character arrays are not used&
	printf("%d $%.2f %s\n", age, assets, pet);

	return 0;


  • Note: the scanf() function uses blank space to divide the input into multiple segments, and then skip the blank space when matching the conversion description with the field in turn. As long as at least one newline character, space or tab character is entered between each input item, it can be entered in one or more lines, for example:
  • The only exception is the% C conversion specification, where scanf() reads every character, including whitespace
  • Common conversion instructions
Conversion description meaning
%c character
%d Signed decimal integer
%e,%f,%g,%a Floating point number (C99 added% a)
%E,%F,%G,%A Floating point number (C99 added% a)
%i Signed decimal integer
%o Signed octal integer
%p Pointer (address)
%s String. All characters from the first non blank character to the next blank character are input
%u Unsigned decimal integer
%u,%X Signed hexadecimal integer

4、 Source code:

Recommended Today

Summary of import and export usage in JavaScript

import import 和 require 的区别 import 和js的发展历史息息相关,历史上 js没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来。这对开发大型工程非常不方便。在 ES6 之前,社区制定了一些模块加载方案,最主要的有 CommonJS 和 AMD 两种。前者用于服务器,后者用于浏览器。ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,完全可以取代 CommonJS 和 AMD 规范,成为浏览器和服务器通用的模块解决方案。也就是我们常见的 require 方法。 比如 `let { stat, exists, readFile } = require(‘fs’);` 。ES6 在语言标准的层面上,实现了模块功能。ES6 模块不是对象,而是通过export命令显式指定输出的代码,再通过import命令输入。 import 的几种用法: 1. import defaultName from ‘modules.js’; 2. import { export } from ‘modules’; 3. import { export as ex1 } from […]