C is a procedural compiled programming language devised by Dennis Ritchie at the Bell Labs in New Jersey in 1972, and standardized in 1983 by ANSI and again by ISO in 1999. The original purpose of C was to write systems software for UNIX, but soon it became the most popular programming language for writing DOS applications, a popularity overshadowed only by the success of using C++ (http://planetmath.org/C) for Windows applications. From the beginning C provided access to certain lower level features of the computer and if that’s not enough it also had simpler mechanisms for meshing with assembly languagePlanetmathPlanetmath; for that reason C is sometimes compared to dangerous but powerful tools like a chainsaw (Banahan et al, 1991).

The following C program takes two integers as inputs and outputs their greatest common divisorMathworldPlanetmath using Euclid’s algorithmMathworldPlanetmath.

#include <stdio.h>
#include <stdlib.h>

int GCD(int x, int y) {
    int wX = x;
    int wY = y;
    int tempX;
    while ( wY > 0 ) {
	  tempX = wX;
	  wX = wY;
	  wY = tempX % wY;

int main() {
    int inputX;
    int inputY;
    int inputSuccess;
    printf("Please enter x ");
    inputSuccess = scanf("%d", &inputX);
    printf("Please enter y ");
    inputSuccess = scanf("%d", &inputY);
    printf("GCD of %d and %d is %d\n", inputX, inputY, GCD(inputX,inputY));

This program was tested in Visual C++ 6.0, and it works. The compiler reported 0 errors and 0 warnings, and the program works as it should, at least as long as the inputs are both positive integers. For the pair 42 and -21, it incorrectly reports the GCD as 42. Note that printf is not a reserved word of the C language, the way that PRINT is in BASIC or Fortran. We can use it because we included the standard I/O library; theoretically, we could write our own printf that tye-dyed the output onto a T-shirt as long as we wrote the appropriate library to take care of it.


  • 1 M. Banahan, D. Brady & M. Doran, The C book, featuring the ANSI C standard Reading, Mass: Addison-Wesley Publishing (1991)
Title C
Canonical name C1
Date of creation 2013-03-22 16:51:34
Last modified on 2013-03-22 16:51:34
Owner PrimeFan (13766)
Last modified by PrimeFan (13766)
Numerical id 11
Author PrimeFan (13766)
Entry type Definition
Classification msc 68N15
Synonym CPL