2.1 Introduction to C

C is a general-purpose programming language with features economy of expression, modern flow control and data structures, and a rich set of operators.

History of the C Programming Language

C is a general-purpose programming language with features economy of expression, modern flow control and data structures, and a rich set of operators.

C was developed at Bell Laboratories in 1972 by Dennis Ritchie. Many of its principles and ideas were taken from the earlier language B and B’s earlier ancestors BCPL and CPL. CPL ( Combined Programming Language ) was developed with the purpose of creating a language that was capable of both high level, machine independent programming and would still allow the programmer to control the behavior of individual bits of information.

There are some of C’s characteristics that define the language and also have lead to its popularity as a programming language. Naturally we will be studying many of these aspects throughout the course.

  • Small size
  • Extensive use of function calls
  • Loose typing — unlike PASCAL
  • Structured language
  • Low level (BitWise) programming readily available
  • Pointer implementation – extensive use of pointers for memory, array, structures and functions.

C has now become a widely used professional language for various reasons.

  • It has high-level constructs.
  • It can handle low-level activities.
  • It produces efficient programs.
  • It can be compiled on a variety of computers.

Its main drawback is that it has poor error detection which can make it off putting to the beginner. However diligence in this matter can pay off handsomely since having learned the rules of C we can break them. Not many languages allow this. This if done properly and carefully leads to the power of C programming.

C’s power and flexibility soon became apparent. Because of this, the Unix operating system which was originally written in assembly language, was almost immediately re-written in C ( only the assembly language code needed to “bootstrap” the C code was kept ). During the rest of the 1970’s, C spread throughout many colleges and universities because of it’s close ties to Unix and the availability of C compilers. Soon, many different organizations began using their own versions of C causing compatibility problems. In response to this in 1983, the American National Standards Institute ( ANSI ) formed a committee to establish a standard definition of C which became known as ANSI Standard C. Today C is in widespread use with a rich standard library of functions.

The Integrated Development Environment of C++ 3.0

Start Turbo C++ IDE

Change to directory C:\TC\BIN


The screen should look like Figure


Select the File menu to create a new file or open an existing file to edit.

Choose a file to open. This screen allow you to change to your directory.

Change directory dialog box

Compile and run a program

To compile a program , use F9 or the compile menu

To run a program, use the run menu or press Ctrl + F9

If your program have no syntax error, the user screen look like in Figure

The user screen of C++ 3.0

To exit from IDE, select the file menu, choose quit or press Alt+X

Basic Components of C Programs


A C program consists of the following characters:

26 capital letter of English alphabet : A, B, C, D, . . . . X, Y, Z

26 small letter of English alphabet : a, b, c, d, . . . .x, y, z

10 digits : 0, 1, . . . 9

Math operators : + – * / = < >

Other symbols : |, \, # , %, ~, . . . .

Key Words

A keyword is an identifier which indicate a specific command. Keywords are also considered reserved words. You shouldn’t use them for any other purpose in a C program.

The most important keywords of Turbo C are

asm auto break case char const continue default
do double else enum extern float for goto
if int long register return short signed sizeof
static struct switch typedef union unsigned void volatile


Identifiers or names refer to a variety of things : functions; tag of structures, union and enumerations; member of structures or unions; enumeration constants; typedef names and objects. There are some restrictions on the names .

Names are made up of letters and digit; The first character must be a letter. The underscore “_” count as a letter; sometime it is useful for improving the readability of long variable names. For example, name unit_price is easier to understand than unitprice. However, don’t begin variable names with underscore, since library routines often use such names.

Upper and lower case are distinct, so x and X are different names. Traditional C practice use lower case for variable names, and all upper case for symbolic constant.

Only the first 31 characters are significant.

Keywords are reserved: you can’t use them as variable names.

Example The following names are valid

i, x, b55, max_val

and the following names are invalid

12w the first character is a digit
income tax use invalid character “ “
char char is a keyword

It is wise to choose variable names that are related to the purpose of the variable, for example, count_of_girls, MAXWORD.

Data Types

Data is valuable resources of computers. Data may comprise numbers, text, images . . .

They belong to different data types.

In programming languages, a data type is a set of values and the operations on those values.

For example, int type is the set of 32-bit integers within the range -2,147,483,648 to 2,147,483,647 together with the operations described in the following table.

Operations Symbol
Addition +
Multiplication *
Division /
Modulus %
Equal to = =
Greater than >
Less than <

A data type can also be thought of as a constraint placed upon the interpretation of data in a type system in computer programming.

Common types of data in programming languages include primitive types (such as integers, floating point numbers or characters), tuples, records, algebraic data types, abstract data types, reference types, classes and function types. A data type describes representation, interpretation and structure of values manipulated by algorithms or objects stored in computer memory or other storage device. The type system uses data type information to check correctness of computer programs that access or manipulate the



In general, a constant is a specific quantity that does not or cannot change or vary. A constant’s value is fixed at compile-time and cannot change during program execution. C supports three types of constants : numeric, character, string.

Numeric constants of C are usually just the written version of numbers. For example 1, 0, 56.78, 12.3e-4. We can specify our constant in octal or hexadecimal, or force them to be treated as long integers.

  • Octal constants are written with a leading zero : -0.15
  • Hexadecimal constants are written with a leading 0x : 0x1ae
  • Long constants are written with a trailing L : 890L or 890l

Character constants are usually just the character enclosed in single quotes; ‘a’, ‘b’, ‘c’. Some characters can’t be represented in this way, so we use a 2 character sequence (escape sequence).

‘\n’ newline
‘\t’ horizontal tab
‘\v’ vertical tab
‘\b’ backspace
‘\r’ carriage return
‘\\’ backslash
‘\’’ single quote
‘\”’ double quotes
‘\0’ null (used automatically to terminate character strings)

Character constants participate in numeric operations just as any other integers (they are represented by their order in the ASCII character set), although they are most often used in comparison with other characters.

Character constants are rarely used, since string constants are more convenient. A string constant is a sequence of characters surrounded by double quotes e.g. “Brian and Dennis”.

A character is a different type to a single character string. This is important.

It is helpful to assign a descriptive name to a value that does not change later in the program. That is the value associated with the name is constant rather than variable, and thus such a name is referred to as symbolic constant or simply a constant.


Variables are the names that refer to sections of memory into which data can be stored.

Let’s imagine that memory is a series of different size boxes. The box size is memory storage area required in bytes.In order to use a box to store data, the box must be given a name, this process is known as declaration. It helps if you give a box a meaningful name that relates to the type of information and it is easier to find the data.The boxes must be of the correct size for the data type you are going to put into it. An integer number such as 2 requires a smaller box than a floating point number as 123e12.

Data is placed into a box by assigning the data to the box. By using the name of the box you can retrieve the box contents, some kind of data.

Variable named by an identifier. The conventions of identifiers were shown in 1.3.3.

Names should be meaningful or descriptive, for example, studentAge or student_age is more meaningful than age, and much more meaniful than a single letter such as a.


Programming languages have a set of operators that perform arithmetical operations , and others such as Boolean operations on truth values, and string operators manipulating strings of text. Computers are mathematical devices , but compilers and interpreters require a full syntactic theory of all operation in order to parse formulae involving any combination correctly.


An expression in a programming language is a combination of values, functions, etc. interpreted according to the particular rules of precedence and association for a particular programming language, which computes and returns another value.

C expressions are arranged in the following groups based on the operators they contain and how you use them:

  • Arithmetic expression
  • Conditional expression
  • Assignment expression
  • Comma expression
  • lvalue
  • Constant expression

Expressions are used as

  • Right hands of assignment statements
  • Actual parameters of functions
  • Conditions of if statements
  • Indexes of while statements
  • Operands of other expressions . . . .


A subprogram (also known as a procedure or subroutine) is nothing more than a collection of instructions forming a program unit written independently of the main program yet associated with it through a transfer/return process. Control is passed to the subprogram at the time its services are required, and then control is returned to the main program after the subprogram has finished.

The syntax used to represent the request of subprogram varies among the different language. The techniques used to describe a subprogram also varies from language to language. Many systems allow such program units to be written in languages other than that of the main program.

In most procedural programming languages, a subprogram is implemented as though it were completely separate entity with its own data and algorithm so that an item of data in either the main program or the subprogram is not automatically accessible from within the other. With this arrangement, any transfer of data between the two program parts must be specified by the programmer. This is usually done by listing the items called parameters to be transferred in the same syntactic structure used to request the subprogram’s execution.

The names used for the parameters within the subprogram can be thought of as merely standing in for the actual data values that are supplied when the subprogram is requested. As a result, you often hear them called formal parameters, whereas the data values supplied from the main program are refereed to actual parameters.

C only accept one kind of subprogram, function. A function is a sub program in which input values are transferred through a parameter list. However, information is returned from a function to the main program in the form of the “value of the function”. That is the value returned by a function is associated with the name of the function in a manner similar to the association between a value and a variable name. The difference is that the value associated with a function name is computed (according to the function’s definition) each time it is required, whereas when a variable ‘s value is required, it is merely retrieve from memory.

C also provide a rich collection of built-in functions.There are more than twenty functions declared in <math.h>. Here are some of the more frequently used.

Name Description Math Symbols Example
sqrt(x) square root
sqrt(16.0) is 4.0
pow(x,y) compute a value taken to an exponent, xy
pow(2,3) is 8
exp(x) exponential function, computes ex
exp(1.0) is 2.718282
log(x) natural logarithm ln x log(2.718282) is 1.0
log10(x) base-10 logarithm log x log10(100) is 2
sin(x) sine sin x sin(0.0) is 0.0
cos(x) cosine cos x cos(0.0) is 1.0
tan(x) tangent tg x tan(0.0) is 0.0
ceil(x) smallest integer not less than parameter xsize 12{ lceil x rceil } {} ceil(2.5) is 3; ceil(-2.5) is –2
floor(x) largest integer not greater than parameter xsize 12{ left lfloor x right rfloor } {} floor(2.5) is 2; floor(-2.5) is –3


The library is not part of the C language proper, but an environment that support C will provide the function declarations and type and macro definitions of this library.The functions, types and macro of the library are declared in headers.

C header files have extensions .h. Header files should not contain any source code. They are used purely to store function prototypes, common #define constants, and any other information you wish to export from the C file that the header file belongs to.

A header can be accessed by

 #include <header>

Here are some headers of Turbo C library

stdio.h Provides functions for performing input and output.

stdlib.h Defines several general operation functions and macros.

conio.h Declares several useful library functions for performing “console input and output” from a program.

math.h Defines several mathematic functions.

string.h Provides many functions useful for manipulating strings (character arrays).

io.h Defines the file handle and low-level input and output functions

graphics.h Includes graphics functions


A statement specifies one or more action to be perform during the execution of a program.

C requires a semicolon at the end of every statement.


Comments are marked by symbol “/*” and “*/”. C also use // to mark the start of a comment and the end of a line to indicate the end of a comment.

1. The Hello program written using the first  commenting style of C
/* A simple program to demonstrate
  C style comments

  The following line is essential 
  in the C version of the hello HUT program
#include <stdio.h>
	printf  /* just print */  ("Hello HUT\n"); 


The Hello program written using the second commenting style of C

//  A simple program to demonstrate
//  C style comments
//  The following line is essential 
//  in the C version of the hello HUT program
#include <stdio.h>
	printf("Hello HUT\n"); //print the string and then go to a new line


By the first way, a program may have a multi-line comments and comments in the middle of a line of code.However, you shouldn’t mix the two style in the same program.

C program structure

A C program basically has the following form:

  • Preprocessor Commands : Declare standard libraries used inside the program
  • Type definitions : Define new data types used inside the program
  • Function prototypes : Declare function types and variables passed to function.
  • Variables : State the names and data types of global variables
  • Functions: Include the main function(required) and the functions that their prototypes were announced above.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.