我想在ANSI C的代码中声明一个函数.我可以在main()中使用它后定义函数吗?例如,我写的绝对值函数:谁能告诉我它是否合理?
#include <stdio.h>
int main()
{
double value1 = -5;
printf("%lf",
abs(value1));
}
double abs(double number)
{
if (number < 0) {
(number * (-1) = number);
}
return number;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个可以执行各种算术功能的基本计算器,从添加开始!现在我已经得到了它的基本逻辑,但我不确定如何获取两个输入并打印出来!
#include <stdio.h>
int main()
{
char mychar;
int a;
int op1;
int op2;
printf("Welcome to Andrew Hu's calculator program!\n"); //Greeting
while(1)
{ printf("Enter a mathematical operation to perform:\n");
scanf("%c", &mychar);
if(mychar == '+') //Valid Operators
a = 1;
else
a = 0;
if(a == 0) //Operator Checker, error if invalid
printf("\nError, not a valid operator\n");
else if(a == 1){
printf("%c\n", &mychar),
printf("Enter OP1:\n"),
/* not sure what to put here to echo the character as a decimal*/
printf("Enter OP2:\n"), …Run Code Online (Sandbox Code Playgroud) 我想在ANSI C中创建一个提示用户输入的代码.首先,我想打印提示.然后,我想回显提示和循环,仅在键入和回显字符时.
我知道要创建一个无限循环,我必须将while()的条件设置为始终为true的东西,就像while(1)
但是,当我测试我的代码时,我似乎一遍又一遍地循环打印"输入一个字符".有关C新手的任何提示吗?
#include <stdio.h>
int main()
{
char mychar;
while(1)
printf("Enter a characater:\n");
scanf("%c", &mychar);
printf("%c", &mychar)
}
Run Code Online (Sandbox Code Playgroud) c ×3
addition ×1
calculator ×1
debugging ×1
declaration ×1
function ×1
loops ×1
printf ×1
prompt ×1