Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

The History of C

Bell Labs first developed the C language in the early 1970s, primarily so that Bell
programmers could write their UNIX operating system for a new DEC (Digital Equipment
Corporation) computer. Bell Labs designed UNIX to run efficiently on small equipment, and
it was the first operating system to be written entirely in a high-level programming
language. Until that time, operating systems were written in assembly language, the
computer's low-level, hardware-based language that is tedious, time-consuming, and
difficult to change. The Bell Labs designers knew they needed a higher-level programming
language to implement their project quicker and make its code easier to maintain.

Because other high-level languages at the time (COBOL, FORTRAN, PL/I, and Algol) were
too slow to use for an operating system's code, the Bell Labs programmers decided to write
their own language. They based their new language on Algol and BCPL, two high-level but
efficient languages. BCPL strongly influenced C, although BCPL did not offer the various
data types that the makers of C required. After a few versions, these Bell programmers
developed a language that met their goals very well. C is efficient (it is sometimes called a
high low-level language due to its speed of execution) and flexible, and contains the proper
constructs enabling it to be maintained over time.

High-level Computer Languages and Compilers

High-level programming languages, such as C, simplify programming in several ways. First,


you don't have to express your instructions in a numeric code. Second, the instructions you
use are much closer to how you might think about a problem than they are to the detailed
approach a computer uses. Rather than worry about the precise steps a particular CPU
would have to take to accomplish a particular task, you can express your desires on a more
abstract level. To add two numbers, for example, you might write the following:

c = a + b;

Seeing code like this, you have a good idea what it does; looking at the machine-language
equivalent of several instructions expressed in numeric code is much less enlightening.

For a computer, the opposite is true. To it, the high-level instruction is an incomprehensible
stream of text. This is where compilers come in. The compiler is a program that translates
the high-level language program into the detailed set of machine language instructions the
computer requires. You do the high-level thinking; the compiler takes care of the tedious
details.

The compiler approach has another benefit. In general, each computer design has its own
unique machine language. So a program written in the machine language for, say, an Intel
Pentium CPU means nothing to a Motorola CPU based iOs device. But you can match a
compiler to a particular machine language. Therefore, with the right compiler or set of
compilers, you can convert the same high-level language program to a variety of different
machine-language programs. You solve a programming problem once, and then you let your
compilers translate the solution to a variety of machine languages.

In short, high-level languages, such as C, Java, and Pascal, describe actions in a more
abstract form and aren't tied to a particular CPU or instruction set. Also, high-level
languages are easier to learn and much easier to program in than are machine languages.
Why use C?
C has been used successfully for every type of programming problem imaginable from
operating systems to spreadsheets to expert systems - and efficient compilers are
available for machines ranging in power from the Apple Macintosh to the Cray
supercomputers. The advantages of C include:

1. the portability of the compiler;


2. the standard library concept;
3. a powerful and varied repertoire of operators;
4. an elegant syntax;
5. ready access to the hardware when needed;
6. and the ease with which applications can be optimised by hand-coding isolated
procedures

C is often called a "Middle Level" programming language. This is not a reflection on its lack
of programming power but more a reflection on its capability to access the system's low
level functions. Most high-level languages (e.g. Fortran) provides everything the
programmer might want to do already built into the language. A low level language (e.g.
assembler) provides nothing other than access to the machines basic instruction set. A
middle level language, such as C, doesn't supply all the constructs found in high-languages
- but it provides you with all the building blocks that you will need to produce the results
you want!

Uses of C
C was initially used for system development work, in particular the programs that make-up
the operating system. Why use C? Mainly because it produces code that runs nearly as fast
as code written in assembly language. Some examples of the use of C might be:

1. Operating Systems
2. Language Compilers
3. Assemblers
4. Text Editors
5. Print Spoolers
6. Network Drivers
7. Modern Programs
8. Data Bases
9. Language Interpreters
10. Utilities

In recent years C has been used as a general-purpose language because of its popularity
with programmers.
Steps of C Programming

