gcc 4.2.4

Version of implementation gcc of programming language C

gcc 4.2.4 is a bug-fix release, containing fixes for regressions in gcc 4.2.3 relative to previous releases of gcc.

Examples:

Factorial - C, Objective-C (67):

This example uses recursive factorial definition. Note that 13! and larger causes an overflow, so the last lines of the output look like this:

13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184

#include <stdio.h>

unsigned long long factorial(unsigned long long n)
{
    if (n == 0) {
        return 1;
    } else {
        return n * factorial (n - 1);
    }
}

int main(void)
{
    int n;
    for (n = 0; n <= 16; n++) {
        printf("%i! = %lld\n", n, factorial(n));
    }
    return 0;
}

CamelCase - C (282):

This example is based on character-wise string processing. fgets here reads at most 99 characters into the string, and stops when it finds end-of-string character, so a long line might be split. C doesn’t provide boolean data type, so it has to be simulated using integer variable.

#include <stdio.h>

void main() {
    char text[100],cc[100];
    fgets(text, sizeof text, stdin);
    int i,j=0,lastSpace=1;
    for (i=0; text[i]!='\0'; i++) 
        if (text[i]>='A' && text[i]<='Z' || text[i]>='a' && text[i]<='z')
        {   if (lastSpace>0)
                cc[j] = toupper(text[i]);
            else
                cc[j] = tolower(text[i]);
            j++;
            lastSpace = 0;
        }
        else
            lastSpace = 1;
    cc[j]='\0';
    printf("%s\n",cc);
}

Quadratic equation - C (370):

This example requires C99, since it uses complex type introduced in it.

#include <stdio.h>
#include <complex.h>
#include <math.h>

void print(int ind, double complex x) {
    printf("x%d = ", ind);
    if (fabs(cimag(x)) < 1e-6)
        printf("%f\n", creal(x));
    else printf("(%f, %f)\n", creal(x), cimag(x));
}

int main() {
    double A, B, C;
    double D;
    printf("A = ");
    scanf("%lf", &A);
    if (fabs(A)<1E-3) {
        printf("Not a quadratic equation\n");
        return 1;
    }
    printf("B = ");
    scanf("%lf", &B);
    printf("C = ");
    scanf("%lf", &C);

    A *= 2;
    D = B*B-A*C*2.0;
    if (fabs(D)<1E-3)
        printf("x = %f", creal(-B/A));
    else {
        print(1, (-B+csqrt(D))/A);
        print(2, (-B-csqrt(D))/A);
    }
    return 0;
}

Quadratic equation - C, Objective-C, C++ (216):

This example works both for C and C++, as well as for Objective-C which is superset of C.

#include <math.h> 
#include <stdio.h>

int main()
{
  int A, B, C;
  double D;
  printf("A = ");
  scanf("%d", &A);
  if (A == 0) {
    printf("Not a quadratic equation.\n");
    return 0;
  }
  
  printf("B = ");
  scanf("%d", &B);
  printf("C = ");
  scanf("%d", &C);

  D = B * B - 4 * A * C;
  if (D == 0) {
    printf("x = %f\n", -B / 2.0 / A);
    return 0;
  }
  
  if (D > 0) {
    printf("x1 = %f\nx2 = %f\n",
           (-B + sqrt(D)) / 2.0 / A, (-B - sqrt(D))/ 2.0 / A);
  } else {
    printf("x1 = (%f, %f)\nx2 = (%f, %f)\n",
           -B / 2.0 / A, sqrt(-D) / 2.0 / A, -B / 2.0 / A, -sqrt(-D) / 2.0 /A);
  }
  return 0;
}

Hello, World! - C, Objective-C, C++ (68):

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}