#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可以编译通过,但是运行程序时会导致程序出错,中断。
}