There are four fundamental stages, or processes, in the creation of any C program:
• Editing
• Compiling
• Linking
• Executing

i. Editing
This is the process of creating and modifying C source code—the name given to the
program instructions you write. Some C compilers come with a specific editor that can
provide a assistance in managing your programs. An editor often provides a complete
environment for writing, managing, developing, and testing your programs. This is
sometimes called an integrated development environment, or IDE.

You can also use other editors to create your source files, but they must store the code as
plain text without any extra formatting data embedded in it. In general, if you have a
compiler system with an editor included, it will provide a lot of features that make it easier
to write and organize your source programs. There will usually be automatic facilities for
laying out the program text appropriately, and color highlighting for important language
elements, which not only makes your code more readable but also provides a clear indicator
when you make errors when keying in such words. These will often provide a lot of help in
ensuring your code is correct with syntax highlighting and autoindenting of your code. Don’t
use a word processor such as Microsoft Word, as these aren’t suitable for producing
program code because of the extra formatting information they store along with the text.

ii. Compiling
The compiler converts your source code into machine language and detects and reports
errors in the compilation process. The input to this stage is the file you produce during your
editing, which is usually referred to as a source file. The compiler can detect a wide range
of errors that are due to invalid or unrecognized program code, as well as structural errors
where, for example, part of a program can never be executed. The output from the compiler
is known as object code and is stored in files called object files, which usually have
names with the extension .obj in the Microsoft Windows environment, or .o in the
Linux/UNIX environment. The compiler can detect several different kinds of errors during
the translation process, and most of these will prevent the object file from being created.
The result of a successful compilation is a file with the same name as that used for the
source file, but with the .o or .obj extension.

iii. Linking
The linker combines the various modules generated by the compiler from source code files,
adds required code modules from program libraries supplied as part of C, and welds
everything into an executable whole. The linker can also detect and report errors, for
example, if part of your program is missing or a nonexistent library component is
referenced. In practice, if your program is of any significant size, it will consist of several
separate source code files, which can then be linked. A large program may be difficult to
write in one working session, and it may be impossible to work with as a single file. By
breaking it up into a number of smaller source files that each provide a coherent part of
what the whole program does, you can make the development of the program a whole lot
easier. The source files can be compiled separately, which makes eliminating simple
typographical errors a bit easier. Furthermore, the whole program can usually be developed
incrementally. The set of source files that make up the program will usually be integrated
under a project name, which is used to refer to the whole program. Program libraries
support and extend the C language by providing routines to carry out operations that aren’t
part of the language. For example, libraries contain routines that support operations such as
performing input and output, calculating a square root, comparing two character strings, or
obtaining date and time information. A failure during the linking phase means that once
again you have to go back and edit your source code. Success on the other hand will
produce an executable file. In a Microsoft Windows environment, this executable file will
have an .exe extension; in UNIX, there will be no such extension, but the file will be of an
executable type. Many IDEs also have a Build option, which will compile and link your
program in one step. This option will usually be found, within an IDE, in the Compile menu;
alternatively, it may have a menu of its own.

iv. Executing
The execution stage is where you run your program, having completed all the previous
processes successfully. Unfortunately, this stage can also generate a wide variety of error
conditions that can include producing the wrong output or just sitting there and doing
nothing, perhaps crashing your computer. In all cases, it’s back to the editing process to
check your source code. This is the stage where, you get to see your computer doing
exactly what you told it to do! In UNIX and Linux you can just enter the name of the file
that has been compiled and linked to execute the program. In most IDEs, you’ll find an
appropriate menu command that allows you to run or execute your compiled program. In
Windows, you can run the .exe file for your program as you would any other executable.
The processes of editing, compiling, linking, and executing are essentially the same for
developing programs in any environment and with any compiled language.

Introduction to Integrated Development Environments (Windows)

