C Input Validation

🛡️ C Input Validation

Input validation ensures that the user enters correct, safe, and expected values.
It prevents:

  • Runtime errors

  • Crashes

  • Unexpected behavior

  • Security issues

Since C does not automatically validate input, programmers must manually check input.


🔹 Why Input Validation Matters?

Without validation:

int age;
scanf("%d", &age);

If user enters "hello" instead of a number → program misbehaves or crashes.


✔ Types of Input Validation in C

Type Example
Type validation Ensure input is numeric, char, etc.
Range validation Check values are within allowed limits
Format validation Validate structure of input (e.g., phone number)
Logical validation Check meaningful value (e.g., age > 0)


1️⃣ Validating Integer Input

#include <stdio.h>

int main() {
int num;

printf(“Enter a number: “);

while (scanf(“%d”, &num) != 1) {
printf(“Invalid input! Enter a number: “);
while (getchar() != ‘\n’); // clears invalid input
}

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

🟢 This ensures the input is numeric.


2️⃣ Range Checking

#include <stdio.h>

int main() {
int age;

printf(“Enter age (1–120): “);

while (scanf(“%d”, &age) != 1 || age < 1 || age > 120) {
printf(“Invalid age! Enter again: “);
while (getchar() != ‘\n’); // clear buffer
}

printf(“Age accepted: %d\n”, age);
return 0;
}


3️⃣ Validating Character Input

#include <stdio.h>

int main() {
char option;

printf(“Enter choice (y/n): “);

while (scanf(” %c”, &option) != 1 || (option != ‘y’ && option != ‘n’)) {
printf(“Invalid choice! Enter y or n: “);
while (getchar() != ‘\n’);
}

printf(“You selected: %c\n”, option);
return 0;
}


4️⃣ Validating String Input (No Numbers Allowed)

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char name[50];
int valid;

do {
valid = 1;
printf(“Enter your name: “);
scanf(“%s”, name);

for (int i = 0; i < strlen(name); i++) {
if (!isalpha(name[i])) {
valid = 0;
printf(“Invalid name! Only letters allowed.\n”);
break;
}
}
} while (!valid);

printf(“Valid name: %s\n”, name);
return 0;
}


5️⃣ Email-like Validation (Simple Format Check)

#include <stdio.h>
#include <string.h>
int isValidEmail(char *email) {
return (strchr(email, ‘@’) && strchr(email, ‘.’));
}

int main() {
char email[100];

printf(“Enter email: “);
scanf(“%s”, email);

if (isValidEmail(email))
printf(“Valid email.\n”);
else
printf(“Invalid email format.\n”);

return 0;
}


🧼 Clearing Input Buffer (getchar())

Important when using scanf() to avoid leftover input:

while (getchar() != '\n');

🧠 Best Practices

✔ Always validate user input before using it
✔ Clear input buffer after invalid entries
✔ Use safer input function (fgets()) for strings
✔ Provide clear error messages
✔ Apply logical and range checks


🏁 Summary Table

Validation Type Example
Type check scanf("%d") validation
Range check Age between 1–120
Logical check Division by zero prevention
Format check Email, phone number

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *