C language 06-Basic data types

Source: Internet
Author: User
Tags integer numbers

    • One, variable
    • Second, type modifier
    • Three, the storage length of the basic data type under different compiler environment

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

C has a rich data type, so it is well suited for writing databases, such as DB2 and Oracle, written in C.

The data types of the C language can be broadly divided into the following categories:

One, variable

As in other languages, the C language uses variables to store the values used by the calculation process, and any variable must first define the type and then use it. Why must we define it first? Because the type of the variable determines the storage space that the variable occupies, the variable type is defined to allocate the appropriate storage space for the variable to hold the data. For example, if you are a char type, I will only allocate 1 bytes to you, it is not necessary to allocate 2 bytes, 3 bytes or more storage space.

The table below describes the storage space used by the basic data types in a 16-bit compiler environment, and it is helpful to learn about these details and how to learn pointers and arrays in the future.

It is important to note that:

1. The use of local variables is a little bit different from Java

1> in Java, if you declare a local variable and use it without initializing the assignment, the compiler directly complains

The 9th line is an error because variable A is not initialized

2> in C, when you declare that you see a local variable, you can use it without initializing the assignment.

1 #include <stdio.h>2 3 int main () 4 {5     int b;           6     printf ("%d", b); 7     return 0;8}

But this is very dangerous and is not recommended. Most people should think that variable B should be 0, not really. Because the system will randomly assign a value to the variable B, the resulting garbage data.

The printed result of the above code is:

Therefore, a local variable must be initialized before it is used, which is the safest way to do it.

* If it is a global int type variable, the system assigns a value of 0 by default

The range of 2.char is:ASCII character or -128~127 integer

Thus, using char to store capital letters A has 2 ways of assigning:

Mode 1char c = ' A ';//mode 2char c = 65;

The above two methods are equivalent because the ASCII value of capital A is exactly 65. Click to view all the values of the ASCII code table.

3.char can only store one character

Chinese characters or strings need to be stored in an array of characters, because one character occupies 2 characters, and a string is made up of one or more characters.

Therefore, the following syntax is wrong:

Char C1 = ' i '; char c2 = ' 123 '; char C3 = "123";

Second, type modifier

We can also add some modifiers to the base data type, and some call it the qualifier, the same meaning.

The following 4 types of modifiers are available:

    • Short type
    • Long type
    • Signed-Signed
    • Unsigned non-symbolic

1. Usage Demo

These modifiers are most often used to modify the int type (you can omit int)

1//The following two types of notation are equivalent to 2 short int s1 = 1; 3 short s2 = 1; 4  5//The following two formulations are equivalent 6 long int l1 = 2, 7 long L2 = 2, 8  9//Can be used consecutively 2 long10 long LL = 10;11 12//The following two notation is equivalent to 13 Signed int SI1 = 3;14 signed Si2 = 3;15 16//The following two notation is equivalent to the unsigned int US1 = 4;18 unsigned US2 = 4;19 20//can also use 2 modifiers 2 1 signed short int ss = 5;22 unsigned long int ul = 5;

2.short and Long

1> Short and long can provide different lengths of integer numbers, that is, you can change the range of integers, such as the value range of short is the -32768~32767,long range is -2147483648~2147483647

2> of course, the storage length of the data will change as well. For example, in a 64-bit compiler environment, short accounts for 2 bytes (16 bits), int accounts for 4 bytes (32 bits), and long takes 8 bytes (64 bits). There are many compilers in the world, different compiler environments, the range of values and the length of occupation is not the same, but fortunately, the ANSI \ ISO set the following rules:

* Short and int are at least 16 bits (2 bytes)

* Long is at least 32 bits (4 bytes)

* The length of short cannot be greater than int,int length cannot be greater than long

* Char must be 8 bits (1 bytes), after all char is the smallest data type we can program

3.signed and unsigned

1> signed represents unsigned, including positive, negative, and 0;unsigned for unsigned, including only positive numbers and 0. For example, the value range of signed is -32768~32767, then the unsigned range is 0~65535, of course, different compilers have different range of values

2> in fact, the difference between signed and unsigned is whether their highest bit is to be used as a sign bit, and does not change the length of the data as short and long, that is, the number of bytes,

4.signed, unsigned can also be modified Char,long can also be modified double

unsigned char c1 = 10;signed char c2 = -10;long double D1 = 12.0;

Three, the storage length of the basic data type under different compiler environment

Red represents a common data type

C language 06-Basic data types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.