C compilers are not part of the standard Windows package, so you may need to obtain and
install a C compiler. Quite a few vendors, including Microsoft, Borland, Metrowerks, and
Digital Mars, offer Windows-based integrated development environments, or IDEs.
Code::Blocks is full featured open source IDE. Most IDEs are combined C and C++
compilers. All have fast, integrated environments for putting together C programs. The key
point is that each of these programs has a built-in editor you can use to write a C program.
Each provides menus that enable you to name and save your source code file, as well as
menus that allow you to compile and run your program without leaving the IDE. Each takes
you back into the editor if the compiler finds any errors, and each identifies the offending
lines and matches them to the appropriate error messages.

Because the Windows IDEs typically handle both C and C++, you need to indicate that you
want a C program. With most IDEs, you use the project type to indicate that you want to
use C. With other products, such as Microsoft Visual C++, you use the .c file extension to
indicate that you want to use C rather than C++. However, most C programs also work as
C++ programs

Note: For the purposes of this course, we shall use the Code::Blocks IDE. This can
be downloaded from http://codeblocks.org

C's Character Set


C does not use, nor requires the use of, every character found on a modern computer
keyboard. The only characters required by the C Programming Language are as follows:
1. A - Z
2. a -z
3. 0 - 9
4. space . , : ; ' $ "
5. # % & ! _ {} [] () < > |
6. + - / * =

KEYWORDS
There are 32 words defined as keywords in C programming language. These keywords have
predefined uses and cannot be used for any other purpose in a C program. These keywords
are used by the compiler, as an aid to building the program. Note that these keywords must
always be written in lowercase see Table below
Keyword Description
auto Defines a local variable as having a local lifetime
break Passes control out of the programming construct
case Branch control
char Basic data type
const Unmodifiable value
continue Passes control to loop’s beginning
default Branch control
do Do While loop
double Floating-point data type
else Conditional statement
enum Defines a group of constants of type int
extern Indicates an identifier as defined elsewhere
float Floating-point data type
for For loop
goto Transfers program control unconditionally
if Conditional statement
int Basic data type
long Type modifier
register Stores the declared variable in a CPU register
return Exits the function
short Type modifier
signed Type modifier
sizeof Returns expression or type size
static Preserves variable value after its scope ends
struct Groups variables into a single record
switch Branch control
typedef Creates a new type
union Groups variables that occupy the same storage space
unsigned Type modifier
void Empty data type
volatile Allows a variable to be changed by a background routine
while Repeats program execution while the condition is true

The main() FUNCTION


All C programs begin with the main() function. Let’s first, however, see what a function is.
From a programming perspective, functions allow you to group a logical series of activities,
or program statements, under one name.
For example, suppose you create a function called bakeCake. The algorithm for baking a
cake might look like this:
 Mix wet ingredients in mixing bowl
 Combine dry ingredients
 Spoon batter into greased baking pan
 Bake cake at 350 degrees for 30 minutes
Note: An algorithm is a finite step-by-step process for solving a problem.
Anyone reading the code will see the function called bakeCake and know right away that
you are trying to bake cakes.
Functions are typically not static, meaning they are entities, that take in and pass back
information. Thus, the bakeCake function would take in a list of ingredients to bake (called
parameters) and return back a finished cake (called a value).
The main() function is like any other programming function in that it groups like activities
and can take in parameters (information) and pass back values (again, information). What
makes the main() function unique from other functions, however, is that the values it
returns are returned to the operating system. Other functions that you will use and create in
C return values back to the calling C statement inside the main() function.
The main function looks like this:-
i. Void of parameters (functions that do not take parameters) and do not return values.
main()
{
}
ii. Void of parameters (functions that do not take parameters) but return values
int main()
{
}

As the preceding examples show, the main() function begins with the keyword main and is
followed by two empty parentheses (). The parentheses are used to encompass parameters
to be passed to the main() function.
Note: C is a case-sensitive programming language. For example, the function
names main(), Main(), and MAIN() are not the same. It takes extra computing
resources to NOT be case-sensitive as input devices such as keyboards distinguish
between cases.

