Does anyone know why I am getting a segfault when I run this code? Valgrind tells me that I have "uninitialized value of size 4" on line 13 if( !isdigit(x) ) and an invalid read size 2 on the same line -- address is not stack'd, malloc'd, or free'd.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int main()
{
int x;
printf("Please enter a number: ");
scanf("%d", &x);
if( !isdigit(x) )
{
printf("You entered %d\n", x);
}
else
{
printf("You did not enter a valid number!\n");
}
return 0;
}
isdigitis meant to be used against character values; it will return true for an input of'1'and false for an input of'a', for example. What are you entering for your input? - John Bodeisdigit()isn't going to work in the way you are hoping if you use using%d. If you enter1, for instance,xwill not contain the ASCII code for the character1, soisdigit()will return false. - Crowmanscanf(...) == 1? - Deduplicator