本文介绍了如何在存储阵列上MPLAB一个变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code适用于$ C编译器的cblock但MPLAB C18编译器我没有得到相同的结果很好。我使用的PIC18单片机。

The following code works fine on CodeBlocks compiler but on MPLAB C18 compiler I don't get the same results. I am using PIC18 microcontroller.

code

    int d[6];
    int all;

    d[0] = 6;
    d[1] = 4;
    d[2] = 8;
    d[3] = 0;
    d[4] = 0;

    all =  10000*d[0] + 1000*d[1] + 100*d[2] + 10*d[3] +  d[4];
    printf("%d", all);

在$ C $个cblocks 输出:64800

Output on CodeBlocks: 64800

在MPLAB 输出:-816

Output on MPLAB: -816

究竟是什么问题?应该不是这个code做工精细?谢谢!

What is exactly the problem? Shouldn't this code work fine? Thanks!

推荐答案

这是有类型的对象 INT 不能保证能够存储超过<$ C值$ C> -32767 或 32767 。您code :: Blocks的执行情况来扩展此范围,但你的MPLAB C18实现(这是不是一个真正的有效的C实现,顺便说一下)没有。你看到的在MPLAB实现是不确定的行为,由于签署整数溢出。使用无符号类型和/或更宽的类型,如很长很长。不要忘了相应修改printf格式说明。 %U unsigned int类型%LD %禄 无符号长%LLD 长长,等...

Objects that have the type int aren't guaranteed to be able to store values beyond -32767 or 32767. Your Code::Blocks implementation happens to extend this range, but your MPLAB C18 implementation (which isn't really a valid C implementation, by the way) doesn't. What you're seeing in your MPLAB implementation is undefined behaviour due to signed integer overflow. Use an unsigned type, and/or a wider type, such as long or long long. Don't forget to modify your printf format specifier accordingly. %u for unsigned int, %ld for long, %lu for unsigned long, %lld for long long, etc...

这篇关于如何在存储阵列上MPLAB一个变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:18