Jonathan
Jonathan

Reputation: 53

How can I call with a int function in main function?

I have a requirement to insert something in the function main > my_isneg to call my_isneg function. How can I do it?

#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();
}

Upvotes: 0

Views: 208

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50774

It's somewhat unclear what you're asking, but maybe you want this:

...
// 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);
}

Output

NPP

Or maybe this, which matches the name my_isneg more closely:

...
// 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');
}

Output

NP

Upvotes: 3

Related Questions