Lab1 (Introduction To C Language)
Lab1 (Introduction To C Language)
Objective
1. Computer Program
2. Programming Language
3. Intro. To C Language
4. Basic Syntax
5. Compiler
6. Tokens
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.
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
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
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.
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:
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");
}
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.
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