Mandatory type conversion of C language series (i)

Source: Internet
Author: User


#include <stdio.h>


Char CChar; Character variable

short int ishort; Short-Integer variable

int ilnt; Shaping variables

float ffloat=70000; Single-precision floating-point type

Cchar= (char) ffloat; Cast assignment

Ishort= (short) ffloat; //

ilnt= (int) ffloat; //

printf ("The char is:%c\n", CChar); Output character Variable value

printf ("The Long is:%ld\n", ishort); Output short Integer variable value

printf ("The int is:%d\n", ILNT); Output integer Variable Value

printf ("The Float is:%f\n", ffloat); Output floating-point variable value

return 0; The return value is 0


Output Result:

The Char Is:p

The Long is:4464

The int is;70000

The float is 70000.000000

Question one:

The Char Is:p

Why the single-precision floating-point type is converted to a character type when the output is p;

Depending on the structure of the C language, the character-type output can be in characters or ASCLL code, as shown in

The simplest examples are:

Cchar1= ' a ';


printf ("%c/n", cChar1);

printf ("%c/n", cChar2);

Output Result:



Because the ASCLL code of a is 97;


ASCLL a total of 128, when more than 128 people, and start again.

For example, the value in the example is 70000.

70000/128=546 more than 112;

From the ASCLL Code table, the 112th place is p;

So the output is P

Question Two

The Long is:4464

Why is the output 4464 when a single-precision floating point is converted to a short integer?

We know that the interval of the short integer is 32768 to 32767, and the two are subtracted plus 1 equals 65536.

Why add 1 because there is an integer 0



The short integer has a total of 65,536 bits, and when this number is exceeded, it starts again.

We can tell by the title


So the output is 4464.


There is a high level of conversion to low levels, data loss can occur, which is often lost in one interval,

For example, when the floating-point type is converted to a character type, the data is lost according to the ASCLL code interval,

When the floating-point type is converted to a short integer, it is lost according to the data interval of the short integer type.

Mandatory type conversion of C language series (i)

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: 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.