简体   繁体   English

C - 条件总是跳到'Else'?

[英]C — Conditional always jumping to 'Else'?

I am working on learning C and am using some practice problems from the Python book I recently finished. 我正在研究学习C并且正在使用我最近完成的Python书中的一些练习题。 My C book is in the mail, but I wanted to get a head start. 我的C书是在邮件中,但我想要先行一步。 I was putting together a simple temperature conversion program and for some reason it always jumps to the 'Else' clause in my conditional... I'm sure I'm missing something simple, but I can't seem to figure it out. 我正在整理一个简单的温度转换程序,由于某种原因它总是跳到我的条件中的'Else'条款......我确信我错过了一些简单的东西,但我似乎无法弄明白。 Any ideas?: 有任何想法吗?:

#include<stdio.h>

main()
{
float temp_c, temp_f;
char convert_from[1];

printf("Convert from (c or f): ");
scanf("%c", &convert_from);

if(convert_from == "c")
{
    printf("Enter temperature in Celsius: ");
    scanf("%f", &temp_c);

    temp_f=(1.8*temp_c)+32;

    printf("The temperature in Fahreinheit is: %f \n", temp_f);
}

else if(convert_from == "f")
{
    printf("Enter temperature in Fahreinheit: ");
    scanf("%f", &temp_f);

    temp_c=(temp_f/1.8)-32;

    printf("The temperature in Celsius is: %f \n", temp_c);
}

else
    printf("Invalid choice. \n");

}

If you are comparing characters do this: 如果您要比较字符,请执行以下操作

char convert_from; 

printf("Convert from (c or f): "); 
scanf("%c", &convert_from); 

if (convert_from == 'c') 
{ 

Otherwise you can't perform the comparison with a string literal "c" (note the double quotes) like that. 否则你不能像这样用字符串文字"c" (注意双引号)进行比较。

In the expression: 在表达式中:

if (convert_from == "c")

convert_from array is converted to a pointer to char , so you are basically comparing a pointer to char to another pointer to char . convert_from数组被转换为指向char的指针,因此您基本上将指向char的指针与另一个指向char的指针进行比较。 "c" is a string literal while 'c' is a char (note the use of "" in the first case and of '' in the second case). "c"是字符串文字,而'c'char (注意在第一种情况下使用"" ,在第二种情况下使用'' )。

With a char convert_from[1]; 使用char convert_from[1]; declaration, here would be the correct code: 声明,这里将是正确的代码:

char convert_from[1];
scanf("%c", convert_from);
if (convert_from[0] == 'c')

but it is more natural to directly use a char instead of an array 1 of char : 但它更自然直接使用char代替的阵列1的char

char convert_from;
scanf("%c", &convert_from);
if (convert_from == 'c')

Well two problems here. 这里有两个问题。

First: You want only to read a single character, so declaring 第一:你只想读一个字,所以声明

char convert_from;

instead of an array of chars with size 1 (as char convert_from[1] did). 而不是大小为1的字符数组(如char convert_from[1]所做的那样)。

And secondly you need to actually compare to a single character, so you would need to do 其次,你需要实际比较一个角色,所以你需要做

if (convert_from == 'c') ...

instead of "c", because "foo" is a string in C which in turn is a pointer to a constant character array ( const char * ). 而不是“c”,因为“foo”是C中的一个字符串,而C又是一个指向常量字符数组( const char * )的指针。

Additionally: which compiler did you use? 另外:你使用了哪个编译器? Mine (llvm-gcc 4.2) warned me about the issues. 我(llvm-gcc 4.2)警告我这些问题。 So either your compiler is quite bogus or you will urgently have to pay attention to compiler warnings. 因此,无论您的编译器是伪造的还是您都迫切需要注意编译器警告。 This might be difficult, as warnings are no errors, however, warnings are there for a reason :-) 这可能很难,因为警告没有错误,但是,警告是有原因的:-)

In C, you cannot compare strings using == (what happens if you do is that the memory locations of the strings get compared, which will yield different results in most cases). 在C中,你不能使用==来比较字符串(如果你这样做会发生比较字符串的内存位置,在大多数情况下会产生不同的结果)。

Also scanf("%c", &convert_from); scanf("%c", &convert_from); is wrong. 是错的。 The array itself will already decay into a pointer, so scanf("%c", convert_form); 数组本身已经衰变成指针,所以scanf("%c", convert_form); will suffice. 就足够了。 In this case however, convert_form will not contain something your C library will consider a string (strings are null-terminated in C). 但是,在这种情况下, convert_form将不包含C库将认为是字符串的内容(字符串在C中以空值终止)。 A minimally invasive change for getting your code to work would be changing 让代码工作的微创改变将会发生变化

if (convert_from == "f") [...]

to

if (covert_form[0] == 'f') [...]

(mind the '' instead of the "", which is a character literal, which basically is just a number and can thus be compared using ==). (注意''而不是“”,这是一个字符文字,它基本上只是一个数字,因此可以使用==进行比较)。

A more idiomatic way to do this would be to declare convert_form as char convert_form and then use scanf("%c", &convert_form); 更惯用的方法是将convert_form声明为char convert_form ,然后使用scanf("%c", &convert_form); , which would accomplish the same as the above. ,这将完成与上述相同。

First of all since you are reading only one character at a time define it as 首先,因为您一次只能读取一个字符,所以将其定义为

char convert_from; char convert_from;

next it is not advisable to compare strings directly hence the statement should be 接下来不建议直接比较字符串,因此声明应该是

if(convert_from == 'c')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM