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

Fundamentals of Software Development

Lecture 2: Introduction to C Programming


Recap
• What is a Flowchart?
• What is a pseudocode?
• What is an algorithm?
Programming Languages
Programming Languages

• What is a programming language?


• A programming language is an artificial language
designed to communicate instructions to a computer
• Syntax and vocabulary for providing computer
instructions
• “Programming languages are the least usable, but
most powerful human-computer interfaces ever
invented”Source: link
Programming Languages: Types
Programming
Languages

Low Level Mid-Level High Level


Language Language Language

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.

• A line containing only whitespace, possibly with a comment, is known as


a blank line, and a C compiler totally ignores it.
C Basic Syntax: Identifier
• A C identifier is a name used to identify a variable, function, or any
other user-defined item. Identifier must be unique. They are created to
give unique name to a entity to identify it during the execution of the
program.
• An identifier starts with a letter A to Z, a to z, or an underscore '_'
followed by zero or more letters, underscores, and digits (0 to 9).
• C does not allow punctuation characters such as @, $, and % within
identifiers. C is a case-sensitive programming language. Thus,
Manpower and manpower are two different identifiers in C.
• Here are some examples of acceptable identifiers:
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
C Basic Syntax: Keywords
• Following list shows the reserved words a.k.a. keywords in C. These
reserved words may not be used as constants or variables or any
other identifier names.
Variables
Variables
• A variable is nothing but a name given to a storage area that the programs
can manipulate.
•Variable names are just the symbolic representation of a memory location.
• Each variable in C has a specific type, which determines the size and layout
of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and the
underscore character. It must begin with either a letter or an underscore.
Upper and lowercase letters are distinct because C is case-sensitive.
Variables
Type Description

char Typically a single octet(one byte). This is an integer type.

int The most natural size of integer for the machine.

float A single-precision floating point value.

double A double-precision floating point value.

void Represents the absence of type.

• C programming language also allows to define various other types of


variables, that will be covered subsequently like Array, Structure, Union, etc.
Variable Definition
• A variable definition tells the compiler where and how much
storage to create for the variable. A variable definition specifies a
data type and contains a list of one or more variables of that type
as follows:
• type variable_list;
• Here, type must be a valid C data type including char, int, float,
double or any user-defined object; and variable_list may consist
of one or more identifier names separated by commas.
• Some valid declarations are shown here:
• int i, j, k; • float f, salary;
• char c, ch; • double act_balance;
Variable Definition
• Variables can be initialized (assigned an initial value) in their declaration. The
initializer consists of an equal sign followed by a constant expression as
follows:
• type variable_name = value;
• Some examples are
• int d = 3, f = 5; // definition and initializing d and f.
• float pi=3.14 //definition and initializing d and f.
• char x = 'x'; // the variable x has the value 'x’.
Variable Declaration
• A variable declaration provides assurance to the compiler that there exists a
variable with the given type and name so that the compiler can proceed for
further compilation without requiring the complete detail about the variable.
• A variable declaration is useful while using multiple files and the variable is
defined in one of the files which will be available at the time of linking of the
program.
• A variable definition has its meaning at the time of compilation only, the
compiler needs actual variable definition at the time of linking the program.
Can you think of the difference between variable
declaration and variable initialisation?
Basic output - printf()
• The printf statement allows you to send output to standard
out. For us, standard out is generally the screen.

• Formatting takes place via placeholders within the format


string.
printf("Your age is %d", age);
Basic input - scanf()
• The scanf function allows you to accept input from
standard in, which for us is generally the keyboard.
scanf("%d", &b);
The program will read in an integer value that the user
enters on the keyboard (%d is for integers, as is printf, so b
must be declared as an int) and place that value into b.
• The scanf function uses the same placeholders as printf:
• int uses %d
• float uses %f
• char uses %c
• character strings use %s
Next Lecture:

C Data Types

You might also like