Chapter 2: Overview of C

📌 Learning Objectives
  • LO 2.1: Produce an overview of C programming language
  • LO 2.2: Exemplify elementary C concepts through sample programs
  • LO 2.3: Illustrate the use of user-defined functions and math functions
  • LO 2.4: Describe the basic structure of a C program
  • LO 2.5: Recognize the programming style of C language
  • LO 2.6: Describe how a C program is compiled and executed

2.1 History of C

C was evolved from ALGOL, BCPL, and B by Dennis Ritchie at Bell Laboratories in 1972. It was developed along with the UNIX operating system. The language was standardized by ANSI in 1989 (ANSI C) and later by ISO in 1990. The latest standard is C99/C11.

ALGOL (1960)
   |
   v
BCPL (1967)
   |
   v
  B (1970)
   |
   v
  C (1972) → ANSI C (1989) → C99 (1999) → C11 (2011)

Fig. 2.1: Evolution of C

2.2 Sample Program 1: Printing a Message

📝 Program 2.1

/* A simple program to print a message */
#include <stdio.h>

int main()
{
    printf("I see, I remember!\n");
    return 0;
}
Output:
I see, I remember!

Explanation:

  • #include <stdio.h> includes the standard input/output header file.
  • main() is the entry point of every C program.
  • printf() is a library function for output.
  • \n is the newline escape sequence.

2.3 Sample Program 2: Adding Two Numbers

📝 Program 2.2

/* Program to add two numbers */
#include <stdio.h>

int main()
{
    int number;
    float amount;
    
    number = 100;
    amount = 30.75 + 75.35;
    
    printf("%d\n", number);
    printf("%5.2f\n", amount);
    
    return 0;
}
Output:
100
106.10

Explanation:

  • int and float are data types.
  • %d is the format specifier for integers.
  • %5.2f prints a float with width 5 and 2 decimal places.

2.4 Basic Structure of a C Program

/* Documentation Section */
#include <stdio.h>      /* Link Section */
#define PI 3.1415       /* Definition Section */

int global_var;         /* Global Declaration */

int main()              /* main() Function */
{
    int local_var;      /* Declaration Part */
    local_var = 10;     /* Executable Part */
    printf("Hello");
    return 0;
}

/* Subprogram Section */
int function1() {
    /* function body */
}

2.5 Executing a C Program

Source Code (.c) → Compiler → Object Code (.o) → Linker → Executable Code (a.out)

Fig. 2.2: Compilation and linking process

💡 Note: Under UNIX/Linux, use cc program.c to compile, and ./a.out to execute. Under Windows with IDEs, use the IDE's build/run buttons.

Chapter Exercises

Review Questions

  1. State whether the following statements are true or false:
    a) Every line in a C program should end with a semicolon.
    b) The closing brace of main() is the logical end of the program.
    c) Comments cause the computer to print the text enclosed between /* and */ when executed.
  2. What does void main(void) mean?
  3. Describe the structure of a C program.

Multiple Choice Questions

  1. Which of the following characterize typical features of C language?
    a) Structured, high-level, machine independent
    b) Object-oriented, platform dependent
    c) Interpreted, dynamically typed
    d) None
  2. C language comprises ______ number of keywords.
    a) 32
    b) 16
    c) 64
    d) 128

Debugging Exercises

Find errors, if any, in the following program:

#include (stdio.h)
void main(void)
{
    print('Hello C');
}

Hint: Check header file syntax and function name.

Programming Exercises

  1. Write a program to display the equation of a line: ax + by = c, with a=5, b=8, c=18.
  2. Write a program to compute and display the value of x where x = a/(b-c). Test with (a=250, b=85, c=25).

Interview Questions

  1. Name some key features of C programming language.
  2. Which function is the starting point in a C program?