簡體   English   中英

如何在主函數中使用int函數進行調用?

[英]How can I call with a int function in main function?

我需要在函數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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM