C language - Basics - learning Notes (2): constant and variable & basic data types
First, constant and variable
1. Constant
Refers to an amount constant unalterable during program execution, the value C language can be represented by a constant. It may represent constant values for various types of data.
To sum up sign when printing, various types of constants to be replaced:
Types of | Number of Bytes | It represents the range | Replacement character (printf) |
---|---|---|---|
char | 1 | -128~127 | %c |
unsigned char | 1 | 0~255 | %c |
short | 2 | -32768~32767 | %hd |
unsigned short | 2 | 0~65535 | % Hu |
int | 2 | -32768~32767 | %d |
unsigned int | 2 | 0~65535 | in% |
long | 4 | -2147483648~2147483647 | %ld |
unsigned long | 4 | 0~4294967295 | % Lu |
float | 4 | -3.4* 10-38 ~ 3.4* 1038 | %f |
double | 8 | -1.7E-308 ~ 1.7E + 308 | %lf |
2. Variable
Refers to the amount of variables in the program can be changed, the variable names and values of the composition. The variable name is an identifier that is the value of a variable data value.
Each program is assigned a variable storage space, which data values stored in the space, the program can use the variable name to access the memory to enable access and modify the contents of variables to read and modify the space.
Declare variables as follows:
Data type name variable name
int year
double time
char flag
Variable definition is a complete sentence, is run in order to work, while the variable declaration is to work at compile time. Under normal circumstances, you can define the variable before long statement on the use of statements .
If you want to use a variable before the variable definition statement, need to use extern keyword to declare variables.
Extern to declare variables using the form below:
extern data type name variable name
extern int key
extern double score
Example 1
#include <stdio.h>
int main(void)
{
extern int data;
printf("data = %d\n",data);
return 0;
}
int data = 23;
Variable assignment means setting a value for the process variable.
Assignment of variables in the form below:
Variable name = value
A int; A = 2;
Double B; B = 3.2;
the above embodiment the amplitude can also write:
int = A 2;
Double B = 3.2;
Example 2
#include <stdio.h>
int main(void)
{
int x = 2;
int y = 2;
int a,b,c = 3;
int i = 3, j = 3, k = 3;
printf("x = %-8d y = %-8d\n",x,y);//%-8d表示以8个数字为宽度进行左对齐
printf("a = %-8d b = %-8d c = %-8d\n",a,b,c);
printf("i = %-8d j = %-8d k = %-8d\n",i,j,k);
return 0;
}
This is because only the amplitude c is 2, the variables a, b values has not yet been initialized as unknown.
3. The address pointer operator and operator
After the variable is set, it will occupy a memory, and memory are all addresses. Variable memory address can also directly into the address of the variable.
Address Address operator (&) to obtain a variable, a pointer operator (*) to obtain an address on the memory contents.
Address operator can only use the variable as an operand. Pointer operator must address.
Example 3
#include <stdio.h>
int main(void){
int key = 0;
printf("Please input a integer: ");
scanf("%d",&key);
printf("key\t = %d\n",key);
printf("&key\t= %p\n",&key);
printf("*(&key)\t= %d\n",*(&key));
return 0;
}
Exercise 1
//使用printf输出Copyright 2007-2008 ALU corporation.
# include <stdio.h>
int main(void){
printf("Copyright 2007-2008 ALU corporation.\n");
printf("Copyright %d-%d ALU corporation.\n",2007,2008);
return 0;
}
Exercise 2
//定义两个整型变量,分别使用scanf函数为他们赋值,并用printf输出
#include <stdio.h>
int main(void){
int m = 0;
int n = 0;
printf("Please enter two integers:");
scanf("%d %d",&m,&n);
printf("The two numbers are %d %d",m,n);
return 0;
}
Second, the basic data types
1. integer data types
Byte length: Short int <int <Long int <Long Long int
sizeof function can be footprint data type, in bytes.
sizeof (data type)
Example 1
#include <stdio.h>
int main(void){
printf("sizeof(shor int) = %d\n",sizeof(short int));
printf("sizeof(int) = %d\n",sizeof(int));
printf("sizeof(long int) = %d\n",sizeof(long int));
printf("sizeof(long long int) = %d\n",sizeof(long long int));
return 0;
}
As can be seen from the figure the number of bytes occupied by each type of the variable.
Unsigned data type and as modifiers with signed integer data prefix, but with modified unsigned data type represents a non-negative number.
Example 2
#include <stdio.h>
int main(void){
/*定义多个变量以存储多个不同整型的数据*/
int int_dec,int_hex,int_oct; //定义三个int变量
int constant = 74; //定义并初始化变量
/*用三种进制形式给整型变量赋值*/
scanf("%d",&int_dec); //以十进制值形式为int_dec赋值
scanf("%x",&int_hex); //以十六进制值形式为int_hex赋值
scanf("%o",&int_oct); //以八进制值形式为int_oct赋值
/*用十进制的形式给整型变量赋值*/
printf("int_dec(11) = %d\n",int_dec); //打印十进制的11
printf("int_hex(11) = %d\n",int_hex); //打印十六进制的11
printf("int_oct(11) = %d\n",int_oct); //打印八进制的11
/*把十进制74用三种进制分别打印*/
printf("(Dec)74 = %d\n",constant); //输出74的十进制形式
printf("(Hex)74 = %x\n",constant); //输出74的十六进制小写形式
printf("(Hex)74 = %X\n",constant); //输出74的十六进制大写形式
printf("(Oct)74 = %o\n",constant); //输出74的八进制形式
return 0;
}
If you are using scanf% i can accept all forms
Storage
Integer values and in different storage intervals can be divided into two categories: signed integer and an unsigned integer. Wherein the signed integer is stored in a memory divided into two parts, a symbol part and a numeric part.
Part represents a symbol **: 0 is non-negative, a negative. ** integer variable value stored in the portion in the form of a binary value. Its value range see table top.
2. The character data type
Table escape character
ASCII code table
With integer values assigned integer value is automatically converted to the corresponding character variable assigned to the ASCII code.
char c1 = 97; // i.e. 'a'
Example 3
#include <stdio.h>
int main(void){
/*定义并初始化各种字符型变量*/
char c1 = '\n';
unsigned char c2 = '\t';
char c3 = 49;
printf("c1 = [%c],[%d]\n]",c1,c1); //输出c1的字符形式和ASCII码值
printf("c2 = [%c],[%d]\n]",c2,c2); //输出c2的字符形式和ASCII码值
printf("c3 = [%c],[%d]\n]",c3,c3); //输出c3的字符形式和ASCII码值
return 0;
}
Will be represented by its original meaning in% c,% d will be represented by the ASCII value for the.
3. floating-point data types
Byte length : a float <Double <= Long Double
Example 4
#include <stdio.h>
int main(void){
printf("sizeof(float) = %d\n",sizeof(float));
printf("sizeof(double) = %d\n",sizeof(double));
printf("sizeof(long double) = %d\n",sizeof(long double));
return 0;
}
On floating point data type and value range Number of Bytes See the table top.
Accuracy is preserved :%. + Number + placeholders
After seven decimal places, for example, can be written as:.% 7f. (Float 7 can be retained after the decimal point, double, and long double 16 can remain after the decimal point)
Storage
to representation LCyT stored in memory. It consists of three parts: a sign bit, the exponent bits, radix bits
4. User-defined types
typedef
and three parts typedef statements: keyword typedef, type the name of the original data and the new data type names. Standard form as follows:
typedef original data type of the new data types
typedef char myChar; // char data type to an alias myChar
All the new data type names may only consist of a single data type, not as part of other data types.
For variables that may change the data type, are generally used data type alias name.
In order to improve readability, often reflected in the renaming of the median.
For example:
typedef char int8_t; // char type is defined as the int8, because there are eight char
typedef unsigned long uint32_t; // the unsigned long type defined uInt32, because there are 32-bit unsigned long
5. qualifier
To a variable is defined as const, means that the value of the variable will not be changed;
the definition of a variable as volatile, meaning the compiler not optimize the variable.
const main role is to define the read-only variables.
const variable name data type name = initial value;
const int NUM_MONTH = 12;
When you define a const, the variable must be initialized, otherwise it will error.
Most compilers do not allocate memory space for const variables, but they are stored in the symbol table, improve program efficiency.
volatile expressed its modified variables may change at any time, you have to re-read every time the value of this variable each time the compiler optimization.
volatile data type name variable name
volatile int data = 10;
in the preparation of multi-threaded programs by defining volatile, so that the program will read the variable before each synchronization thread to get the latest value of the variable is modified.
Exercise 1
//输入char型、int型和float型,再将其输出
#include <stdio.h>
int main(void){
/*定义3个变量*/
char c_v = 0;
int i_v = 0;
float f_v = 0;
printf("Input a char number:");
scanf("%c",&c_v);
printf("Input a int number:");
scanf("%d",&i_v);
printf("Input a float number:");
scanf("%f",&f_v);
printf("c_v = [%c]\n",c_v);
printf("i_v = [%d]\n",i_v);
printf("f_v = [%f]\n",f_v);
return 0;
}
Exercise 2
//设计一个程序,将输入的小写字母对应的大写字母输出
#include <stdio.h>
int main(void){
char letter = 0;
printf("Input a lowerletter(a~z):");
scanf("%c",&letter);
printf("The capital is %c.\n",letter + 'A'-'a');
return 0;
}
Exercise 3
//设计一个程序来验证浮点数的精度损失
#include <stdio.h>
int main(void){
float n = 0.0;
n = 321654.32654;//有效数字超过8位,即将精度损失
if(n != 321654.32654)
printf("data lost\n");
return 0;
}