我是 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", °ree1, &scale1);
convert_temp(degree1, scale1, °ree2, &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/