Following the parentheses are two braces. The first brace denotes the beginning of a logical
programming block and the last brace denotes the end of a logical programming block. Each
function implementation requires that you use a beginning brace, {, and a closing brace, }.
The following program code demonstrates a complete, simple C program. From this code,
you will learn how single program statements come together to form a complete C program.

/* COM 121 Procedural Programming I */


//by Mr. E. Kiprop
#include <stdio.h>
main()
{
printf("\n Hello World\n");
}

When the preceding program is compiled and run, it outputs the text “Hello World” to the
computer screen,
Multiline Comment block

Single line comment block

Preprocesson directive

Begin logical program block

Program statement

End logical program block


Program statement terminator
Escape sequence
printf function

Lets now go through these components one by one.

COMMENTS
Comments are an integral part of program code in any programming language. Comments
help to identify program purpose and explain complex routines. They can be valuable to you
as the programmer and to other programmers looking at your code.
In the following line of code, the text C Programming for the Absolute Beginner is ignored
by the compiler because it is surrounded with the character sets /* and */.

/* COM 121 Procedural Programming I


Topic 1
Example program */

The character set /* signifies the beginning of a comment block; the character set */
identifies the end of a comment block. These character sets are not required to be on the
same line and can be used to create both single-line and multi-line comments. To
demonstrate, the following block of code shows the usefulness of multi-line commenting.

Your C program may not compile correctly if you leave one of the comment character sets
out or if you reverse the characters. For example, the following code segment leaves out a
comment character set and will not compile.

/* COM 121 Procedural Programming I


The next line of code also will not compile because comment character sets have been
incorrectly ordered.
*/ COM 121 Procedural Programming I */

You can also create quick one-line comments with the character set //. The next line of code
demonstrates this.
//by Mr. E. Kiprop

Any characters read after the character set // are ignored by the compiler for that line only.
To create a multi-line comment block with character set //, you will need the comment
characters in front of each line. For example, the following code creates a multi-line
comment block.
//COM 121 Procedural Programming I
//Topic 1
//Example program

