0% found this document useful (0 votes)
8 views

Compilers

Compila

Uploaded by

yasirkhosa902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Compilers

Compila

Uploaded by

yasirkhosa902
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/compi_p.

html

Part A - Introduction

Compilers
Edit, compile and run C programs
Self-document programs using comments

"C is quirky, flawed and an enormous success." (Ritchie, D. 1993)

Languages | C Compilers | Basic C Syntax | Exercises

Programming languages demand completeness and greater precision than human languages. The ultimate interpreter of a
computer program is a machine. A machine cannot interpret intent or nuance and we need to provide more detail in our
instructions than we do in casual conversations with humans. In this sense, programming is more arduous than writing a formal
report for someone else to read.

This chapter describes the generations of prgramming languages, identifies some key features of the C language, describes the
compilers that we use to convert programs written in C into binary instructions that hardware can execute and reviews the basic
syntax introduced in the sample programs.

LANGUAGES

Generations
Well over 2500 programming languages exist. The different generations of programming languages that computer scientists have
developed include:

1. Machine languages. These are the native languages that CPU processes. Each manufacturer provides its own set of
instructions.
2. Assembly languages. These are readable versions of the native machine languages. Assembly languages simplify
coding considerably. Each manufacturer provides the assembly language for its own machines.
3. Third-generation languages. We use these languages to describe how a result is to be obtained. The instructions are
NOT tied to any particular machine. Examples include C, C++ and Java.
4. Fourth-generation languages. We use these languages to describe what is to be done without specifying how it is to be
done. These instructions are NOT tied to any particular machine. Examples include SQL, Prolog, and Matlab.
5. Fifth-generation languages. We use these languages for artificial intelligence, fuzzy sets, and neural networks.

The third, fourth and fifth generation languages are high-level languages. High-level languages exhibit no direct connection to
any machine language. Their instructions are more human-like and less machine-like. A program written in a high-level
language is relatively easy to port across different platforms.

Eric Levenez maintains an up-to-date map of 50 of the more popular languages.

TIOBE Software tracks the currently most popular languages and the long-term trends based on world-wide availability of
software engineers, courses and third party vendors as calculated from Google, Bing, Yahoo!, Wikipedia, Amazon, YouTube and

1 of 5 2/3/2016 7:10 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/compi_p.html

Baidu search engines.

Features of C
C is one of the more popular third-generation languages. It serves as an excellent, first language for several reasons:

C is English-like
C is quite compact - has a small number of keywords
C programs that need to be maintained are large in number
C is the lowest in level of the high-level languages
C is faster and more powerful than other high-level languages
UNIX, Linux and Windows operating systems are written in C and C++

Comparative times for a standard test (Sieve of Eratosthenes) are

Language Time to Run


Assembler 0.18 seconds
C 2.7 seconds
Basic 10 seconds

C COMPILERS
To convert a program written in the C language into the machine language equivalent, we use an operating system program
called a compiler. A compiler takes a set of program instructions as input and converts that set into a set of machine language
instructions. We call the input to the compiler our source code and the output from the compiler the binary code. Once we have
compiled our program, we do not need to re-compile it unless we change the code. To execute the program, we direct the
operating system to load the binary code.

Compilers can optimize our program instructions.

Examples
Let us write a program that displays the phrase "This is C" and name our source file hello.c. Source files in the C
language end with the extension .c.

In a text editor, enter the following statements:

/* My first program // comments introducing the source file


hello.c */

#include <stdio.h> // information about the printf identifier

2 of 5 2/3/2016 7:10 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/compi_p.html

int main(void) // the starting point of the program


{
printf("This is C"); // send output to the screen

return 0; // return control to the operating system


}

Each line is explained in more detail in the following section.

Linux

The C compiler that ships with the Linux operating system is called gcc.

To create a machine language version of our source code, enter the following command

gcc hello.c

By default, the gcc compiler produces an output file named a.out. a.out contains all of the machine language needed to
execute the program.

To execute the machine language version of the program, enter the command shown on the left. The output that will appear on
the screen is shown on the right:

a.out This is C

Windows

The C compiler that runs on Windows platforms is called cl. To access this compiler, open a Visual Studio command prompt
window. To create a machine language version of our source code, enter the following command

cl hello.c

By default, the cl compiler produces a file named hello.exe. hello.exe contains the machine language version of our
source code. To execute this machine language version, enter the command shown on the left. The output that will appear on the
screen is shown on the right:

hello This is C

BASIC SYNTAX
The source code listed above contains basic syntax found in nearly every C program.

Documentation

3 of 5 2/3/2016 7:10 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/compi_p.html

The comments self-document our source code and enhance its readability. Comments are important in the writing of any
program. C supports two styles: multi-line and inline.

Multi-Line Comments

/* My first program
hello.c */

/* and */ delimit comments that may extend over several lines. Within these delimiters, we may include any character, except
the */ pair.

Inline Comments

int main(void) // the starting point of the program

// indicates that the following characters to the end of the line are a comment that the compiler should ignore.

Whitespace

Whitespace enhances program readability by helping display the structure of a program. C compilers ignore whitespace
altogether. Whitespace refers to any of the following:

blank space
newline
horizontal tab
vertical tab
form feed
comments

We may introduce whitespace anywhere except within an identifier or within a pair of double quotes. main and printf are
identifiers in the sample above. The blank spaces within "This is C" are characters within a set rather than whitespace.

We start source code with comments to identify the program and provide important distinguishing information.

Indentation

By indenting the printf("This is C") statement and placing it on a separate line, we show that printf("This is
C") is part of something larger, which we have called int main(void).

Program Startup
Every C program includes a line like int main(void). Program execution starts at this line, which we call the entry point
for the program. The pair of braces that follows this syntax contains the program instructions.

int main(void) // program startup


{

return 0; // return to operating system


}

The operating system transfers control to this entry point when the user loads the binary code (a.out). The last statement
(return 0;) before the closing brace transfers control back to the operating system.

Program Output

4 of 5 2/3/2016 7:10 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/compi_p.html

The following executable statement outputs "This is C" to the standard output device.

printf("This is C");

The line before int main(void) tells the compiler that printf is a valid identifier.

#include <stdio.h> // information about the printf identifier

Case Sensitivity
The C language is case sensitive. That is, the compiler treats the character 'A' as different from the character 'a'. If we
change the identifier printf() to PRINTF() and recompile, the compiler will report an error. However, if we change
"This to "THIS and recompile, the compiler will not report any error. It accepts changes in case within a string literal.

EXERCISES
Install the cl compiler on your own computer (Visual Studio)
Complete the Workshop on Getting Started

5 of 5 2/3/2016 7:10 PM

You might also like