#include <stdio.h>
[Result Screenshot Attached ]
int main(){
int a;
printf("Enter a number %d\n", a);
scanf("%d", &a);
(a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");
return 0;
}
結果也如下:-
PS D:\學習C語言> cd "d:\學習C語言" ; if ($?) { gcc ternary.c -o 三元 } ; if ($?) { .\ternary } 輸入一個數字 12914124
uj5u.com熱心網友回復:
在接受輸入之前,您已經列印了“a”,因此它正在拋出垃圾值。試試這個代碼:
#include <stdio.h>
int main(){
int a;
printf("Enter a number :");
scanf("%d", &a);
(a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");
return 0;
}
uj5u.com熱心網友回復:
你的代碼 -
#include <stdio.h>
int main() {
int a; // <-- Here, You are not assigned any value, so compiler assign garbage integer value to the a. In your case it is 12914124
printf("Enter a number %d\n", a); // <-- Here you are printing that value, so compiler give output as "Enter a number 12914124"
// That's why you are getting the random number.
scanf("%d", &a);
(a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");
return 0;
}
更正的代碼 -
#include <stdio.h>
int main() {
int a;
printf("Enter a number "); // <-- Remove '%d\n' and 'a' in here
scanf("%d", &a);
(a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/361327.html
標籤:C
