C Tutorial
This C program gets name and age as user inputs and checks whether person is eligible to vote or not using if else statement.
Checks whether age is greater than or equal or not.
#include<stdio.h> void main() { char name[30]; int age, wait_time; printf("Enter name: "); scanf("%s", name); printf("\nEnter your age: "); scanf("%d", &age); if(age>=18) { printf("\n%s: you are eligible to vote!", name); } else { wait_time = 18-age; printf("%s: you are not eligible to vote and need to wait for %d years!", name, wait_time); } }Output:
$ cc vote-eligibility.c $ ./a.out Enter name: person1 Enter your age: 45 person1: you are eligible to vote! $ ./a.out Enter name: person2 Enter your age: 13 person2: you are not eligible to vote and need to wait for 5 years![
Below scenario is one of the reason to get this error.
printf("%s: you are not eligible to vote and need to wait for %d years!", wait_time);
consider above printf statement, will provide the error segmentation fault error if you misses one of the argument name string.
Segmentation fault (core dumped)
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page