当前位置:网站首页>C language tutorial (2) - printf and data types that come with c
C language tutorial (2) - printf and data types that come with c
2022-07-31 05:45:00 【How so many names are occupied】
Table of Contents
printf
printf outputs the content to stdout (standard output, the final destination is the display), which can be used like this:
printf("hello");printf("hello %s", "xxx");printf("the var va is %d\n", va); //\n is a newline // is a comment
More help information can be entered
man 3 printf
If you want to use printf, you need to write it at the top of the code
#include
#include is a preprocessing command. During preprocessing, copy and paste the content of the file in <> to this location
Where can I find the files in<>?
In linux, the files in <> are in
/usr/local/include/usr/lib/gcc/i386-redhat-linux/4.1.2/include/usr/include...
Look inside.
While #include "" is preprocessing, copy and paste the contents of the file in "" to this location, first search in the current directory, if not found, then go to the place where <> was found.
Data Type
The data type is the access rule to the address space
How computers store data
Computers can only store binary data, namely 0s and 1s.
For signed numbers, the most significant bit is the sign bit, 0 is positive and 1 is negative.In computers, storing positive numbers is directly converted to binary.To store a negative number, you need to first convert the absolute value of the number to binary, then invert the bits, and finally add 1.
For unsigned numbers, there is no sign bit, and it can be directly converted to binary.
Therefore, if an unsigned number 6 is subtracted from a signed number -20, it is a binary number
0000 0110
With binary numbers
1110 1100
Subtract, the result is
0001 1010
If this number is viewed according to the rules of signed numbers, it is 26 in decimal.
This stuff is often tested in interview questions.
sizeof
The sizeof operator can know the number of bytes accessed by a data type or the number of bytes of the address space occupied by a variable. For example, if you want to know the number of bytes accessed by an int type, you can write the following code
sizeof(int)
int type
The int type accesses 4 bytes
short type
short type accesses 2 bytes
char type
Char type access 1 byte
float type
float type accesses 4 bytes, which is the type of decimal
double type
The double type accesses 8 bytes and is a decimal type
Variable Definition
Data type + variable name; a variable is defined
Also available
Data type + variable name = value; initialize variable
For example, define the int variable va, which is initialized to 1.
int va=1;
Variable usage
You can use the variable name directly.For example, output the value of va
printf("va=%d\n", va);
Pointer
A pointer is actually a data type
Let's look at this code
int *p;
* indicates that p is a pointer type.A pointer type variable stores an address.int means that when accessing the address in the variable of the pointer type, the access rules of int are followed.
So, how to get the address of the specified variable?
&+variable can get the address of the specified variable.
For example, to create a pointer type variable, the address of the variable va is stored in the pointer type variable, and the access rules of int are followed when accessing the address in the pointer type variable.The code can be written as
int *p=&va;
Read the contents of the address stored by p, use *p;
printf("va=%d\n", *p);
C language has an interview question called distinguishing whether a computer is big endian or little endian.
The big endian mode means that the high byte of the data is stored in the low address of the address space, and the low byte of the data is stored in the high address of the address space
Little-endian mode means that the high byte of the data is stored in the high address of the address space, and the low byte of the data is stored in the low address of the address space
How to tell?code is
#includeint main(void){short a=0x01;//0x.. is a hexadecimal number, 0x01 is written as a short type is 00000000 00000001char *c=&a;//When accessing the address in the variable, follow the access rules of char, if it is big endian, then *c=0if((*c)==0){//if usage: if(expression){code}.If the expression is not 0, the code in {} is executed.==, if the left and right sides are not equal to 0printf("big\n");}else{//else follows the if, using the method: else{code}.If the expression of if is 0, execute the code in {}printf("small\n");}return 0;}
At this point, we have learned almost the data types that come with the C language.Next we should learn about user-defined data types
边栏推荐
猜你喜欢
随机推荐
代码块、Package,Import,封装(第六天)
uni-app进阶之认证【day12】
数据集划分以及交叉验证法
find、filter、map的区别
Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘
Redis:简单实用
C语言文件读、写、定位函数
11 【组件通信】
正则表达式基础知识
闭包(三)----执行环境
Flink sink redis writes to Redis
Oracle数据库中的“limit”查询
了解SSRF,这一篇就足够了
局部变量成员变量、引用类型、this,static(第五天)
联盟链的真实场景在哪里
剑指offer基础版 ----- 第25天
Flask 的初识
剑指offer基础版 --- 第24天
数据库上机实验3 连接查询和分组查询
Qt Creator + CMake 运行调试总会自动 build 所有目标