0

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:

  1. Modifying a string literal
  2. Accessing an address that is freed
  3. Accessing out-of-array index bounds
  4. Improper use of scanf()
  5. Stack Overflow
  6. 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;
}
  • 2
    Your output should be included as text rather than an image or especially a linked image. – Chris Jun 11 '23 at 06:19
  • 3
    String comparisons cannot be performed with `==` in C. Instead use `strcmp`. – Chris Jun 11 '23 at 06:20
  • 1
    You should start by fixing your warnings. Things like `scanf("%s",&name);` are incorrect. For a string, which has an array type, it should be `scanf("%s",name);` This will pass a `char *`, as expected, rather than the `char (*)[50]` that you're passing. This probably won't cause an observable bug, but it's a pointer type error that should not be present in your code. – Tom Karzes Jun 11 '23 at 06:27
  • The comparison `category=="E1"` will always be false. Hint: You're comparing two different pointer values for equality, so of course the result will be false. To compare strings, use `strcmp`. – Tom Karzes Jun 11 '23 at 06:29
  • 1
    Finally, here's the cause of your segmentation fault: `scanf("%c \n",proceed);` should obviously be `scanf("%c \n",&proceed);` Again, enable warnings, and *fix them*. Do that *before* even attempting to run your code. – Tom Karzes Jun 11 '23 at 06:29
  • Actually, `scanf("%c \n",proceed);` should be `scanf(" %c",proceed);` Note space before % – stark Jun 11 '23 at 10:36
  • @stark That's a different issue, and your "fix" will still cause a segmentation violation. You can't pass an integer value to `scanf` where a `char *` is required. Try it and you'll see. – Tom Karzes Jun 11 '23 at 12:15
  • Thanks @Chris and @TomKarzes. I've been reviewed and fixed my code and it work. Thanks for the guidance. I'm lil confuse about the use `strcmp`. My intend is if user enter different category such as E2 the calculation will be different. Is it suitable to use `strcmp` – the noobies Jun 11 '23 at 12:36
  • @thenoobies To compare strings, use `strcmp`. To compare pointer values, use `==` and `!=`. The former looks at the string lengths and characters. The latter only looks at the memory addresses of the starts of the strings. – Tom Karzes Jun 12 '23 at 01:16

0 Answers0