-3

Create a function that displays ’N’ or ’P’ depending on the integer’s sign entered as a parameter. If n is negative, display ’N’. If n is positive or null, display ’P’.

• Here’s how it should be prototyped :

void ft_is_negative(int n);

This is what I did ;

int main()
{
void    ft_is_negative(int n);
int n;
{
    if (n < 0)
    {
        write(1, "N", 1);
    }
    else
    {
        write(1, "P", 1);
    }
}

return 0;
}     

So this only prints P for me. I would like it to ask for the parameter and then print the result.

mghayas
  • 1
  • 1
  • 1
    Where is your function definition? You only repeat the declaration and then have a bunch of code in your `main` function – UnholySheep Feb 08 '21 at 16:14
  • 1
    Welcome to StackOverflow! Please read [ask]. In particular, it's very important to actually ask a question! You can normally identify a question by the presence of a question mark `?`. In your case, I can't tell what you're actually trying to ask! Please [edit] your question to include a... question. – canton7 Feb 08 '21 at 16:14
  • *If n is positive or null* – `n` is not a pointer that it could be null. – Rohan Bari Feb 08 '21 at 16:14
  • @canton7: I guess "how can i get solve the following" in the title is meant to be the question... However, since the poster seems to lack the very basics of how to even *declare* a function in C, I'm not sure we can help them in a meaningful way. – Heinzi Feb 08 '21 at 16:14
  • 1
    Yeah, I'm *guessing* the problem is that the code doesn't compile, because it won't, but it would be nice if they actually said what the problem was – canton7 Feb 08 '21 at 16:16
  • 1
    From reading your code, it looks like you're lacking some fundamental knowledge about C. I recommend starting with the course notes your teacher has given you so far, and asking them or your peers if you have questions. You need to know how to write a function before you'll be able to complete this task – canton7 Feb 08 '21 at 16:22
  • @RohanBari I suspect they meant "positive or zero", and it didn't translate correctly. – Barmar Feb 08 '21 at 16:46

2 Answers2

1

Your code compiles (which is quite surprising, to be honest).

What you have right now is simply code that is executed inside the main function. int n is an uninitialized variable and you cannot know its value (apparently, it seems to be >=0 when you tried). In other words: the program currently has undefined behavior.

What you need to do: you need to declare and define a function which writes the required output. Once you have that, you need to call that function from your main function.

A function's signature follows the form: return type + function name + function parameters, e.g. int fun(int num). A function needs a body too, which defines what this function does when called.

A very simple example:

/* define the function "square", which takes a single parameter and returns the squared value */
int square(int num) {
  return num * num;
}

int main(void) {
  /* call the function: */
  int squared = square(5);
  /* squared now contains the value 25. you can use it in other calculations or function calls, such as `printf` */
  return 0;
}

With that, you should have the necessary information to rewrite your program to define a function, call the function and then write the function's output.

knittl
  • 246,190
  • 53
  • 318
  • 364
0
  1. ... So this only prints P for me

    n is used uninitialized, that seems UB.

  2. This line:

    void    ft_is_negative(int n);
    

    does nothing because of having no code contained in it, it's just a declaration.

  3. Rest of the code does nothing whatever you're expecting from them.


Solution: Declare and define the function correctly and use it:

#include <stdio.h>
#include <unistd.h>

void ft_is_negative(int n) {
    // only write function
    if (write(1, (n >= 0) ? "P" : "N", 1) == -1) {
        // write failed
    }
}

int main(void) {
    int n = 10;

    ft_is_negative(n); // P --- when -X -> N
    puts(""); // newline

    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34