C Language Basics Tutorial (my c tour started) [Four]_c language

Source: Internet
Author: User
Tags constant lowercase

10. Basic data type: integral type (top)

1 int of integral type

The C language provides a number of integer types (integers ) that differ in their range of values and whether they can be negative. An int is one of an integral type and is generally called an integral type.
int represents a signed integer , that is, a variable declared with int can be a positive number, can be a negative number, or it can be zero, but only an integer. The minimum range of values for int is -32767 to 32767. The value range of int varies by machine, but must be greater than or equal to-32767 to 32767. Generally, an int occupies a single word of memory space. As a result, a 16-bit old IBM compatible machine uses 16 bits to store int, with a value range of -32768 to 32767 . The current personal computers are generally 32-bit word, these computers, int is usually 32-bit, the value range is 2147483648 to 2147483647. For computers that use 64-bit CPUs, it is natural to use more bytes to store an int, and of course the value range will be greater.


2. declaring a variable of type int

As we have seen in previous tutorials, int declares an integer variable that starts with an int followed by the name of the variable and ends with a semicolon (;). For example:

int erns; / * Declare a variable * *
* Note: Be sure to use a comma (,), not a semicolon (;) * /
int hogs, cows, goats; / * Declare three variables * *

The above declaration creates the variables, but does not provide them with the value . In the previous tutorial, we have used two methods to get the variable "value". One is the assignment : cows = 500;. The other is using the scanf function: scanf ("%d", &goats);. Here's a third way to learn.


3. Initializing variables

Initializing a variable is to assign an initial value to a variable: when declaring a variable, write an equal sign (=) after the variable name, and then write down the values that you want to assign to the variable. For example:

int hogs = 21;
int cows = goats = 14;
int dogs, cats = 94;

The above declaration creates variables and allocates space for these variables, and also assigns an initial value. Note that only cats in the third row is initialized to 94, and dogs is not initialized! The following figure:


4. INT constant

In the example above, 21, 32, 14, and 94 are all integer constants . In C, the default type of an integer constant is int, that is, an integer constant occupies an amount of memory space that is generally equal to the size of the space occupied by a variable of type int. If the integer constant is larger than the value range of int, then the compiler will treat the integer constant as a long int, as we'll talk about later.
21, 32, 14, and 94 are all within the range of int, so they are all int constants.


5. output int type data
We can use the printf function to output int type data. As we saw in the previous tutorial, placeholder%d represents the output of int data, which tells the printf function where to output the appropriate int data. %d is also known as the format qualifier , format specifier, because it specifies what form the printf function should use to output data. The first argument to the printf function can only be a string, which is called a format string. The number of%d in the format string, we should provide a corresponding number of int parameters to the printf function. An int parameter can be an int variable, an int constant, and an expression with an int type. For example:

int year = 2005; / * Year is an int type variable * *
printf ("%d-%d-%d\n", year, 9, 20 + 9); / * 20 + 9 is an addition expression * *

It is our responsibility to ensure that the number of format qualifiers is consistent with the number of parameters, and the compiler is not responsible for capturing this error! For example:

#include <stdio.h>

int main (void)
{
int ten = ten, two = 2;

printf ("%d minus%d is%d\n", ten); / * Write less two parameters.

GetChar (); / * Wait for user to press ENTER * *
return 0;
}

The program can be compiled, but the results will be unexpected, as we write down two fewer parameters. The first%d is replaced by the value of the parameter ten, while the other two%d will be replaced by the value stored in memory. Because the value stored in memory is indeterminate, the output is indeterminate.


6. octal (octal) and hex (hexadecimal)

In C, integer constants are decimal (decimal) integers by default. By adding a specific prefix to the integer constant, you can set it to either a octal or a hexadecimal integer. Prefix 0x or 0X sets the integer constant to a hexadecimal integer. Note that the number 0, not the letter O, don't get it wrong! For example: decimal 16 is 0x10 or 0x10 in hexadecimal notation. Precede the integer constant with prefix 0 , which indicates that it is a octal integer. Note that the number 0, not the letter O. For example: Decimal 16 is the octal system is 020.


7. output data in octal or hexadecimal form

Use the Format qualifier %o to output integers in the form of eight. Note that the lowercase letter O is not the number 0. Use %x or %x to output integers in the form of 16. Lowercase x indicates that the output uses lowercase letters, and capital X indicates that the output uses uppercase letters. Using% #o,% #x or% #X, the resulting output will include the prefix 0,0x or 0X. For example:

#include <stdio.h>

int main (void)
{
int x = 200;

printf ("Dec =%d; octal =%o; Hex =%x; HEX =%x\n ", x, X, x, x);
printf ("Dec =%d; octal =% #o; Hex =% #x; HEX =% #X \ n ", x, X, x, x);

GetChar ();
return 0;
}

The output of this program is:

DEC = 200; octal = 310; hex = C8; HEX = C8
DEC = 200; octal = 0310; Hex = 0xc8; HEX = 0xc8

11. Basic Data type: integral type (medium)

1. Other integer types

int is the basic integer type of the C language, which satisfies our need to process general data. The C language also provides four keywords that can be modified int: Short,long,signed, and unsigned. Using these four keywords, the C language standard defines the following integer types:

