[英]Compilation error “s is undeclared,” but it is the result of `scanf`
this is my code it is for calculating resistors in series or parallel the error is on line 12 这是我的代码,用于计算串联或并联电阻,错误在第12行
if ( sp == s )
full code in case of earlier mistake 完整代码,以防出现早期错误
#include <stdio.h>
int main ( void ) {
char sp ;
float a, b, resistancep, resistances ;
printf ("Enter s for resistor in series or p for parallel\n") ;
scanf ( " %c", &sp ) ;
printf ("Enter two resistors calculate\n" ) ;
scanf ("%f%f", &a, &b ) ;
resistancep = a * b / ( a + b ) ;
resistances = a + b ;
if ( sp == s ) {
printf ( "The total resistance is%f\n", resistances ) ;
}
else {
printf ( "The total resistance is%f\n", resistancep ) ;
}
I have also tried 我也尝试过
if ( sp == "s" )
and 和
if ( sp, s )
You probably want 你可能想要
if ( sp == 's' )
Just s
is looking for a variable named s, which you haven't declared. 只是
s
在寻找尚未声明的名为s的变量 。
"s"
usually refers to a string , which is an array of characters. "s"
通常是指一个字符串 ,它是一个字符数组。 You are reading a single character off of input, and "s" != 's'
. 您正在从输入中读取单个字符,并且是
"s" != 's'
。 Remember, and array (almost) never equals an element of the array. 请记住,数组(几乎)永远不等于数组的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.