Copyright notice: This article from Administration Blog, reproduced please indicate the source: http://blog.csdn.net/a_zhon/.
Directory (?) [+]
Before we learn the C data types, let's review the basic data types and their features in Java: Basic data type 1. There are eight basic data types in Java and one reference data (String) type as follows:
byte 1字节 char 2字节 short 2字节 int 4字节 float 4字节 long 8字节 double 8字节 boolean
2. What are the types of C? We can take a look.
int 4字节char 1字节float 4字节double 8字节long 4字节short 2字节signed 4字节unsigned 4字节void 1字节
The 3.C language provides us with a function that can be used to calculate the length of a type directly, so it is very convenient to know the length of a type.
Length of calculation type: sizeof (type) returns a length of type int main () {The length of the calculation typeprintf"Byte of type int:%d\n",sizeofint));printf"Char type occupies bytes:%d\n",sizeofchar));printf"The byte of type float:%d\n",sizeoffloat));printf"The byte of the double type:%d\n",sizeof (double)); printf ( "Long" of bytes:%d\n ", sizeof (long)); printf ( "short type of bytes:%d\n", sizeof (short)); printf ( "signed type of bytes:%d\n", sizeof (signed)); printf ( "unsigned type takes up bytes:%d\n", sizeof (unsigned)); printf ( "void type occupies bytes:%d\n", sizeof (void))}
4. Careful you may find out why a Boolean type is missing? How do you judge that? The original C language used the non-0 and the "%" is true and false.
//C语言中boolean类型,0表示为false,非零表示true if(0) { printf("false\n"); } else { printf("true\n"); }
Some differences between the 5.Java basic data types and C languages:
- The length of the char type in Java is 2 bytes, and the length in the C language is 1 bytes
- The length of a long type in Java is 8 bytes, and the length in C is 4 bytes (C99 (99) Standard provisions: Long type, not less than shaping)
- No byte in C language
- Boolean type in c language, 0 = false, nonzero means true
- Singed: Signed Range: 128 ~ 127–> ( -2^7 ~ 2^7-1)
- Unsinged: Unsigned range: 0 ~ 255–> (0 ~ 2^8-1)
- void: No type, representing any type
- There are no string types in the C language, but you can use a char array to represent
Two: Output function 1. The output function is much simpler. than Java's
System.out.println()
And a few more code.
printf("内容");//如果需要换行我们只需要添加换行符就行了printf("\n","内容");
2. Write a simple output statement
main() { char c = ‘a‘; printf("c==\n",c); //使docs窗口停留 system("pause");}
Why didn't we export our A? The original C language in the output to use the placeholder, then we add a placeholder to try
main() { ‘a‘; printf("c==%c\n",c); //使docs窗口停留 system("pause");}
Now it's time to get the output, so let's talk about the placeholder for this ghost thing.
- int–>%d
- Long int–>%ld
- Char–>%c
- Float–>%f
- Short Plastic –>%HD
- Double–>%lf
- Hex Output –>% #x
- unsigned (unsigned) –>%u
- String –>%s
- Octal –>%o
Different types have to be exported with different placeholders, otherwise the accuracy is lost.
3. Now let's define some variables and output them
Main () {char c =' A ';int i =12345678; Long L =526247678; float F =3.1415; Double d =3.1415926535;//Back analysis, why do you write char* str="I am a string"; printf ("c==%c\n", c); printf ("i==%d\n", i); printf ("l==%ld\n", L); printf ("f==%f\n", f); printf ("d==%lf\n", D); printf ("str==%s\n", str); //Make Docs window stay system ("pause");}
Gee ~, the output of f behind how much more than two a 0? In the C language, the default is to retain six decimal places, in order to retain the corresponding number of digits, you need to add ". Reserved digits" In the following code:
printf("f==%.4f\n",f);//效果就是f==3.1415,图就不贴了
Three: Input function 1. Introduction to input functions
scanf("占位符",内存地址);
2. Write a simple input function, type a number from the console and print it to the console
int i;printf("亲!请输入一个数字:\n");//&i,就是取i的地址,也就是把从控制台接收到的数据赋值给i。scanf("%d",&i);printf("输入的数字为:%d\n",i);
3. Use arrays to represent our strings, use a For loop to print the group contents
//定义一个数组[]必需在数组名右边 char arr[]= {‘H‘,‘e‘,‘l‘,‘l‘,‘o‘}; //for循环需要将变量在外面,不能像java一样定义在里面 int j; for(j=0; j<5; j++) { printf("arr[%d]=%c\n",j,arr[j]); } //C语言中没有String类型,但是可以用char数组来表示 //直接将数组输出 printf("arr:%s\n",arr);
4. We can also enter a string from the console
printf("请输World:\n"); //声明数组和数组长度 char arr2[5]; //将控制台的内容赋值给arr2 scanf("%s",&arr2); printf("arr2:%s\n",arr2);
5. In the output of the array you will find that you will output a lot of messy things, because: the array is a contiguous memory space, you need to add an array at the back of the' + 'Indicates the end
char arr3[]= {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘\0‘}; printf("arr3==%s\n",arr3);
This article is about here, the next will continue to learn the C language pointers and other content, the pace of learning can not stop, continue to move forward.
Android JNI Programming (ii) Basic data type of--c language, output function, input function