PROGRAM STATEMENTS
Many lines in C programs are considered program statements, which serve to control
program execution and functionality. Many of these program statements must end with a
statement terminator. Statement terminators are simply semicolons (;). The next line of
code, which includes the printf() function, demonstrates a program statement with a
statement terminator.
printf("\nHello World!\n");
Some common program statements that do not require the use of statement terminators
are the following:
• Comments
• Preprocessor directives (for example, #include or #define)
• Begin and end program block identifiers
• Function definition beginnings (for example, main())

The preceding program statements don’t require the semicolon (;) terminator because they
are not executable C statements or function calls. Only C statements that perform work
during program execution require the semicolons.
A function commonly used for displaying output to the computer screen is the printf()
function. As shown next, the printf() function is used to write the text “Hello World!” to
the standard output.
printf("\nHello World!\n");
Like most functions, the printf() function takes a value as a parameter (More of functions
later in the course)
Any text you want to display in the standard output must be enclosed by quotation marks.
For the most part, characters or text that you want to appear on-screen are put inside
quotation marks, with the exception of escape characters or escape sequences. The
backslash character (\) is the escape character. When the printf() statement shown above
is executed, the program looks forward to the next character that follows the backslash. In
this case, the next character is the character n. Together, the backslash (\) and n
characters make up an escape sequence.

Escape Sequences
Escape sequences are specially sequenced characters used to format output.
This particular escape sequence (\n) tells the program to add a new line. Look at the
following program statement.
printf("\nC you later\n");
This printf() function adds two new lines for formatting purposes. Before any text is shown,
the program outputs a new line. After the text is written to standard output, in this case the
computer screen, another new line is written.

Table below describes some common escape sequences.


Escape Sequence Purpose
\a Produces a audible or visual alert
\b moves the active position back one space on the current line
\n Creates a new line
\r Moves the cursor to the beginning of the current line
\t Moves the cursor to the next tab
\\ Inserts a backslash
\" Inserts a double quote
\' Inserts a single quote
\a produces an audible or visible alert
\? Question mark (?).

Escape sequences must be enclosed in single quotes when assigned to a character variable.
For example, you could make the statement
char advance = '\n';

and then print the variable advance to advance the printer or screen one line.

The alert character (\a), produces an audible or visible alert. The nature of the alert
depends on the hardware, with the beep being the most common. (With some systems, the
alert character has no effect.) The alert character shall not change the active position. By
active position, the standard means the location on the display device (screen, teletype,
printer, and so on) at which the next character would otherwise appear. The active position
is a generalization of the screen cursor.

Next, the \b, \n, \r, \t, and \v escape sequences are common output device control
characters. These affect the active position. A backspace (\b) moves the active position
back one space on the current line. A newline character (\n) sets the active position to the
beginning of the next line. A carriage return (\r) moves the active position to the beginning
of the current line. This is useful for some formatting tasks when the cursor’s position is of
importance, especially with printed output because a printer can overwrite text already
printing. A horizontal tab character (\t) moves the active position to the next horizontal tab.

The three escape sequences (\\, \', and \") enable you to use \, ', and " as character
constants. Because these symbols are used to define character constants as part of a
printf() command, the situation could get confusing if you use them literally. Suppose you
want to print the following line:

Jane says, "a \ is a backslash."

Then use this code:

printf("Jane says, \"a \\ is a backslash.\"\n");


More examples:
printf("\nA single quote looks like \'\n");
printf("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");

When a character, be it an escape sequence or not, is part of a string of characters enclosed


in double quotes, don't enclose it in single quotes. Notice that none of the other characters
in this example (J, a, n, e, and so on) are marked off by single quotes. A string of
characters enclosed in double quotes is called a character string

Directives
Notice the program statement that begins with the pound sign (#):
#include <stdio.h>
When the C preprocessor encounters the pound sign, it performs certain actions depending
on the directive that occurs prior to compiling. In the example, the program told the
preprocessor to include the stdio.h library with my program. The name stdio.h is short
for standard input output header file. It contains links to various standard C library
functions, such as printf().

The preprocessor looks at your program before it is compiled (hence the term
preprocessor). Following your preprocessor directives, the preprocessor replaces the
symbolic abbreviations in your program with the directions they represent. The
preprocessor can include other files at your request, and it can select which code the
compiler sees.

All preprocessor directives begin with the # symbol at the beginning of a line. The #
symbol can be preceded by spaces or tabs, and it allows for space between the # and the
remainder of the directive. A directive can appear anywhere in the source file, and the
definition holds from its place of appearance to the end of the file.

When the preprocessor spots an #include directive, it looks for the following filename and
includes the contents of that file within the current file. The #include directive in your
source code file is replaced with the text from the included file. It's as though you typed in
the entire contents of the included file at that particular location in your source file.

Why include files? Because they have information the compiler needs. The stdio.h file, for
example, typically includes definitions of EOF, NULL, getchar(), and putchar(). The last
two are defined as macro functions. It also contains function prototypes for the C I/O
functions.

The .h suffix is conventionally used for header files—files with information that are placed at
the head of your program. Header files often contain preprocessor statements. Some, such
as stdio.h, come with the system, but you are free to create your own.

GCC COMPILER
The gcc compiler is an ANSI standard C compiler. A C program goes through a lot of steps
prior to becoming a running or executing program. The gcc compiler performs a number of
tasks for you. Most notable are the following:
• Preprocesses the program code and looks for various directives.
• Generates error codes and messages, if applicable.
• Compiles program code into an object code and stores it temporarily on disk.
• Links any necessary library to the object code and creates an executable file and stores
it on disk.
ANSI is an abbreviation for the American National Standard for Information
Systems. ANSI’s common goal is to provide computing standards for people who
use information systems.

Use the .c extension when creating and saving C programs. This extension is the standard
naming convention for programs created in C.

You might also like