Data loss problems with type conversion in C language

Source: Internet
Author: User
Tags integer printf

When a longer integer is converted to a shorter integer or char type, the excess high portion is discarded.

Program One:

#include <stdio.h>     
int main ()     
{     
    int i;     
    char c;     

    i=65535;     
    c=48;     

    I=c;
    c=i;

    printf ("char=%c\n", c);     
    printf ("int=%d", I);     
    GetChar ();     
}

The output is:

Program Two:

#include <stdio.h>     
int main ()     
{     
    int i;     
    char c;     

    i=65535;     
    c=48;     

    c=i;     
    I=c;     
    printf ("char=%c\n", c);     
    printf ("int=%d", I);     
    GetChar ();     
}

The output is:

Characters are not visible because of c=-1

When a lower type of data is converted to a higher type, it is generally only a form change without affecting the substance of the data, and some data may be lost when the higher type of data is converted to a lower type.

This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1153598

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.