Lecture 2 Introduction To C Programming
Lecture 2 Introduction To C Programming
Assembly Python
C
C#
Java
Fortran
Low level Language:
• Advantages:
• Provide convenient representation and “pseudo-instructions
• Easily convertible to machine language. (10101010001)
• Fast Execution time.
• Disadvantages:
• Not easy to understand
• Machine-dependent (The program will only work for the
machine it was designed for)
• Near to the machine and far away from the programmer.
• Knowledge of hardware is required when programming in
assembly language.
Machine Code and Assembly Language
• Ultimately all computer programs must be converted into
machine code for execution on the CPU.
• A computer can be programmed using assembly
language, which is a readable version of machine
code with one-to-one correspondence to the actual
machine instructions.
Low level Language: Assembly
High level languages
• Advantages:
• Provide strong abstraction, style and context which are more
comfortable to learn.
• Machine independent (No need to learn the hardware
specification)
• Closer to the human language and far from the machine
language.
• Provide rich syntax, vocabulary and subroutines( Arrays,
functions, operators, variables etc.)
• Disadvantages:
• Must be compiled or interpreted before the execution.
• Takes additional time to execute to translate the source code
to machine code.
• High-level language programs are slower than low-level
programs
High level language operation
3. Points CPU to
generated code
CPU
executing
executing
2A 0C 40
AF
Programming 06 03
Print Cls
Instruction
language C5
Instruction (itself a machine 06 FF 7E
instruction code 23
10 FC C1
program)
10 F4 C9
1. Reads text 2. Generates
file machine code
Program source
code typed into a
text file (or lots of
text files in practice)
Compilers and Interpreters
• A compiler converts the text instructions into a machine
code program once as part of the development process.
After that the machine code program can be run over
and over again with no compilation.
• An interpreter converts the text instructions into a
machine code program line by line every time it is run
and the machine code program is not kept anywhere.
Can you think of differences between low and high level
languages?
C Programming Language
Top Programming Languages
Source: https://www.tiobe.com/tiobe-index/
Origins of C
• C is a by-product of UNIX, developed at Bell Laboratories by
Ken Thompson, Dennis Ritchie, and others.
• Thompson designed a small language named B that was
based on BCPL (Basic Combined Programming
Language), a systems programming language developed in
the mid-1960s.
• By 1971, Ritchie began to develop an extended version of B.
• He called his language NB (“New B”) at first.
• As the language began to diverge more from B, he changed
its name to C.
• The language was stable enough by 1973 that UNIX could be
rewritten in C.
Copyright © 2008 W. W.
Norton & Company.
15 All rights reserved.
Standardization of C
• K&R C
• Described in Kernighan and Ritchie, The C
Programming Language (1978)
• De facto standard
• C89/C90
• ANSI standard X3.159-1989 (completed in 1988;
formally approved in December 1989)
• International standard ISO/IEC 9899:1990
• C99
• International standard ISO/IEC 9899:1999
• Incorporates changes from Amendment 1 (1995)
Copyright © 2008 W. W.
Norton & Company.
16 All rights reserved.
C- Middle level language
• C is often called a "Middle Level" programming language. This is because of its
capability to access the system's low level functions. 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, probably does not supply all the constructs
found in high-languages - but it provides with all the building blocks that are
needed to produce the results a programmer wants!
Source: Leon, A. and Leon, M., 1999. Introduction to computers. Leon Techworld.
C-Based Languages
• C++ includes all the features of C but adds classes
and other features to support object-oriented
programming.
• Java is based on C++ and therefore inherits many C
features.
• C# is a more recent language derived from C++ and
Java.
• Perl has adopted many of the features of C.
Copyright © 2008 W. W.
Norton & Company.
All rights reserved.
18
Properties of C
• General-purpose programming
• Cross-platform
• C is a relatively low language
Copyright © 2008 W. W.
Norton & Company.
All rights reserved.
19
Strengths of C
• Efficiency
• Portability
• Power
• Flexibility
• Standard library
• Integration with UNIX and Linux
Copyright © 2008 W. W.
Norton & Company.
All rights reserved.
20
Weaknesses of C
• Programs can be error-prone.
• Programs can be difficult to understand. (check The
International Obfuscated C Code Contest IOCC)
• Programs can be difficult to modify.
Copyright © 2008 W. W.
Norton & Company.
All rights reserved.
21
Effective Use of C
• Learn how to avoid pitfalls.
• Use software tools (Codeblocks, debuggers) to make programs
more reliable.
• Take advantage of existing code libraries.
• Adopt a sensible set of coding conventions.
• Avoid “tricks” and overly complex code.
• Stick to the standard.
Copyright © 2008 W. W.
Norton & Company.
All rights reserved.
22
C Program Structure
•A C program basically consists of the following parts
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
Hello World Example
C Program Structure
• 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 int 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.
• The next line return 0; terminates the int main() function and returns
the value 0.
Layout of C program
pre-processor directives
global declarations
main()
{
local variables to function main ;
statements associated with function main ;
}
function1()
{
local variables to function 1 ;
statements associated with function 1 ;
}
function2()
{
local variables to function f2 ;
statements associated with function 2 ;
}
C Program Execution
Compilation and execution process of C can be divided in to multiple steps:
• Pre-processing
Using a Pre-processor program to convert C source code in expanded source code. "#includes" and "#defines"
statements will be processed and replaced actually source codes in this step.
• Compilation
Using a Compiler program to convert C expanded source to assembly source code.
• Assembly
Using an Assembler program to convert assembly source code to object code.a
• Linking
Using a Linker program to convert object code to executable code. Multiple units of object codes are linked to together
in this step.
• Loading
Using a Loader program to load the executable code into CPU for execution.
Source: https://sites.google.com/site/cpge6151/c-pro/compilation-and-linking-processes
C Program Execution
• What is the difference between a linker and loader?
C Basic Syntax
C Basic Syntax: Semicolon & comments
• In a C program, the semicolon is a statement terminator. That is, each individual
statement must be ended with a semicolon. It indicates the end of one logical entity.
For example:
printf("Hello, World! \n");
return 0;
• Comments are like helping text in the C program and they are ignored by the compiler.
They start with /* and terminate with the characters */ as shown below −
/* my first program in C */
• Comments within comments are not allowed and they do not occur within a string or
character literals.
C Basic Syntax: Whitespace
• Whitespace is the term used in C to describe blanks, tabs, newline
characters and comments.
• Whitespace separates one part of a statement from another and enables
the compiler to identify where one element in a statement, such as int,
ends and the next element begins. For example:
int age;
There must be at least one whitespace character (usually a space) between int and age for
the compiler to be able to distinguish them. On the other hand, in the following statement:
fruit = apples + oranges; // get the total fruit
No whitespace characters are necessary between fruit and =, or between = and apples,
although one can include some to increase readability.
C Data Types