02-C language data types

type of data

insert image description here

variables and constants

initialization

Assigning a value while defining a variable is called "initialization".

constant

There are two ways to define constants:
1. Keywordsconst

const data type constant name = value;
such as: const float pi = 3.14159;

2. Macro definition

#define constant name value
PS: There is no "=" and ";" here,
such as: #define PI 3.14159

It is recommended to define constants in the form of macro definitions.

plastic surgery

hexadecimal

Octal : Beginning with the number 0 indicates that the number is octal. For example: 012 means 12 in octal.
Hexadecimal : Beginning with "0x" or "0X" indicates that the number is hexadecimal. Such as: 0xabc, 0Xabc, 0xABC, 0XABC, these four are the same number.
Binary cannot be written directly.

The so-called "base" is actually a different format of numbers. Integer variables can be assigned values ​​in different formats, and integer variables can also be output in different formats.

Conversion

insert image description here
insert image description here
insert image description here
insert image description here

shaped input

C4996 hint for VS

Since Microsoft does not recommend using the traditional C library functions scanf, strcpy, sprintf, etc. in VS2013, direct use of these library functions will prompt C4996 error: VS recommends using functions with _s, such as scanf_s, strcpy_s,
insert image description here
but these are not Standard C functions.

To continue using this function, you need to add the following directives to the source file to avoid this error message:

#define _CRT_SECURE_NO_WARNINGS     //这个宏定义最好要放到.c文件的第一行
#pragma warning(disable:4996)	//或者使用这个

address-of operator

&For the address-of operator, use &变量名.

	int a;
	//将输入的值存入变量a的地址中
	scanf("%d", &a);

The difference between signed and unsigned numbers

Signed number: the highest bit is the sign bit, 0 represents a positive number, and 1 represents a negative number.
Unsigned number: The highest bit is not a sign bit, but a part of the number. Unsigned numbers cannot be negative.

When we write a program to deal with a negative value that cannot appear, we generally use unsigned numbers, which can increase the maximum value of the number.

sizeof keyword

  • sizeof is not a function, so there is no need to include any header files, its function is to calculate the byte size of a data type in memory
  • The return value of sizeof is size_t
  • The size_t type is unsigned int under the 32-bit operating system, which is an unsigned integer

How to use sizeof
1. For data types

sizeof usage form: sizeof(type)
data type must be enclosed in parentheses: sizeof(int)

2. For variables

Sizeof usage form: sizeof(var_name) or sizeof var_name 
variable name can not be enclosed in brackets. Such as sizeof (var_name), sizeof var_name, etc. are all correct forms

The parenthesized usage is more common and most programmers adopt this form.

#include<stdio.h>

int main() {
    
    

	int a;
	short b;
	long c;
	long long d;

	printf("整形占用的内存为%u字节\n", sizeof(a));
	printf("短整形占用的内存为%u字节\n", sizeof(b));
	printf("长整形占用的内存为%u字节\n", sizeof(c));
	printf("长长整形占用的内存为%u字节\n", sizeof(d));

	return 0;

}
type of data take up space
short (short integer) 2 bytes
int (integer) 4 bytes
long (long shaping) Windows is 4 bytes, Linux is 4 bytes (32-bit), 8 bytes (64-bit)
long long (long long shaping) 8 bytes

PS: The C/C++ standard requires that the memory occupied by long must be at least as large as int, not necessarily larger than int.

Although there is almost no difference between int and long in Windows and Linux (32-bit), they are still two different types. If you have the following function declaration:

void Foo(int value);
void Foo(long value);

That Foo(1)will call the former function, and Foo(1L)will call the latter function.

character type

Character variables occupy 1 byte. The essence of char is a 1-byte integer. So 2 character variables can do calculations.

escape character

escape character meaning
\b Backspace (BS), move the current position to the previous column
\n Line feed (LF), move the current position to the beginning of the next line
\r Carriage return (CR), move the current position to the beginning of the line
? represents a question mark
\0 number 0
\ddd Octal escape character, d range 0~7
\xhh Hexadecimal escape character, h range 0~9, a~f, A~F
#include<stdio.h>

int main() {
    
    
	printf("abc");//(1)
	printf("\b456");//(2)
	printf("\n");//(1)+(2)输出ab456

	printf("abc");//(3)
	printf("def");//(4)
	printf("\r123\n");//(5)  (3)+(4)+(5)输出123def,注意(5)中的\n不能少,否则输出123 ef
	return 0;
}

How numbers are stored in memory

original code

  • The highest bit is used as the sign bit, 0 means positive, 1 means negative
  • The other numerical part is the binary number of the absolute value of the value itself
  • The original code of the negative number is based on its absolute value, the highest bit becomes 1

inverse code

  • For positive numbers, the inverse code is the same as the original code
  • For negative numbers, the sign bit remains unchanged, and the other parts are reversed (1 becomes 0, 0 becomes 1)

