c - 不理解运行温度转换程序后收到的消息

标签 c xcode

我是 C 语言的新手,正在尝试使用 Xcode 编写一个程序,将华氏温度转换为摄氏温度,反之亦然。到目前为止,我的代码如下。

#include <stdio.h>
#include "hw2.h"

void convert_temp(int degree1, char scale1, int* degree2, char* scale2){

    if (scale1 == 'F') {
        *degree2 = ((degree1 - 32) * 5) / 9;
        *scale2 = 'C';
    }
    else {

        *degree2 = ((degree1 * 9) / 5) + 32;
        *scale2 = 'F';
    }
}

int main() {
    int degree1, degree2;
    char scale1, scale2;
    printf("Enter a temperature and a scale\n");
    scanf("%d %c", &degree1, &scale1);
    convert_temp(degree1, scale1, &degree2, &scale2);
    printf("%d %c = %d %c\n", degree1, scale1, degree2, scale2);
    return 0;
}

这是一个正确的输入/输出示例:

Enter a temperature and a scale
32 F
32 F = 0 C

但是,当我运行代码时,这是我得到的:

Enter a temperature and a scale
32 F
hw2 was compiled with optimization - stepping may behave oddly; variables may not be available.
(lldb) 

我无法理解我得到的输出。谁能告诉我为什么我的输出中没有 32 F = 0 C?我的代码中的所有内容对我来说都很好。

最佳答案

假设 hw2 是您的程序的名称,然后调试器提示它是在打开优化的情况下编译的,这在开发过程中是不正常的,因为优化器会做各种聪明的事情让程序运行得更快。

您需要在 Xcode 中执行以下操作:

  • 确保您正在使用 Debug 构建配置进行调试(检查您的方案)。
  • 确保您没有为Debug 构建配置打开优化(检查您的build设置)。

关于c - 不理解运行温度转换程序后收到的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36599700/

相关文章:

c# - 通过互操作接收字符串

c - fork 调用不执行子代码

ios - 类型 "ViewController"的 Swift 值没有名为 "Self"的成员错误

xcode - 如何将标签Core Plot向右移动?

ios - Xcode 7.1 "iTunes store operation failed you are not authorized to use this service"

C 转换为华氏度 转换为摄氏度

c - 十六进制溢出但变量不溢出

c - 传递不带类型的函数参数,然后声明指向同名变量的指针

IOS9和uiwebview问题?

swift - 单击选项卡栏图标时重置 ViewController