1 short Int (abbreviated to short), like int, also signed integers
2 Long Int (abbreviated: Long), signed integer
3 Long int (short: Long Long), C99 the standard added type,
Signed integers
4 unsigned int (abbreviation: unsigned), unsigned integer, cannot represent negative numbers
5) unsigned long int (abbreviated: unsigned long), unsigned integer,
Cannot represent negative numbers
6 unsigned short int (abbreviated: unsigned short), unsigned integer,
Cannot represent negative numbers
7) unsigned long int (abbreviated: unsigned long),
C99 added type, unsigned integer
8 all integer types that do not indicate unsigned are signed integers by default.
In front of these integer types, add signed to make the reader more aware
These are signed integers, although there are no signed representing signed integers.
For example: Signed int is equivalent to int.

In general, we call short an integer , a long length , and a long long as a long integer, and an int as an integral type . The integer types that begin with unsigned are collectively called unsigned integers. For example: We call unsigned short as an unsigned integer . Analogy


2. Declaration mode

These integer types are declared in the same way as the int type. For example:

long int estine;
Long Johns;
short int erns;
Short ribs;
unsigned int s_count;
Unsigned players;
unsigned long headcount;
unsigned short yesvotes;
Long long ago; / * C99 Unique * *
unsigned long long ego; / * C99 Unique * *

If your compiler does not support the C99 standard, you cannot use long long and unsigned longlong.


3. Value range (representation range)

The standard also stipulates that these integer types ofminimum range of values。 The minimum representation range for short is the same as int-32767 to 32767. i.e.-(2^15-1) to (2^15-1). Among them, 2^15 represents 2 of the 15-time side. Similarly, 2 of the 20 times are written as 2^20, and so on. Note: C language 2^15 does not mean 2 of the 15 times, in order to write conveniently, we would like to say so. The minimum range for a long is-2147483647 to 2147483647. i.e.-(2^31-1) to
(2^31-1). The minimum representation range for unsigned short is the same as the unsigned int, which is 0 to 65535 (2^16-1). The minimum range of unsigned long is 0 to 4294967295 (2^32-1). The minimum range for a long long is-9223372036854775807 (-(2^63-1)) to 9223372036854775807 (2^63-1); unsigned long long is 0 to
18446744073709551615 (2^64-1).
Standard specification, int's representation rangecannot be less thanThe representation range of short, the range of longcannot be less thanThe representation range of Int. This means that a short variable may occupy less space than an int variable, while a long variable may occupy more space than an int variable. In a 16-bit computer, int and short are typically 16-bit, and long is a 32-bit 32-bit computer where short is generally 16, while long and int are 32 bits. TC2 (16-bit compiler), int is 16-bit, and dev-c++ (32-bit compiler), int is 32-bit.
Variables declared using the unsigned int can only represent positive integers. If the int is 16 digits, then the unsigned int representation range is 0 to 65535 (2^16-1). This is because the unsigned does not require a symbol bit and can use all 16 bits to represent integers. and int needs a bit assign Bit, used to indicate positive and negative, and only 15 bits are used to represent integers.
Currently, long long is 64 digits, long is 32 bits, short is 16 bits, and int or 16 bits, or 32 bits. The exact number of bits that a compiler uses to represent these types, we can useoperatorsizeof to get. For example:

printf ("%lu\n", (unsigned long) sizeof (int) * 8); / * The number of digits to output int
printf ("%zu\n", sizeof (short) * 8); / * The number of digits to output short * *

sizeof's usage we'll talk about it later, so just have an impression. The %zu in the second sentence is C99 specific, such as the results of your compiler does not support C99 (precisely, if your compiler uses a library function that does not support C99), the result of the run will be an error.


4. Selection of integer types

If you're dealing with just a positive integer , you should prioritize those integer types that start with unsigned. If you are dealing with an integer that is outside the range that int can represent, and long has a representation range larger than int in your compiler, use long. However, if not necessary, try not to use long, as it may reduce the efficiency of the program. One thing to note: If you have a compiler in which both long and int are 32-bit, and you need to use a 32-bit integer, you should use long instead of Int. Only in this way can our programs be safely ported to 16-bit computers, becauseint is generally 16-bit in 16-bit computers. Similarly, if you need to use a 64-bit integer, it's a long long. If int is a bit, then using short can save space, but you have to make sure that the integer you are working on does not exceed the short representation range . This "savings" is meaningless for computers with large memory.

5. long constants and long long constants

in general, integer constants are stored as int types. If we use an integer constant that exceeds the representation of intscope, the C language requires the compiler to automatically use unsigned int to handle this constant. If unsigned is not enough to indicate that thisconstant, the compiler will use a long. If that's not true, then use unsigned long,long long, in turn ., unsigned long long. If unsigned long long is not the case, then the compiler is out of the means. Note: LongLong and unsigned long long are C99 specific. For example, if an int is 16 digits, it will not represent a constant1000000. The compiler uses long to handle this constant, because unsigned int does not represent 1000000.
Similarly, hexadecimal and octal integer constants are usually also treated as int. However, when we use constants that exceed theafter the scope of the int is represented, the compiler uses unsigned int,long,unsigned long,long long andunsigned long long. Until the type used is sufficient to represent that constant.
Sometimes we use smaller constants, but we want this constant to be treated as long, which requires aThe constant is followed by the suffix l (lowercase letter L) or L (capital letter L). We should avoid using L, because L easy andThe number 1 is confusing. For example, an integer constant of 7 is handled as an int, but an integer constant of 7L (or 7l) is taken aslong to deal with. Similarly, after the integer constant plus the suffix ll or ll, the constant is treated as a long longto deal with. For example: 3LL. If you want to use unsigned integer constants, you should also use the suffix u or u. For example: 2u,3u,4lu,5ul,6lu,7llu,8ull,9ull.
These suffixes can also be used for hexadecimal and octal integer constants. For example: 020L,010LL,
0x30ul,0x40ull.

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.