Complement

  • For positive numbers, the original code, inverse code, and complement code are the same
  • For a negative number, its complement is its complement plus 1
  • The sign bit of the complement code remains unchanged, and the other bits are negated, and finally the whole number is added by 1 to obtain the original code.
    In the computer system, the values ​​are all stored in the complement code.
    When adding two numbers expressed in two's complement, if the highest bit (sign bit) has a carry, the carry is discarded

Signed and unsigned ranges

Signed character value range : − 2 7 -2^727~ 2 7 − 1 2^7-1 271
insert image description here
artificially defined "-0 (1000 0000)" as the minimum negative number in the interval, so the minimum value is not -127, but -128.

Signed integers and long integers are the same.
Signed integer defines "-0" as − 2 31 -2^{31}231 .
Under Windows or Linux (32 bits), signed long integer defines "-0" as− 2 31 -2^{31}231

Unsigned character value range : 0~ 2 8 − 1 2^8-1281 (0000 0000 ~ 1111 1111)
Unsigned integer and long integer are the same.

data overflow

The value after data overflow is still within the value range, but it just goes into a "cycle".

#include<stdio.h>

int main() {
    
    

	char a = 127;//取值范围:-128到127
	a = a + 1;
	unsigned char b = 255;//取值范围:0到255
	b = b + 1;
	printf("a = %d, b = %d\n", a, b);//输出a = -128, b = 0
	return 0;
}

String formatting input and output

A string is a continuous char space in memory , ending with '\0' (number 0).
String constants differ from character constants:
insert image description here

At the end of each string, the compiler will automatically add an end flag '\0', that is, "a" contains two characters 'a' and '\0'.

printf function and putchar function

  • printf is to output a string;
  • putchar outputs a char;

printf format characters:

print format corresponding data type meaning
%p void * Output address in hexadecimal form
%% % output a %

printf additional format:

character meaning
l (letter l) Appended in front of d, u, x, o, indicating a long integer
- align left
m (represents an integer) data minimum width
0 (number 0) Add 0 to the front of the output until the specified column width is filled. It cannot be used with -
mn (represents an integer) m refers to the field width, that is, the number of characters occupied by the corresponding output item (including the decimal point) on the output device. n refers to the precision, which is used to specify the number of decimal places of the output floating-point number. For numeric types, when n is not specified, the implied precision is n=6 digits.
#include<stdio.h>

int main() {
    
    
	int abc = 10;
	printf("abc = '%6d'\n", abc);//      输出abc = '    10' 
	printf("abc = '%-6d'\n", abc);//     输出abc = '10    '
	printf("abc = '%06d'\n", abc);//     输出abc = '000010'
	printf("abc = '%-06d'\n", abc);//    输出abc = '10    '

	double def = 12.3;
	printf("def = '%6.2f'\n", def);//    输出def = ' 12.30'
	printf("def = '%-6.2f'\n", def);//   输出def = '12.30 '
	printf("def = '%06.2f'\n", def);//   输出def = '012.30'
	printf("def = '%-06.2f'\n", def);//  输出def = '12.30 '
	printf("def的内存地址:%p\n", &def);//输出def的内存地址:006FFBDC
	printf("输出一个%%\n");//             输出一个%

	return 0;
}

scanf function and getchar function

  • Scanf can get the data input by the user through the standard input device through % ​​escape;
  • getchar reads a char from the standard input device;
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int main() {
    
    
	int a, b;
	printf("请输入2个整数(以空格或回车作为分隔符):\n");
	scanf("%d%d", &a, &b);
	printf("您输入的2个整数是%d和%d\n\n", a, b);
	printf("请输入2个整数(以逗号作为分隔符):\n");
	/*
	scanf的第一个参数可以指定输入的分隔符,
	但是不能用\n作为分隔符,所以scanf("%d\n%d")是不允许的。
	此处以逗号作为分隔符。
	*/
	scanf("%d,%d", &a, &b);
	printf("您输入的2个整数是%d和%d\n\n", a, b);
	printf("请输入2个整数(第一个整数最多为3位整数):\n");
	scanf("%3d%d", &a, &b);
	printf("您输入的2个整数是%d和%d\n\n", a, b);
	return 0;
}

The input and output results of the above code are as follows:
insert image description here

#include<stdio.h>

int main() {
    
    

	char ch1, ch2;

	printf("请输入第1个字符:\n");
	ch1 = getchar();//此处输入一个字符后需回车才能输出下面的文字
	printf("您输入的字符为%c\n", ch1);
	/*
	这里的getchar()是将上面的输入的回车读取出来,方便后面接收新的字符输入
	*/
	getchar();
	printf("请输入第2个字符:\n");
	ch2 = getchar();
	printf("您输入的字符为%c\n", ch2);

	return 0;
}

Guess you like

Origin blog.csdn.net/jiejingguo/article/details/130604942