C Language Fundamentals - Data Types
Author: Yin Zhengjie
Copyright: original works, declined to reprint! Otherwise held liable.
A. Constants and variables
1> Keyword
A total of 32 C keywords.
1 > data type key (12) char, Short, int, Long, a float, Double, unsigned, Signed, struct, Union, enum, viod
2 > . Keywords control statement (12)
IF, the else, Switch , Case, default, for, do, the while, BREAK, contiue, GOTO, return . 3 > . type keyword memory (5)
Auto, extern, Register, static, const . 4 >. other Categories (3)
the sizeof, typedef, volatile
2> Data Types
The role of data types: compilers budget Object (variable) allocated memory space.
1> defined data type
definition of variables:
data type variable name [also known identifier], such as: "char Habby;"
variable assignment:
variable name = value, such as: "NAME = Jason Yin;"
data type variable name [ID] = value, such as: "int of AGE = 10;"
2> identifier naming rules.
letters, numbers and underscores;
not start with a digit (first character must be a letter or underscore);
see the name EENOW;
can not function and other identifiers within the same repeat;
not use the built-in C language keyword;
identifiers are case sensitive;
3> Constant
The effects of constants: the program is running, the amount of value which can not be changed; constants generally appear in the expression or assignment statement; a few examples: integer constant: 100 , 200 - 100 , 0 , etc. real constant : 3.14 , 9.18 , 1.44 , etc. character constants: ' a ' , ' B ' , ' 10 ' , ' \ n- ' string constant: " Age " , " 123 " , " Jason " constant definition: constData type constant name; #deifne constant name value Note: by constant "#deifne" is defined according to the value to match the data type, defined in this way is recommended constant. const modified constant is unsafe, can be modified by the pointer
4> Variables
Of a variable:
the program is running, its value can be changed;
variables must be defined before use, you must have the appropriate data type previously defined variable;
the variable characteristics:
Variables assigned to a respective memory at compile time;
by the name and address of the corresponding memory access;
5> Use Cases
[[email protected] / yinzhengjie / code / day002] # CAT variable_definitions.c / * @author: yinzhengjie Blog: http://www.cnblogs.com/yinzhengjie EMAIL: [email protected] * / #include <stdio.h> #define the PI 3.1415926 int main ( void ) { // define the variable int _age = 18 is ; the printf ( " Age D} = {% \ n- " , _age); // define a constant const int MONTH = 12 is ; the printf ( " year-month {% d} \ n- " , MONTH); // references use "#define" variable defined the printf ( " circumferential rate:% F {} \ n- " , the PI); } [[email protected] / yinzhengjie / code / day002] # [[email protected] / yinzhengjie / code / day002] # [[email protected] / yinzhengjie / code / day002] # GCC - O variable_definitions variable_definitions.c [[email protected] .org.cn / yinzhengjie / code / day002] # [[email protected] / yinzhengjie / code / day002] # ./ variable_definitions Age = { 18 is } year { 12} Months circumferential rate: { 3.141593 } [[email protected] / yinzhengjie / code / day002] # [[email protected] / yinzhengjie / code / day002] #
II. Binary
Hex is hex digits, is a method for people to carry provisions. For any band -X band, it means that the number of operation comes just one location x into one. Every decimal is a decimal, hexadecimal is a hexadecimal every, every binary is a binary, and so on, that is, every hex x x carry.
1> Binary
Every 2 into the binary 1, all the numbers 0,1 are composed of, as 11 + 1 = 100 .
Decimal Binary turn:
addition modulo two methods in reverse order (each divided by the decimal number 2, remove the remainder, the results sorted by flashbacks).
Decimal Binary specifically:
the weight method (a binary number of bits from each of N 0 power starts multiplied by 2, and the result is added to the respective bits).
2> octal
八进制逢8进1,所有的数字是0到7组成。
十进制转八进制:
除八反序取余法(将十进制数每次除以8,取出余数,按照结果倒叙排序)。
二进制转化为八进制:
将二进制数字从右到左,每3个二进制数划分为一组,将每组数据的二进制转换成八进制对应的数字即可。
八进制转换为二进制:
八四二一法则(将八进制的每一个位数上的数字拆开,分别用二进制表示即可。)
3>.十六进制
十六进制逢16(F)进1,由数字0-9和字母A-F组成。 十进制转化为十六进制 除十六反序取余法(将十进制数每次除以16,取出余数,按照结果倒叙排序,需要注意的是大于10的数字应该由A-F来表示) 十六进制转化为十进制 权值法(将二进制各个位数从0位开始乘以16的N次幂,将各个位数的结果相加)。 十六进制转换为二进制 八四二一法则(将十六进制的每一个位数上的数字拆开,分别用二进制表示即可。) 二进制转换为十六进制 将二进制数字从右到左,每4个二进制数划分为一组,将每组数据的二进制转换成八进制对应的数字即可。
十六进制转换为八进制
思路就是先将十六进制转换成二进制,再将二进制转换成八进制。
八进制转换成十六进制
思路就是先将八进制换转成二进制,再将二进制转换成十六进制。
4>.进制转换表
5>.在C程序中进制打印案例
[[email protected] /yinzhengjie/code/day002]# cat binary_conversion.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:[email protected] */ #include <stdio.h> int main(void) { //十进制数字10 int a = 10; printf("%d\n",a); //定义八进制 int b = 010; printf("%d\n",b); //定义十六进制 int c = 0x10; printf("%d\n",c); return 0; } [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# gcc -o binary_conversion binary_conversion.c [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# ./binary_conversion 10 8 16 [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]#
[[email protected] /yinzhengjie/code/day002]# cat binary_conversion.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:[email protected] */ #include <stdio.h> int main(void) { //十进制数字10 int a = 10; printf("%o\n",a); //定义八进制 int b = 010; printf("%o\n",b); //定义十六进制 int c = 0x10; printf("%o\n",c); return 0; } [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# gcc -o binary_conversion binary_conversion.c [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# ./binary_conversion 12 10 20 [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]#
[[email protected] /yinzhengjie/code/day002]# cat binary_conversion.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:[email protected] */ #include <stdio.h> int main(void) { //十进制数字10 int a = 10; printf("%x\n",a); //定义八进制 int b = 010; printf("%x\n",b); //定义十六进制 int c = 0x10; printf("%x\n",c); return 0; } [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# gcc -o binary_conversion binary_conversion.c [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# ./binary_conversion a 8 10 [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]#
[[email protected] /yinzhengjie/code/day002]# cat binary_conversion.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:[email protected] */ #include <stdio.h> int main(void) { //十进制数字10 int a = 10; printf("%X\n",a); //定义八进制 int b = 010; printf("%X\n",b); //定义十六进制 int c = 0x10; printf("%X\n",c); return 0; } [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# gcc -o binary_conversion binary_conversion.c [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]# ./binary_conversion A 8 10 [[email protected] /yinzhengjie/code/day002]# [[email protected] /yinzhengjie/code/day002]#
三.计算机内存数值存储方式
1>.原码
一个数的原码(原始的二进制码)有如下特点: 最高位作为符号位,0表示正,1表示负; 其它数值部分都是数值本身绝对值的二进制数; 负数的原码是在其绝对值的基础上,最高位变为1; 举个例子,我们用一个字节的原码来表示+15,-15,+0,-0 +15 : 0000 1111 -15 : 1000 1111 +0 : 0000 0000 -0 : 1000 0000 原码表示方法简单易懂,带有符号数本身转换方便,只要符号还原即可,但当两个整数相减或不同符号相加时,必须比较两个数哪个绝对值大,才能决定谁减谁,才能确定结果是正还是负,所以原码不便于加减运算。
2>.反码
3>.补码
4>.补码的意义
四.
五.