How to declare null values in C

Asked

Viewed 133 times

0

My problem is the next one I’m creating loop while which cannot receive values less than or equal to zero, and would also declare that it cannot receive empty values for the program force the user to enter a number and not the enter with no field in scanf().

  printf("Digite o valor da posicao X:");
    scanf("%d",&x);
    //faz a verificação se o numero é 0 ou < 0
  while (x <= 0){
    printf("0 ou < 0 nao forma um triangulo por favor digite novamente a medida X\n");
    scanf("%d",&x);
  }

When the user just type enter it accepts no data on the console, in case it wanted the system to accept only if a number was typed on the console.

  • 1

    You need to explain better, preferably what code you’re having trouble with. Null is one thing, empty is another, depends on the type of data you’re dealing with. Null is 0, but the scanf() does not deal with anything that can be null. So the question has wrong premises and is confused, needs to specify better so we can help.

  • I made the change.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1

I believe this is what you want. You check whether the input was valid, the function scanf() provide this as a return, just read the documentation. And you can also validate the input of the die with the value you want. All this in a single loop.

#include <stdio.h>

int main(void) {
    int x;
    while (scanf("%d", &x) != 0 && x <= 0) printf("0 ou < 0 nao forma um triangulo por favor digite novamente a medida X\n");
    printf("%d", x);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.