# Thread: New to programming, Segmentation Fault?

1. ## New to programming, Segmentation Fault?

I've been trying to look elsewhere online, but the code I see is all very complicated compared to my knowledge level. I'm trying to create a program that does exactly this and I keep getting a "Segmentation fault" error after it asks for the two integers.

Please enter two integers: -1 3

Description Data
----------- ----
Input 1 -1
Input 2 3
Sum 2
Half of Input 1 -0.5
Half of Input 2 1.5
Quotient 0

Here's my code as it is currently, I know it's messed up, but I literally started last week and my teacher isn't helping. What all is causing this segmentation fault?

Code:
```#include <stdio.h>

int main (void)
{
// create a and b variables aka local declarations
int a;
int b;
int Sum;
int Half_of_Input_1;
int Half_of_Input_2;
int Quotient;

// statements
printf("\nName: Herp Derp\n");

scanf("%d %d, &a, &b");
scanf("%d %d %d %d, &Sum, &Half_of_Input_1, &Half_of_Input_2, &Quotient");
//inputting of data

Sum = a + b;
Half_of_Input_1 = a / 2;
Half_of_Input_2 = b / 2;
Quotient = a / b;

// chart data
printf("%20c%20c", "Description Data");
printf("%20c%20c", "___________ ____");
printf("%20c%20d", "Input 1", a);
printf("%20c%20d", "Input 2", b);
printf("%20c%20d", "Sum", Sum);
printf("%20c%19.1f", "Half of Input 1", Half_of_Input_1);
printf("%20c%19.1f", "Half of Input 2", Half_of_Input_2);
printf("%20c%20d", "Quotient", Quotient);

return 0;
}```

2. Compiling with warnings turned up:
Code:
```new.c:17: warning: too few arguments for format
new.c:18: warning: too few arguments for format
new.c:27: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
new.c:27: warning: too few arguments for format
new.c:28: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
new.c:28: warning: too few arguments for format
new.c:29: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
new.c:30: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
new.c:31: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
new.c:32: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
new.c:32: warning: format ‘%19.1f’ expects type ‘double’, but argument 3 has type ‘int’
new.c:33: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
new.c:33: warning: format ‘%19.1f’ expects type ‘double’, but argument 3 has type ‘int’
new.c:34: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’```
scanf is used like this:
Code:
`scanf("%d", &intVar);`

3. Green -> correct
Red -> wrong

Originally Posted by dc210
Code:
```#include <stdio.h>
int main (void){
// create a and b variables aka local declarations
int a;
int b;
int Sum;
int Half_of_Input_1;
int Half_of_Input_2;
int Quotient;

// statements
printf("\nName: Herp Derp\n");

scanf("%d %d", &a, &b");
scanf("%d %d %d %d", &Sum, &Half_of_Input_1, &Half_of_Input_2, &Quotient");
//inputting of data

Sum = a + b;
Half_of_Input_1 = a / 2;
Half_of_Input_2 = b / 2;
Quotient = a / b;

// chart data
printf("%20c%20c", "Description Data");
printf("%20c%20c", "___________ ____");
printf("%20c%20d", "Input 1", a);
printf("%20c%20d", "Input 2", b);
printf("%20c%20d", "Sum", Sum);
printf("%20c%19.1f", "Half of Input 1", Half_of_Input_1);
printf("%20c%19.1f", "Half of Input 2", Half_of_Input_2);
printf("%20c%20d", "Quotient", Quotient);

return 0;
}```
try this:

Code:
```#include <stdio.h>

int main (void)
{
// create a and b variables aka local declarations
int a;
int b;
int Sum;
int Half_of_Input_1;
int Half_of_Input_2;
int Quotient;

// statements
printf("\nName: Herp Derp\n");

scanf("%d %d", &a, &b);
scanf("%d %d %d %d", &Sum, &Half_of_Input_1, &Half_of_Input_2, &Quotient);
//inputting of data

Sum = a + b;
Half_of_Input_1 = a / 2;
Half_of_Input_2 = b / 2;
Quotient = a / b;

// chart data
printf("%20s", "Description Data");
printf("%20s", "___________ ____");
printf("%20s %20d", "Input 1", a);
printf("%20s %20d", "Input 2", b);
printf("%20s %20d", "Sum", Sum);
printf("%20s %19.1d", "Half of Input 1", Half_of_Input_1);
printf("%20s %19.1d", "Half of Input 2", Half_of_Input_2);
printf("%20s %20d", "Quotient", Quotient);

return 0;
}```

4. Thank you very much for the help guys. It's running properly, but it's misaligned a lot. I don't really know how to do field width properly yet.