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

Source: Internet
Author: User
Tags int size lowercase reserved

12. Basic data type: integral type (bottom)

1. output variables of various integer types

To output different types of integers, you need to use a format qualifier that is not used. Output an integer of type unsigned int, with %u . Output long with %ld; If you want to output in hexadecimal or octal form, use %lx(or %lx) or %lo. Note: Although the suffix of an integer constant does not matter in uppercase or lowercase English letters, they must be in lowercase with the format qualifier! If we want to output a short type of integer, you can prefix h, or %hd, in the middle of%d,%ho and %hx(or %hx ) represents output in eight or hexadecimal notation, respectively. The prefix h and L can be combined with u to represent the output unsigned integer. For example:%lu represents the output unsigned long integer;%hu represents the output unsigned short type integer. If your compiler supports C99, you can use %lld and %llu respectively to represent the output long long and unsigned long. Let's look at a program that outputs various types of integers:

#include <stdio.h>

int main (void)
{
unsigned int un = 3000000000; / * I am using the compiler int is 32-bit * /
Short end = 200; * * and Short is 16-bit
Long big = 65537;

printf ("un =%u and not%d\n", un, un);
printf ("end =%hd and%d\n", end, end);
printf ("big =%ld and not%hd\n", big, big);

printf ("Press ENTER to quit ...");
GetChar ();
return 0;
}

Run this program using dev-c++ compilation The output is as follows:

UN = 3000000000 and not-1294967296
End = 200
Big = 65537 and not 1
Press ENTER to quit ...

      This program indicates that using a format qualifier incorrectly can result in unexpected output. First, the error uses%d to do the format qualifier for the unsigned integer variable un, resulting in a negative number. This is because my computer uses the same binary form to represent 3000000000 and 129496296, and the computer only knows binary . So, if we use%u to tell printf to output unsigned integers, that's 3000000000, and if we misuse%d, then the output is a negative number. However, if we change the code 3000000000 to 96, the output will not be abnormal. Since 96 does not exceed the representation range of Int.
     Then, for the second printf, regardless of whether we use%HD or%d, the output results are the same. This is because the c  language standard stipulates that when the short type value is passed to the function, it is automatically converted to the int type value. It is converted to int because int is designed to be the most efficient integer type of computer processing. So, for computers with a short and int size, converting the variable end to an int type and passing it to a function is faster. So, H seems to have no meaning. Fact We can use%HD to see what happens when a larger integer type is truncated to a short type.
    and the third printf, which causes the output to be 1 due to misuse of%HD. This is because if long is 32 bits, the binary form of 65537 is 0000 0000 0000 0001 0000 0000 0000 0001, and the%HD command printf outputs the  short type value, which causes printf to only pair 16-bit processing, resulting in an output of 1.
     in the previous tutorial, we said that it is our responsibility to ensure that the number of format qualifiers is consistent with the number of parameters. Similarly, it is our responsibility to ensure that the type and parameter types of the format qualifiers are consistent ! As mentioned above, using a format qualifier incorrectly can result in unexpected output!


2. integer Overflow

First, look at the following programs:

#include <stdio.h>

int main (void)
{
/* 32-bit int denotes the upper and lower limits of the range * *
int i = 2147483647, j =-2147483648;
unsigned int k = 4294967295, L = 0;

printf ("%d%d%d%d\n", I, I+1, J, J-1);
printf ("%u%u%u%u\n", K, K+1, k+2, L, L-1);

printf ("Press ENTER to quit ...");
GetChar ();
return 0;
}

Run this program using dev-c++ compilation The output is as follows:

2147483647-2147483648-2147483648 2147483647
4294967295 0 1 0 4294967295
Press ENTER to quit ...

In this program, i+1 is a negative number, j-1 is a positive number, k+1 is the 0,L-1 is 4294967295. This is because, after the addition and subtraction operations, their values are beyond the range of the type of integers they correspond to, we call this behavior overflow .
If the value of the unsigned int variable exceeds the upper bound, it returns 0 and then increases from 0. If less than 0, the upper bound of the unsigned type is reached, and then the upper limit is reduced. It's like a man running around the runway, circling around and back to the starting point. If an int variable overflows, it becomes a negative number, or a positive number.
For integers of type unsigned, they overflow with the same conditions as described above, which is the standard rule. But the standard does not specify what happens when a signed integer overflows. This is the most common case of a signed integer overflow that is described here, but different situations may occur on other computers using other compilers.

13. Identifiers Naming rules

1. length Limit

C89 stipulates that the compiler should at least be able to handle internal identifiers (internal identifier) within the character (including 31), and for external identifiers (external identifier), the compiler should at least be able to handle an external identifier within 6 characters (including 6). An identifier is a name that we take for variables (variable), macros (macro), or functions (function), and so on. for example int num; The NUM in this statement is an identifier.
The latest C99 standard stipulates that the compiler should at least be able to handle internal identifiers within the characters (including 63), and that the compiler should at least be able to handle external identifiers within the characters (including 31).
In fact, we can use characters that exceed the maximum number limit to name identifiers, but the compiler ignores that part of the character . In other words, if we use 35 characters to name a variable, and that compiler can only handle a variable name of 31 characters, then the extra 4 characters will be ignored by the compiler, only the preceding 31 characters are valid. Some ancient compilers can only handle identifiers within 8 characters, and for such compilers, identifiers Kamehameha and Kamehameko are equivalent because they precede the 8-word story.


2. available characters and composition rules

The standard stipulates that identifiers can only be written in uppercase and lowercase letters , underscores (_), and Arabic numerals . The first character of an identifier must be uppercase or lowercase letters or underscores, not numbers.

Legal namingillegal naming
Wiggles $Z]*** * $,] and * are illegal characters/
Cat2 2cat/ * Cannot start with a number * *
Hot_tub hot-tub/*-is illegal character * *
TaxRate Tax Rate* * Can not have spaces */
_kcab don ' t/ * ' is illegal character *

The operating system and the identifiers in the C language standard library generally begin with an underscore, which is customary. Therefore, we should avoid using underscores as the beginning of our own defined identifiers.
C is a case-sensitive language, in other words, Star, Star, Star,star, and star are all different identifiers.
We cannot use keywords and reserved identifiers to name our custom variables. For keywords and reserved identifiers, click the keyword and the reserved identifier

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.