【C++】Enhancement of C++ language to C language③ (Type checking enhancement - all functions and variables must have a type | New bool type - introduction to bool type)





1. Enhanced type checking - all functions and variables must have a type




1. C language function type - function parameters and return value types can be determined


In the C language, the return value type of the function can not be specified when it is defined. If the function parameter is empty, you can pass in any parameter without error;

In the following code, the function f has no return value type, nor does it indicate the parameter type. After receiving the parameters, the parameters can still be printed normally;

The function num does not indicate the type of return value, but it can return an integer value of 1, and the function parameter is empty, but it will not report an error if several parameters are passed into the function;


Code example:

#include <stdio.h>

// 函数中参数没有写明类型
// 也没有写返回值
f(i)
{
    
    
	printf("i = %d \n", i);
}

// 函数没有参数, 可以传入任意参数
// 函数没有返回值类型, 但是可以返回返回值
num()
{
    
    
	return 1;
}

int main()
{
    
    
	f(10);
	printf("num = %d \n", num(1, 2, 3));
	return 0;
}

Execution result: use the gcc compiler to compile the above C language source code, then execute the compilation result a.exe, and find that it can be compiled and executed successfully;

C:\Users\octop\Desktop>gcc hello.c

C:\Users\octop\Desktop>a.exe
i = 10
num = 1

insert image description here


2. C++ language function type - function parameters and return value types must be specified


In the C++ language, all variables and functions must have types;

Copy the code in the above C language to the C++ environment, and then compile it again, and found a bunch of errors;

1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>Hello.cpp
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(5,3): error C2065: “i”: 未声明的标识符
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(5,4): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(6,1): error C2448: “f”: 函数样式初始值设定项类似函数定义
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(13,1): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(19,2): error C2064: 项不会计算为接受 1 个参数的函数
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(20,35): error C2660: “num”: 函数不接受 3 个参数
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(12,1): message : 参见“num”的声明
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(20,9): warning C4473: “printf”: 没有为格式字符串传递足够的参数
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(20,9): message : 占位符和其参数预计 1 可变参数,但提供的却是 0 参数
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(20,9): message : 缺失的可变参数 1 为格式字符串“%d”所需
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

insert image description here





2. New bool type - Introduction to bool type



There is no concept of boolean value in C language, only the concept of 0 and 1, use int type + macro definition to represent boolean value, as shown in the following code;

// 使用 宏定义 定义布尔类型
#define BOOL int
#define TRUE 1
#define FALSE 0

// 使用 宏定义 定义一个布尔变量
BOOL flag = FALSE;

On the basis of the C language, the C++ language adds a Boolean type, and the type name is bool;


bool Boolean type value: the value has two true and false;

  • true means true, which is represented by 1 inside the compiler; if a non-zero value (including negative numbers) is assigned to a bool variable, it will turn true;
  • False means false, which is represented by 0 inside the compiler; if you assign a value of 0 to a bool variable, it will turn to false;

The bool layout type occupies memory space:

  • In general, bool type variables occupy 1 byte of memory space;
  • If multiple bool type variables are defined together, the compiler will optimize when compiling, and each bool variable occupies 1 bit, that is, 1 bit, and 8 bits are 1 byte Byte;

In the following code, the size of the bool type variable is printed as 1 byte;

  • Assigning a non-zero value to a bool type variable will be converted to true;

  • Assign 0 to the bool type variable, it will be converted to false;

Code example:

// 导入标准 io 流头文件
// 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;

void main()
{
    
    
	// 声明 bool 变量
	bool b = true;
	// 打印 bool 变量的大小
	cout << "sizeof(b) : " << sizeof(b) << endl;
	// 打印 bool 变量的值
	cout << "b : " << b << endl;

	// 1. 为其赋值 10 会被当做 1 值为 true
	b = 10;
	// 打印 bool 变量的值
	cout << "b : " << b << endl;

	// 2, 为其赋值 -1 会被当做 1 值为 true
	// 负数也会被当做 1 , 转为 true
	b = -1;
	// 打印 bool 变量的值
	cout << "b : " << b << endl;

	// 3. 为其赋值 0 会被当做 0 值为 false
	b = 0;
	// 打印 bool 变量的值
	cout << "b : " << b << endl;

	system("pause");
}

Results of the :

sizeof(b) : 1
b : 1
b : 1
b : 1
b : 0
请按任意键继续. . .

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132372288