You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Python十进制转二进制代码转为C语言时遇到的字符赋值类型不匹配警告问题

解决十进制转二进制的C代码问题:警告与结果错误

Hey there! Let's break down what's going wrong with your C code and fix it step by step, comparing it to your working Python version.

1. 先搞定assignment to 'char' from 'char *'警告

This warning pops up because you're trying to assign a string literal ("0" or "1", which is a char* pointer pointing to a null-terminated string) to a single char variable (tmp_s). In C, single characters use single quotes ('0', '1'), while double quotes are reserved for strings. Fix that part like this:

if (tmp_i == 0) {
    tmp_s = '0';  // 用单引号表示单个字符
} else {
    tmp_s = '1';
}

2. 去掉多余的floor,用整数除法

In your Python code, math.floor(decimal_number / 2) ensures integer division, but in C, dividing two integers with / already does integer division (it truncates towards zero, which works exactly like floor for positive numbers). Using floor() here is unnecessary and causes issues because it returns a double—assigning that back to an int can lead to unexpected behavior. Replace that line with:

decimal_number /= 2;  // 等价于 decimal_number = decimal_number / 2,纯整数除法

3. 修正二进制位的顺序(核心问题)

This is why your output doesn't match the Python result! Your Python code prepends each new bit to the front of the string (binary_number = str(tmp) + binary_number), so the first remainder you calculate (least significant bit) ends up on the right side of the final string. But your original C code appends each bit to the start of the array, meaning the first remainder becomes the leftmost character—so your binary string is reversed.

Here are two straightforward fixes:

方案一:从数组末尾往前填充(匹配Python逻辑)

#include <stdio.h>

int main() {
    int decimal_number = 4;
    char binary_number[32];  // 用固定大小数组,足够存32位整数的二进制
    int counter = 30;  // 从倒数第二个位置开始,留最后一位放终止符
    
    // 先初始化数组末尾的终止符
    binary_number[31] = '\0';
    
    // 单独处理输入为0的情况,否则循环不会执行
    if (decimal_number == 0) {
        binary_number[counter] = '0';
        counter--;
    }
    
    while (decimal_number > 0) {
        int tmp_i = decimal_number % 2;
        // 直接赋值,简化代码
        binary_number[counter] = (tmp_i == 0) ? '0' : '1';
        decimal_number /= 2;
        counter--;
    }
    
    // 从第一个有效字符开始打印
    printf("%s\n", &binary_number[counter + 1]);
    return 0;
}

方案二:先正向存再反转字符串(更贴近你原代码结构)

#include <stdio.h>
#include <string.h>

int main() {
    int decimal_number = 4;
    char binary_number[32];
    int counter = 0;
    
    // 处理输入为0的情况
    if (decimal_number == 0) {
        binary_number[counter++] = '0';
    }
    
    while (decimal_number > 0) {
        int tmp_i = decimal_number % 2;
        binary_number[counter++] = (tmp_i == 0) ? '0' : '1';
        decimal_number /= 2;
    }
    
    // 添加字符串终止符
    binary_number[counter] = '\0';
    
    // 反转字符串得到正确顺序
    int start = 0;
    int end = counter - 1;
    while (start < end) {
        char temp = binary_number[start];
        binary_number[start] = binary_number[end];
        binary_number[end] = temp;
        start++;
        end--;
    }
    
    printf("%s\n", binary_number);
    return 0;
}

额外小提示

  • 一定要处理decimal_number = 0的情况,不然原代码会输出空字符串,这显然不对。
  • 用固定大小数组(比如char binary_number[32])比变长数组(VLAs)更安全,变长数组不是所有C标准都支持,还可能导致栈溢出。

内容的提问来源于stack exchange,提问作者Michael Hajný

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 13:07:34