Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1...
I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one.
Below are two very condensed snippets of which I have added the results inside the each code section.
IMPORTANT! This will NOT even compile on gcc 2.95.3 for the AMIGA, but WORKS correctly on OSX 10.14.1, gcc 4.2.1.
/* No_error gcc 4.2.1 demo. */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int NUM_1;
int NUM_2;
char CHARACTER;
int main(int argc, char *argv[])
{
/* Conditional BEFORE _variables_! */
if (argc <= 3)
{
printf("ERROR!\n\n");
printf("Not enough arguments!\n");
exit(1);
}
int NUM_1 = strtod(argv[1], NULL);
int NUM_2 = strtod(argv[3], NULL);
CHARACTER = *argv[2];
printf("\n%i, %i, %c\n\n", NUM_1, NUM_2, CHARACTER);
return(0);
}
/* ****************************************
Results OSX 10.14.1, gcc 4.2.1...
Last login: Fri Mar 15 14:45:03 on ttys000
AMIGA:amiga~> cd Desktop/Code/C
AMIGA:amiga~/Desktop/Code/C> gcc noerror.c
AMIGA:amiga~/Desktop/Code/C> ./a.out
ERROR!
Not enough arguments!
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 v
ERROR!
Not enough arguments!
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 v 3
1, 3, v
AMIGA:amiga~/Desktop/Code/C> _
**************************************** */
ALSO IMPORTANT! This compiles and WORKS perfectly on the AMIGA but gives a segmentation fault on OSX 10.14.1, gcc 4.2.1.
/* Error gcc 4.2.1 demo. */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int NUM_1;
int NUM_2;
char CHARACTER;
int main(int argc, char *argv[])
{
int NUM_1 = strtod(argv[1], NULL);
int NUM_2 = strtod(argv[3], NULL);
CHARACTER = *argv[2];
/* Conditional AFTER _variables_! */
if (argc <= 3)
{
printf("ERROR!\n\n");
printf("Not enough arguments!\n");
exit(1);
}
printf("\n%i, %i, %c\n\n", NUM_1, NUM_2, CHARACTER);
return(0);
}
/* ****************************************
Last login: Fri Mar 15 15:48:37 on ttys000
AMIGA:amiga~> cd Desktop/Code/C
AMIGA:amiga~/Desktop/Code/C> gcc error.c
AMIGA:amiga~/Desktop/Code/C> ./a.out
Segmentation fault: 11
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 r
Segmentation fault: 11
AMIGA:amiga~/Desktop/Code/C> ./a.out 1 r 6
1, 6, r
AMIGA:amiga~/Desktop/Code/C> _
**************************************** */
As you can see the second code gives a Segmentation fault: 11
on OSX 10.14.1, gcc 4.2.1 but compiles and works correctly on the AMIGA.
Can anyone explain why there is a SEGMENTATION FAULT when the if
conditional statement is inside the 'main()' function to start with and the assignments set?