Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What's the difference between integer, floating point and character?
What's the difference between integer, floating point and character?
1. Integer data type

C defines five integer data types.

Integer data type table

The serial number type name represents a range of bytes.

1 signed char signed single byte integer type1-128 ~+127

2 short int short integer 2 -32768~+32767

3 int integer 4-2147438648 ~+214748647

4 long int Long integer 4-2147438648 ~+214748647

5 long long int long integer 8-922337203685475808 ~+-9223372545

Examples output the number of bytes of various integer types.

# Including

int main(void) {

Printf("sizeof (signed character) = %d/n ",sizeof (signed character));

printf("sizeof(short int) = %d/n ",sizeof(short int)); /* the results of sizeof are all int types */

printf("sizeof(int) = %d/n ",sizeof(int));

printf("sizeof(long int) = %d/n ",sizeof(long int));

printf(" sizeof(long long int)= % d/n ",sizeof(long long int));

Returns 0;

}

Compile and run the results

[root @ localhost CCC]# gcc c 15 . c

[root@localhost ccc]#。 /a.out

Sizeof (signed character) = 1

sizeof(short int) = 2

sizeof(int) = 4

sizeof(long int) = 4

sizeof(long long int) = 8

Program description: sizeof is a byte operator, usage, sizeof (data type). Sizeof is used to get the number of bytes occupied by the data type. Our running environment is Redhat 5 Linux, and the compiler is GCC.

2. Unsigned integer type

Corresponds to signed and unsigned integer types.

Unsigned integer type table

Number, type, name, number of bytes, value range

1 unsigned characters 1 0~255

2 Cancel short integer 2 0~65535

3 unsigned integer 4 0~4294967295

4 unsigned long integer 4 0~4294967295

5 unsign long long int 8 0 ~ 1844674407370955 16 15

The example outputs the number of bytes of various unsigned integer types.

# Including

int main(void) {

Printf("sizeof (unsigned character) = %d/n ",sizeof (unsigned character));

Printf("sizeof (unsigned short integer) = %d/n ",sizeof (unsigned short integer)); /* the results of sizeof are all int types */

Printf("sizeof (unsigned int) = %d/n ",sizeof (unsigned int));

Printf("sizeof (unsigned long integer) = %d/n ",sizeof (unsigned long integer));

Printf("sizeof (unsigned long integer) = %d/n ",sizeof (unsigned long integer));

Returns 0;

}

Compile and run the results

[root @ localhost CCC]# gcc c 16 . c

[root@localhost ccc]#。 /a.out

Sizeof (unsigned character) = 1

Sizeof (unsigned short integer) = 2

Sizeof (unsigned integer) = 4

Sizeof (unsigned long integer) = 4

Sizeof (unsigned long integer) = 8

3. Integer constants

Integer constants refer to constants used to represent integer values, which are divided into short int, integer (int), long int and long long int. C default integer (int). Various types of decimal tables of integer constants (suffixes are case-insensitive)

Serial number data type octal decimal hexadecimal

1 integer 0 1 12 74 0x4a

2 Long integer (l) 0 1 12l 74l 0x4al

3 long integer (ll) 0112ll74ll0x4all

4 unsigned integer (u) 0 1 12u 74u 0x4au

5 unsigned long integer (ul) 0112ul74ul0x4aul

6 unsigned long integer (ull) 0112ull74ull0x4aull

4. Character data type

There is only one kind of character data in C language, namely char data. Char is also directly called character type. Character types occupy the least memory space, usually one byte. Integers stored in char type variables can be represented as signed or unsigned values, depending on the compiler.

Byte length of sample character data type

# Including

int main(void) {

printf("sizeof(char) = %d/n ",sizeof(char));

Printf("sizeof (signed character) = %d/n ",sizeof (signed character));

Printf("sizeof (unsigned character) = %d/n ",sizeof (unsigned character));

Returns 0;

}

Compile and run the results

[root @ localhost CCC]# gcc c 17 . c

[root@localhost ccc]#。 /a.out

sizeof(char) = 1

Sizeof (signed character) = 1

Sizeof (unsigned character) = 1

5. Character variables

Character variables are variables used to store character values. Character variables are also divided into signed and unsigned.

Grammatical structure:

[signature] charch1;

[unsigned] charch2;

example

# Including

int main(void) {

char ch 1 = '/n ';

Unsigned character CH2 = '/t ';;

char ch3 = 48

printf("ch 1 = [%c],[%d]/n ",ch 1,ch 1);

printf("ch2 = [%c],[%d]/n ",ch2,CH2);

printf("ch3 = [%c],[%d]/n ",ch3,CH3);

Returns 0;

}

Compile and run the results

