-3

I'm doing a project and I'm just curious to know if it is possible to have a line that says "if something is not entered" and a prompt statement would be followed.
For example,

if(id_ == NULL){printf("John Doe is absent.")}.

Just a curious question because I want to explore C programming a bit more.

Venkata Krishna
  • 1,768
  • 2
  • 14
  • 21

2 Answers2

1

You can do this with scanf (or similar functions: fscanf, sscanf...). Assuming id_ is an int:

if(scanf("%d",&id_)!=1){
      printf("John Doe is absent.");
}

These functions return the number of input items successfully matched and assigned.

see top voted answer here for more info.

Community
  • 1
  • 1
yiabiten
  • 792
  • 8
  • 27
0
#include <stdio.h>

void input_id(int **id){
    int num;
    printf("input id:");
    if(scanf("%d", &num)==1)
        **id = num;
    else
        *id = NULL;
}

int main(void){
    int id;
    int *id_ = &id;

    input_id(&id_);
    if(id_ == NULL){
        printf("John Doe is absent.\n");
    } else {
        printf("id : %d\n", id);
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70