gpt4 book ai didi

c - 如何在 main 函数中调用 int 函数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:03:19 26 4
gpt4 key购买 nike

我需要在函数 main > my_isneg 中插入一些内容来调用 my_isneg 函数。我该怎么做?

#include <unistd.h>
void my_putchar (char c)
{
write (1, &c, 1);
}
int my_isneg (int n)
{

if (n < 0) {
my_putchar (78); }
else {
my_putchar (80);
}
}

int main (void)
{
my_isneg();
}

最佳答案

有点不清楚你在问什么,但也许你想要这个:

...
// print 'N' 1 if the number n is strictly negative, print 'P' otherwise
int my_isneg(int n)
{
if (n < 0) {
my_putchar('N'); // use 'N' instead of 80 (it's more readable)
}
else {
my_putchar('P'); // use 'P' instead of 80
}
}

int main(void)
{
my_isneg(-1);
my_isneg(1);
my_isneg(2);
}

输出

NPP

或者可能是这个,它更接近于名称 my_isneg:

...
// return 1 if the number n is strictly negative
int my_isneg(int n)
{
return n < 0;
}

int main(void)
{
if (my_isneg(-1))
my_putchar('N');
else
my_putchar('P');

if (my_isneg(1))
my_putchar('N');
else
my_putchar('P');
}

输出

NP

关于c - 如何在 main 函数中调用 int 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52642615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com