《Beginning C++17》-学习笔记-Chapter 02-Introducing Fundamental Types of Data

本文介绍了C++17中关于基本数据类型的细节,包括除法和模运算的定义,使用using声明和指令简化命名空间引用的方法,以及不同类型混合运算时的类型转换规则。此外,还提及了浮点数的签名性质以及宽字符类型 wchar_t,char16_t 和 char32_t 在处理Unicode字符中的应用。
摘要由CSDN通过智能技术生成

#include <iostream>

int main()
{
	//int apple_count{ 15.5 }; /*此行会导致编译错误。错误类型是narrowing conversion。*/
	int apple_count{ 15 }; /*第一种赋值形式;The braces enclosing the initial value are called a braced initializer.这种赋值形式自C++11开始引入。commonly referred to as uniform initialization. */
	int orange_count( 5 ); /*第二种赋值形式;Functional notation*/
	int total_fruit = apple_count + orange_count; //第三种赋值形式; assignment notation;initial value for a variable can be an expression*/
	std::cout << total_fruit <<std::endl; /*输出结果为20*/
}
#include <iostream>

int main()
{
	int counter_01 { 0 }; // counter_01 initiated to 0
	std::cout << counter_01 << std::endl;

	int counter_02{}; /*counter_02 initiated to 0 as well;Zero initialization works for any fundamental type.For all fundamental numeric types, an empty braced initializer is always assumed to contain the number zero.*/
	std::cout << counter_02 <<std::endl;
}
#include <iostream>

int main()
{
	int written_with_single_quote = 1'234;/*since C++14, you can use the single quote character, ', to make numeric
literals more readable. */
	std::cout << written_with_single_quote<< std::endl;//输出结果为数字 1234

	int hex_num{ 0x4D2 };//hexadecimal literal is prefixed with 0x or 0X
	std::cout << hex_num << std::endl;//输出结果为十进制数1234

	int octal_num{ 02322 };//hexadecimal literal is prefixed with 0x or 0X
	std::cout << octal_num << std::endl;//输出结果为十进制数1234

	int bi_lit{ 0B100'1101'0010 };/*Binary literals were introduced by the C++14 standard. You write a binary integer literal as a sequence of binary digits(0 or 1) prefixed by either 0b or 0B. You can use a single quote as a separator in any integer literal to make it easier to read. */

	std::cout << bi_lit;//输出结果为十进制数1234

}

It is defined such that, for all integers x and y, (x / y) * y + (x % y) equals x.

The result of both the division and modulus operator is undefined when the right operand is zero—what’ll happen depends, in other words, on your compiler and computer architecture.

 


#include <iostream>

int main()
{
	int positive_num = 345;
	int negtive_num = -positive_num; /*The minus sign is a unary operator and it negates the value of a variable.*/
	std::cout << negtive_num<< std::endl;//输出结果为数字 -345;
	//int denominator = 0;//denominator是分母的意思
	//int div_by_zero = positive_num / denominator;//这个statement可以编译通过,但是运行程序时会导致程序出错,中断。

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值