You are on page 1of 6

Lab 1

Introduction to C Language & Compiler

Objective
1. Computer Program
2. Programming Language
3. Intro. To C Language
4. Basic Syntax
5. Compiler
6. Tokens

What is Computer Program


A computer program is a set of instructions for a computer to perform a specific task. Programs generally fall into these
categories applications, utilities or services. Program are written in programming language then translated into machine
code by a compiler and linker so that the computer can execute it directly or run it line by line (interpreted) by an
interpreter program.

What is Programing Language


A programming language is a set of commands, instructions, and other syntax use to create a software program.

Low Level Language


A low-level language is a programming language that provides little or no abstraction of programming concepts, and is
very close to writing actual machine instruction. Two good example of low-level language are assembly and machine
code.

High Level Language


A HLL is a computer programming language that isnt limited by computer, designed for a specific job, and is easier to
understand. It is more like human language and less is easier to understand. However, for a computer to understand and
run a program created with high-level language, it must be compiled into machine language. The first high-level
language were introduced in the 1950s. Today, there are many high-level language in use, including BASIC, C, C++, Cobol
etc..

C Language - Overview
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX
operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R
standard.

The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has
now become a widely used professional language for various reasons

Easy to learn
Structured language

C Language
Abdul Qadeer Mudassar
abdulqadeer.mudassar@gmail.com
It produces efficient programs
It can handle low-level activities
It can be compiled on a variety of computer platforms

Facts about C
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around the early 1970s.
The language was formalized in 1988 by the American National Standard Institute (ANSI).
The UNIX OS was totally written in C.
Today C is the most widely used and popular System Programming Language.
Most of the state-of-the-art software have been implemented using C.
Today's most popular Linux OS and RDBMS MySQL have been written in C.

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

Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Databases
Language Interpreters
Utilities
C Programs

A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with
extension ".c"; for example, hello.c. You can use text editor to write your C program into a file.

Integrated development environment (IDE)

The IDE is also known as integrated design environment or integrated debugging environment, is a software application
that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of

GUI builder/ a source code editor


a compiler and/or an interpreter
build automation tools
a debugger

Turboo C++, Dreamweaver are examples of IDEs

C Language
Abdul Qadeer Mudassar
abdulqadeer.mudassar@gmail.com
Steps involved in writing and executing a program

C Compiler
It translates the C source code into object code. It take some time to analyze and process a program, the resulting
executable is some form of machine- specific binary code. The computer hardware interprets (executes) the resulting
code, hence the program execution is fast.

Creating a program
Use an editor to write the source code. This file contains a source code which consists of executable code. Save the file
with .c extension only.

Preprocessing: Using a Preprocessor program to convert C source code in expanded source code. "#includes" and
"#defines" statements will be processed and replaced actually source codes

Compiling the program

The next step is to compile the program. The code is compiled by using compiler. i.e. object code. The compiler program
convert C expanded source to assembly source code.

Assembly
Using an Assembler program to convert assembly source code to object code.

Linking a program to library

C Language
Abdul Qadeer Mudassar
abdulqadeer.mudassar@gmail.com
The object code of a program is linked with libraries that are needed for execution of a program. The linker is used to
link the program with libraries. It creates a file with .exe extension. Multiple units of object codes are linked together.

Execution of program
The final executable file is then run by the command.

--Loading: Using a Loader program to load the executable code into CPU for execution.

Learn C Language

C standard library
A C library is a file containing several object files that can be used as a single entity in a linking phase of a program.
Normally the library is indexed, so it is easy to find symbols (functions, variables and so on) in them. For this reason,
linking a program whose object files are ordered in libraries is faster than linking a program whose object files are
separate on the disk.

The C standard library consists of a set of sections of the ISO C standard which describe a collection of headers and
library routines used to implement common operations, such as input/output and string handling.

#include <stdio.h>
#include <conio.h>
C Preprocessor directives
A Preprocessor is a program that processes the code before it passes through the compiler. The preprocessor directives
are placed in the source program before the main line before the source code passes through the compiler it is examined
by the preprocessor for any preprocessor directives. The preprocessor is executed before the actual compilation of code
begins. Preprocessor directives follow the special syntax rules and begin with the symbol # and do not require any semicolon
at the end.

For example:

#include Specifies a file to be included

#define Defines a macro substitution

C - Program Structure
Before we study the basic building blocks of the C programming language, let us look at a bare minimum C program
structure so that we can take it as a reference in the upcoming chapters.

pre-processor directives
C Language
Abdul Qadeer Mudassar
abdulqadeer.mudassar@gmail.com
global declarations

main()
{
local variable deceleration
statement sequences
function invoking
}
Hello World Example
A C program basically consists of the following parts

Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments

Let us look at a simple code that would print the words "Hello World"

#include<stdio.h>
void main(){

printf("Hello World");
}

Let us take a look at the various parts of the above program


The first line of the program #include <stdio.h> is a preprocessor command, which tells a C compiler to include
stdio.h file before going to actual compilation.
The next line void main() is the main function where the program execution begins.
The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the
program. So such lines are called comments in the program.
The next line printf(...) is another function available in C which causes the message "Hello, World!" to be
displayed on the screen.

Compile and Execute C Program


Let us see how to save the source code in a file, and how to compile and run it. Following are the simple steps

Write program in Turboo C++ or Code block


After that in Turboo C++ click on F2 to save
To compile Alt+F9
To run Ctrl+F9 or in code block press F9 only.

C Language
Abdul Qadeer Mudassar
abdulqadeer.mudassar@gmail.com
Tokens
A token is the smallest element in a program that is meaningful to the compiler. These tokens define the structure of the
language. The token set can be divided into five categories: Identifiers, Keywords, Literals, Operators, and Separators.

1. Identifiers
Identifiers are names provided by you. These can be assigned to variables, methods, functions etc. to uniquely identify
them to the compiler.

2. Keywords
Keywords are reserved words that have a specific meaning for the compiler. They cannot be used as identifiers. For
Example int,float, main, void etc.

3. Literals
A Literal is the source code representation of a fixed value; literals are represented directly in your code without
requiring computation

4. Operators
An operator is a symbol that operates on one or more operands to produce a result.

They will be discussed in greater detail in the next article.

5. Separators
Separators are symbols that indicate the division and arrangement of groups of code. The structure and function of code
is generally defined by the separators. The separators used in C are as follows:

parentheses ( )
braces { }
brackets [ ]
semicolon ;

C Language
Abdul Qadeer Mudassar
abdulqadeer.mudassar@gmail.com