Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me.

For example this code

#include <stdio.h>
main()
{
   unsigned long a=0;
   unsigned long b=0;
   int z;
    {
    printf("Enter two digits you want to add seperated by a space:");
    scanf("%u %u", a, b);
   }



z= a + b;
printf("\n%u\n", z);


}

Its because I forgot the &'s! Okay I got it nevermind :slight_smile:

---------- Post updated at 11:39 AM ---------- Previous update was at 10:33 AM ----------

#include <stdio.h>

main()
{
   unsigned long c=0;
   unsigned long a=0;
   unsigned long b=0;
   unsigned long z;
   unsigned long noBits = 32;
   unsigned long x = (1 << noBits - 1);
   unsigned long y = 129;
   int i;
   unsigned long k;
   unsigned long t;
   unsigned long g;
   {
    printf("Please choose a function\n Calc (1)\n Binary convertor (2)\n");
    scanf("%u", &c);
     {
    if (c == 1)
       {
        {printf("\n 1 for addition\n 2 for subtraction\n 3 for multiplication\n 4 for division\n");
        scanf("%u", &g);

          printf("\nEnter two digits seperated by a space:");
           scanf("%u %u", &a, &b);


       if (g==1)
            {z= a + b;
printf("\n%u\n", z);}
       if (g==2)
            {z=a - b;
printf("\n%u\n", z);}
       if (g==3)
            {(z= a * b);
printf("\n%u\n", z);}
       if (g==4)
            {(z= a / b);
printf("\n%u\n", z);}

                printf("\n%u\n", z);
        }
       }
       
    if (c == 2)
       {printf("Enter any whole number:");
        scanf("%d", &y);
        for (i=noBits - 1; i >= 0; i--)
  {
    printf("i = %d  ", i);
    x = 1 << i;
    k = x & y;
    if (k != 0){
      printf("binary = 1\n");
    } else {
      printf("binary = 0\n");
    }
                
  }
     }


   }
}
}

why doesnt the calculator work!

Please be more specific about 'what does not work'. There several issues in your code. Be sure to turn on warnings - example, using gcc:

gcc -Wall myprog.c -o myprog

This will help you. Fix what the compiler does not like. We all have to do this.

1 Like

Apologies.
The code is running for me and I do have my warnings turned on etc;
First it asks you to choose from a calculator and Binary convertor,
You either enter "1" (for calc) and "2" (for binary convertor) and press Enter.
If you choose CALC It then allows you to choose from
Addition, subtraction, multiplication, Division, and then you have to enter two digits separated by a space.Everything upto there but then it also gives me a '0' as an answer. How ever if i get rid of all of my code leaving just the addition part, it works perfectly fine.

Thankyou