When I use this compiler command:
gcc ../main.c -w -o sc
And this code:
/* sc - a simple calculator */
/* glibc */
#include <stdio.h>
#include <stdlib.h>
/* global variables */
int ans;
void help()
{
printf("Type in an equation (2 + 2) and it will solve it. (4)\n");
printf("-- remember spaces between characters.\n");
}
void error()
{
printf("That value is not accepted.\n");
printf("---------------------------\n");
help();
}
int main(int argc, char *argv[])
{
if(strcmp(argv[1], "help") == 0) {
help();
} else if(argc == 4) {
if( /* Removed calculator function */ (argc, argv) != 0) {
error();
return 2;
}
} else {
error();
return 1;
}
return 0;
}
When I run the binary without any arguments this is the output:
Segmentation fault (core dumped)
Why does it fail when it falls to the else and not when it has arguments?