[root @ localhost CCC]# gcc c 18 . c

[root@localhost ccc]#。 /a.out

ch 1 = [

], [ 10]

ch2 = [ ],[9]

ch3 = [0]、[48]

Program Description: The escape character' /n' is a line break. In the first printf function, %c will be replaced with' /n', and the output will be replaced with line breaks. We can see that the line break appears in the middle of the first square bracket of the output. %d is the ASCII value of c 1. The escape character' /t' is a horizontal tab, and the %c in the second printf will be replaced by' /t' and output to the terminal. The ASCII code corresponding to the value 48 is 0, so the output corresponding to the terminal is 0.

6. Floating-point data types

C language defines three floating-point data types:

Floating point, single precision

double-precision

Long double precision

There are different regulations for different types of floating-point numbers in the C standard, and the byte length is also different under different compilers or different hardware conditions.

Test three kinds of floating-point byte lengths with an example.

# Including

int main(void) {

printf("sizeof(float) = %d/n ",sizeof(float));

printf("sizeof(double) = %d/n ",sizeof(double));

printf(" sizeof(long double)= % d/n ",sizeof(long double));

Returns 0;

}

Compile and run the results

[root @ localhost CCC]# gcc c 19 . c

[root@localhost ccc]#。 /a.out

sizeof(float) = 4

sizeof(double) = 8

sizeof(long double) = 12

Table of floating-point byte length, precision, magnitude range and I/O format

Serial number data type byte length precision order of magnitude range printf and scanf formats

1 floating (f) 4 7 -38~38 %f

2 double eights is about16-308 ~ 308% f.

3 long double (1) 12 or so 19 -4932~4932 %llf.

7. Floating point precision

Floating-point precision is ranked as float, double and long long double from low to high.

Example Checking Floating Point Precision

# Including

int main(void) {

float f = 0.98765432 10 1234567890 12f; /* You may not use the f suffix, but warning */

Double d = 0.9876543210123456789012;

Long double LD = 0.9876543210123456789012l; /* suffix l or L */

printf("f/t= %.25f/n ",f);

printf("d/t= %.25lf/n ",d);

printf(" LD/t = % . 25 lf/n ",LD);

Returns 0;

}

Compile and run the results

[root@localhost ccc]# gcc c20.c

[root@localhost ccc]#。 /a.out

f = 0.98765432834625244 14062500

d = 0.98765432 10 123456262954

LD = 0.98765432 10 1234567892 17 150

8. Floating point storage method

Floating-point values are stored in memory in the form of scientific notation. The floating-point memory form contains three parts:

1) sign bit

Sign bit The sign bit of floating-point type has only one bit, which is the highest bit. This bit is 1, which means negative number, and this bit is 0, which is not negative.

2) Exponential bit

Floating-point exponent bits are stored in the form of complement, which is the exponent part of scientific counting method.

3) Cardinality

Cardinal digit is the last digit of floating-point type, which determines the precision of numerical value.

Floating-point storage segment table

Serial number data type sign bit exponent bit radix bit deviation value

1 floating-point 1 8 23 127

2 pairs111521023

3 long double1156416383

Examples of detecting the storage status of float, double and long double.

# Including

int main(void) {

float fone = 2.0

float ftwo = 2.5

Double done = 2.0

double dtwo = 2.5

long double ldone = 2.0

Long double ldtwo = 2.5

/* Output the storage content of floating-point data in memory */

printf(" float(2.0)= % 08x/n " ,*(int *)(& amp; fone));

printf(" float(2.5)= % 08x/n " ,*(int *)(& amp; ftwo));

/* Output the storage content of double-precision data in memory */

printf(" double(2.0)= % 0 16 llx/n " ,*(long long *)(& amp; done));

printf(" double(2.5)= % 0 16 llx/n " ,*(long long *)(& amp; dtwo));

/* Output the storage content of long double-precision data in memory */

printf(" londou(2.0)= % 08x % 08x % 08x/n ",

*(((int *)(& amp; ldone)) + 2),

*(((int *)(& amp; ldone)) + 1),

*(((int *)(& amp; ldone)))));

printf(" londou(2.5)= % 08x % 08x % 08x/n ",

*(((int *)(& amp; ldtwo)) + 2),

*(((int *)(& amp; ldtwo)) + 1),

*(((int *)(& amp; LD two)))));

Returns 0;

}

Compile and run the results

[root @ localhost CCC]# gcc c 2 1 . c

[root@localhost ccc]#。 /a.out

float (2.0) = 40000000

float (2.5) = 40200000

double(2.0) = 400000000000000

double(2.5) = 400400000000000

londou(2.0)= 00004000 800000000000000

londou(2.5)= 00004000 a 000000000000