我使用repl.it來編寫C語句,但是當我運行它時,系統會跳過if語句中的第二個scanf。scanf()在if語句中不起作用
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (void)
{
char services[40];
loop: printf ("I can help you to do somethings(fibonacci number, pi,
x^y and exit)\n");
scanf ("%s", &services);
if (strncmp(servies, "fibonacci number"))
{
float n, first = 0, second = 1, terms = 1;
printf ("please enter the terms:\n");
scanf ("%f", &n);
printf ("fibonacci number terms golden
ratio\n");
while (terms <= n)
{
terms = ++terms;
printf ("%f%35f%10f\n", first, terms, first/second);
terms = ++terms;
printf ("%f%35f%10f\n",second, terms, first/second);
first = first + second;
second = first + second;
goto loop;
}
}
}
什麼問題?
'scanf(「%s」,&services);' - >'scanf(「%s」,services);' –
您應該得到'strncmp'行的編譯錯誤,注意編譯器輸出 –
'terms = ++ terms;'導致未定義的行爲,我想你的意思是'terms = terms + 1;' –