I'm new in C programming, may i get reasons of why it does not print the below code:
printf(" Name: %s \n",name);
printf("IC: %s \n",nic);
printf("Category: %s \n",category);
printf("Total hours: %d \n",totalhours);
printf("Gross: %d \n",gross);
printf("Overtime: %d \n",overtime);
printf("Netpay: %d \n",netpay);
This is the error i got: enter image description here
The error stated it got segmentation fault. I've read on web it may caused by:
- Modifying a string literal
- Accessing an address that is freed
- Accessing out-of-array index bounds
- Improper use of scanf()
- Stack Overflow
- Dereferencing uninitialized pointer
I'm not sure which are the causes. I'm trying change %c to %d in scanf("%c \n",proceed);
it does run my code but didn't print the if-else statement.
Below is my full code:
#include <stdio.h>
int main() {
char proceed='Y';
char name[50];
char nic[50];
char category[50];
int totalhours;
double bal;
double gross;
double overtime;
double netpay;
while(proceed=='Y'){
printf("Name:");
scanf("%s",&name);
printf("IC:");
scanf("%s",&nic);
printf("Category:");
scanf("%s",&category);
printf("Total Hours:");
scanf("%d",&totalhours);
printf("Hello World SDN BHD\n");
printf("=======\n");
if(totalhours<=60 && category=="E1"){
if(totalhours>=40){
bal=totalhours-40;
gross=40*100;
overtime=(1.5*100)*bal;
netpay=gross+overtime;
}
else{
bal=0;
gross=40*100;
overtime=0;
netpay=gross+overtime;
}
printf(" Name: %s \n",name);
printf("IC: %s \n",nic);
printf("Category: %s \n",category);
printf("Total hours: %d \n",totalhours);
printf("Gross: %d \n",gross);
printf("Overtime: %d \n",overtime);
printf("Netpay: %d \n",netpay);
}
printf("Cont?:");
scanf("%c \n",proceed);
}
return 0;
}