Real variables (float variable), character data, a string constant (variable), character constant (variable)

Real variables

1, a rounding error of real variables
because the memory space allocated to real variables is limited, so when storing data, rounding will encounter problems, we give an example

void main()
{
float a,b;
a=123456.789e5;
b=a+20;
printf("%f\n",a);
printf("%f\n",b);
}

Here Insert Picture Description

The results appear this time is such, and because a spill itself, and after 20 plus also did not produce change.

Character data

Character data including character constants and character variables.

Character constants : a character enclosed in single quotes.
For example: 'A', 'M' are valid character constant.

In the C language, character constant has the following characteristics:
1) only character constant enclosed in single quotes, not double quotes or other brackets.
2) Character constants can only be a single character, not a string.
3) character can be any character centralized character. However, after the character number is defined as the value can not participate in the operation. The '5' and 5 are different. '5' is a character constant, can not participate in operations.

Character variable
type specifier character variable is char. Character variable type definition format with integer variables: char a, b;

Escape character:
escape character is a special character constants. Backslash "\" followed by one or more characters. Escape character has a specific meaning, with the original meaning of the character contrast, the so called "escape characters", such as the common program \ n, meaning that a carriage return line feed. The escape character is used to indicate that the main control code is not easy with the general character indicated.

Use the escape character:

#include <stdio.h>

void main()
{
   int a,b,c;
   a=5;b=6;c=7;
   printf("ab c\tde\rf\n");
   printf("hijk\tL\bM\n");
}

The result of this procedure the final output:
Here Insert Picture Description
Here you can see, by using escape characters, some less easy to express that out of characters gave out.

Character data storage format and use in memory.

Only the memory allocated to a variable byte space character, you can only store one character. Character values in the form of ASCII code stored in the memory unit variables. The decimal ASCII code x is 120, y is 121. The decimal ASCII code for a character variable a, b impart 'X' and 'Y' values:
A = 'X';
B = '. 7';
here is that, in a, b and the two units 120 and storing the binary code 55.

for example:

#include <stdio.h>
void main()
{
char a,b;
a = 120;
b = 121;
printf("%c,%c\n",a,b);
printf("%d,%d\n",a,b);
}

Put it in the final output:
Here Insert Picture Description
you can clearly see. We are the first line of output in the form of characters (it will look for the corresponding ASCII characters in the table), the second line in the form of an integer to print out, in fact, a binary number is stored in memory 120 and 121. Similarly, the a = 120; replace a = 'x'; is the same effect.

[4.1] lowercase letters into uppercase letters

#include <stdio.h>
void main()
{
char a,b;
a = 'a';
b = 'b';
a = a - 32;
b = b - 32;
printf("%c,%c\n%d,%d\n",a,b,a,b);//将a、b的字符形式显示出来,再将a,b的整型形式展现出来(即内存中存放的数值)
}

The result of this output is as follows:
Here Insert Picture Description
Why allocated memory space char is one byte it? Because a byte is 8 bits, 2 ^ 8 256 can accommodate a full ASCII table.

String constants:
a character sequence consisting of a pair of double-quoted. Such as "China", "boy" and so are legitimate string constants.
And character string constants are constants different amounts, character constants are enclosed by a single quotation marks, string constants enclosed in double quotes; character constants can only be a single character string constant may contain a character or characters; can the character a character constant is assigned to a variable, but not a character string assigned to a variable constant (k can char a = 'a'; but not char a = "a";) ; a byte character constant amount space, and the space occupied by the string, the string variable number of bytes plus 1 plus 1 is the end of this symbol of the string "\ 0" (ASCII code 0)

Published 10 original articles · won praise 1 · views 1245

Guess you like

Origin blog.csdn.net/weixin_43671182/article/details/94336244