Tutorials Home   >   Programming Basics   >   Input Validation

Input Validation

1. Introduction

Input validation is the process of checking whether the data entered by the user is correct, meaningful, and acceptable before the program uses it. In C programming, users often enter input through the keyboard, and if invalid input is not checked, it can cause wrong results, program crashes, or unexpected behavior.

Therefore, input validation is very important to make programs:

  • Reliable

  • Safe

  • User-friendly


2. Need for Input Validation

Without input validation, a program may:

  • Accept wrong data types

  • Produce incorrect output

  • Enter infinite loops

  • Crash or behave unexpectedly

Examples of Invalid Input

  • Entering letters instead of numbers

  • Entering negative values where only positive values are allowed

  • Entering values outside the allowed range


3. Common Input Methods in C

Input validation is usually applied to data entered using:

  • scanf()

  • getchar()

  • gets() (not recommended)

  • fgets() (recommended)


4. Validating Integer Input

Problem

User should enter a positive integer only.

Example Using scanf() and while Loop

#include <stdio.h>

int main() {
int num;

printf(“Enter a positive number: “);
scanf(“%d”, &num);

while(num <= 0) {
printf(“Invalid input! Enter a positive number: “);
scanf(“%d”, &num);
}

printf(“Valid input: %d”, num);
return 0;
}

Explanation

  • Input is checked using a condition

  • Loop continues until valid input is entered


5. Validating Range of Input

Example

Accept marks between 0 and 100 only.

#include <stdio.h>

int main() {
int marks;

printf(“Enter marks (0–100): “);
scanf(“%d”, &marks);

while(marks < 0 || marks > 100) {
printf(“Invalid marks! Enter again: “);
scanf(“%d”, &marks);
}

printf(“Marks entered: %d”, marks);
return 0;
}


6. Validating Character Input

Example

Accept only Y or N.

#include <stdio.h>

int main() {
char choice;

printf(“Enter Y or N: “);
scanf(” %c”, &choice);

while(choice != ‘Y’ && choice != ‘N’) {
printf(“Invalid input! Enter Y or N: “);
scanf(” %c”, &choice);
}

printf(“Valid choice: %c”, choice);
return 0;
}


7. Validating String Input

Problem

Ensure user enters a non-empty name.

Using fgets()

#include <stdio.h>
#include <string.h>
int main() {
char name[50];printf(“Enter your name: “);
fgets(name, 50, stdin);while(strlen(name) <= 1) {
printf(“Invalid name! Enter again: “);
fgets(name, 50, stdin);
}

printf(“Name entered: %s”, name);
return 0;
}


8. Validating Numeric Input Using fgets() and sscanf()

This method is safer than scanf().

Example

#include <stdio.h>

int main() {
char input[20];
int num;

printf(“Enter a number: “);
fgets(input, sizeof(input), stdin);

while(sscanf(input, “%d”, &num) != 1) {
printf(“Invalid input! Enter a number: “);
fgets(input, sizeof(input), stdin);
}

printf(“Valid number: %d”, num);
return 0;
}


9. Input Validation Using Functions

Input validation can be made reusable using functions.

Example

int isPositive(int n) {
if(n > 0)
return 1;
else
return 0;
}

10. Common Input Validation Techniques

Technique Description
Range checking Checks min and max values
Type checking Ensures correct data type
Loop validation Repeats input until valid
Character validation Checks allowed characters
String length check Prevents empty input

11. Common Errors Without Input Validation

  • Program crashes

  • Wrong calculations

  • Security issues

  • Poor user experience


12. Advantages of Input Validation

  • Prevents invalid data

  • Improves program reliability

  • Avoids runtime errors

  • Makes program user-friendly


13. Real-Life Applications

  • Login systems

  • ATM machines

  • Online forms

  • Student management systems


14. Best Practices for Input Validation

  • Always validate user input

  • Use loops for re-entry

  • Use fgets() instead of gets()

  • Display clear error messages


15. Conclusion

Input validation in C ensures that the program accepts only correct and meaningful data. By validating integers, characters, strings, and ranges, programmers can make programs safer, more reliable, and user-friendly. Input validation is an essential concept for writing error-free and professional C programs.