当前位置:网站首页>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 commentMore help information can be entered
man 3 printfIf 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 0110With binary numbers
1110 1100Subtract, the result is
0001 1010If 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
边栏推荐
猜你喜欢
随机推荐
Sword Point Offer Special Assault Edition ---- Day 2
剑指offer专项突击版 ---- 第 6 天
wpf ScrowViewer水平滚动
uni-app进阶之生命周期【day8】
12 【nextTick 过渡与动画】
Redis:简单实用
Redis:安装使用
win11中利用IIS10搭建asp网站
局部变量成员变量、引用类型、this,static(第五天)
wpf wrapPanel居中并从左到右排列
字符串的新增方法
剑指offer基础版 ----- 第25天
Proteus 8 Professional安装教程
The process and specific code of sending SMS verification code using flask framework
PAT_乙级_真题练习_1007_素数对猜想
C语言教程(一)-准备
数据库上机实验2 单表查询和嵌套查询
基于web3.0使用钱包Metamask的三方登陆
数据集划分以及交叉验证法
剑指offer基础版 --- 第24天








