0

Who to check if an input is left blank

For example: int i; scanf("%d", &i); This will inquire user for an input. If this is left blank, this will generate error if used in any function. So how can i check if input is blank? (So that I can replace that with my error handler)

22nd May 2020, 6:35 AM
Samiul Nasif Tayem
Samiul Nasif Tayem - avatar
3 Respuestas
+ 1
When you use scanf but user does not input anything then scanf will return a negative value. So you use use if statement to check if there was any input or not. Example: #include <stdio.h> int main() { int foo; if (scanf("%d", &foo) > 0) { printf("Input = %d", foo); } else { printf("No input"); } return 0; }
22nd May 2020, 6:52 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
🔫 Rick Grimes does the same apply for 'char' value? Like: char test_char;
22nd May 2020, 7:30 AM
Samiul Nasif Tayem
Samiul Nasif Tayem - avatar