SlideShare a Scribd company logo
1 of 74
Instructor
Mr. S.Christalin Nelson
01-Sep-2017
Computer Fundamentals - II
At a Glance
• Generation of Programming Languages
• Programming Paradigms
• Structure & Execution Environment of a Basic C Program
• Need of Translators, Linkers, Locaters, Loaders & Editors
• Software Engineering & Problem Solving Methods
• Few Inclusions from C99 and C11 Standards
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
2 of 74
01-Sep-2017
Generations
• Generation -1: Machine Level Programming Languages (late
1940s)
• Generation -2: Assembly Languages (early 1950s)
• Generation -3: High-Level Languages (mid 1950s to present)
• Generation -4: Specification/Query Language, Report
Generators (1970s to present)
• Generation -5: Artificial Intelligence
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
4 of 74
Features of Languages (1/2)
• Machine Level Language
– Fast and efficient, executed directly on the CPU
– Consists only of 0s and 1s
– Difficult for humans to read, write, and debug
• Assembly Language
– Also included Interpreting Routines & early Compilers
– Simple mnemonic instructions <opcode> <operands>
– Assembler translates into machine code
– Hand coding in assembly only for low-level needs
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
5 of 74
Features of Languages (2/2)
• High Level Languages
– Easier for humans to read, write, debug
– Compiler translates into machine code before running
– Interpreter translates into machine code at runtime
– Examples: FORTRAN, LISP, COBOL, ALGOL (Ada, Basic, C, C++,
Java, Pascal, Smalltalk, …)
• Fourth Generation
– Examples: Maple, Mathematica, Postscript, SPSS, SQL
• Fifth Generation
– Examples: Prolog
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
6 of 74
Family Tree of Languages
01-Sep-2017
Fortran
BASIC
Cobol
LISP
Scheme
ML
Prolog
PL/1
Algol 60
Algol 68
Pascal
Modula 3
Ada
C
C++
Simula
Smalltalk
Java
Dylan
Ruby
Perl
PythonC#
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
7 of 74
01-Sep-2017
Procedural Paradigm
• Procedures, sequential execution of code are basic building
blocks of program.
• Examples
– FORTRAN (FORmula TRANslating; John Backus, IBM, 1950s)
– ALGOL (ALGOrithmic Language, 1958)
– COBOL (COmmon Business Oriented Language, 1960)
– BASIC (Beginner's All-purpose Symbolic Instruction Code, John
Kemeny and Thomas Kurtz, Dartmouth, 1963)
– Pascal (Niklaus Wirth, 1970)
– C (Dennis Ritchie, Bell Labs, 1972)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
9 of 74
Object Oriented Paradigm
• Program is designed around the objects and classes required
to solve the problem.
• Examples
– Smalltalk (Alan Kay, Xerox PARC, 1971)
– Ada (US Dept of Defense, 1975)
– C++ (Bjarne Stroustrup, Bell Labs, 1983)
– Java (James Gosling, Sun Microsystems, 1995)
– C# (Microsoft, 2000)
01-Sep-2017
Alan Kay
B. Stroustrup J. Gosling
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
10 of 74
Functional Paradigm
• Program is designed around the evaluation of functions,
rather than modifying state
• Examples
– LISP (John McCarthy, MIT, 1958)
– Common Lisp
– Dylan
– Logo
– Scheme
– ML (Robin Milner et al, Edinburgh, 1970s)
– Haskell (purely functional language, 1990)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
11 of 74
Logic & Scripting Paradigms
• Logic
– Program is declarative, based on mathematical logic
– Example: Prolog (1972)
• Scripting
– Used for text processing, shells, HTML, CGI
– Examples
• awk (Aho, Weinberger, Kerningham, Bell labs, 1978)
• Perl (Larry Wall, NASA, 1987)
• Tcl/Tk (John Ousterhout, 1988)
• Python (Guido van Rossum, CWI, 1991)
• PHP (Rasmus Lerdorf, 1995)
• Ruby (Yukihiro Matsumoto, 1996)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
12 of 74
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin
Nelson|SoCSE|UPES
13 of 74
01-Sep-2017
History of C Language
• Kenneth Thompson and Dennis Ritchie at Bell Labs in 1972
• History
– International Group, ALGOL (1960)
– Martin Richards, BCPL (1967)
– Ken Thompson, B (1970)
– Dennis Ritchie, Traditional C (1972)
– Kernighan & Ritchie, K&R C (1978)
– Standardization (incompatible code of different compilers)
• In 1983, ANSI (American National Standard Institute) started
defining a standard for C called ANSI-C. ANSI-C was adopted by
ISO in 1990 renamed to C89 (Officially ANSI/ISO 9989:1990)
• C99 (Officially ISO/IEC 9899:1999)
• C11 (Officially ISO/IEC 9899:2011)
01-Sep-2017
Brian Kernighan and D. Ritchie
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
15 of 74
C Language
• High Level Procedural Language
• Features
– Coded routines of UNIX OS
– Concise syntax
– Programs a Efficient and Fast
– Highly portable
– Case-sensitive
– No built-in operations for handling composite data types such
as strings, sets, and lists
• Suited for writing both System software and Business
packages (E.g. C Compilers)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
16 of 74
Basic Structure of C Program
Documentation Section (Comments)
Link Section (Link functions from System Library)
Definition Section (Define Symbolic Constants)
Global Declaration Section (Global Variables & User Defined Fn.)
main() Function Section
{
Declaration Part
Executable Part
}
Subprogram Section
User-Defined Functions
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
17 of 74
Sample C Program
• Problem: Sum of two Numbers
//my first program to explain the structure
#include <stdio.h> //preprocessor directive
void main() //Program Entry: main() fn.
{ //Start of Program
int a, b, sum; //declaration of variables
printf(“Enter value of a & b: ”); //Output: printf() built-in fn.
scanf(“%d %d”, &a, &b); //Input: scanf() built-in fn.
sum = a + b; //Arithmetic expression/stmt.
printf(“Sum: %d”, sum);
} //End of Program
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
18 of 74
Preprocessor Directives
• Provides instruction to the compiler before the actual program
execution begins
• Hash symbol (#)
• Types (partial list)
– # include <headerfile.h>
• Specify header file which holds prototype definition of built-in
functions. Compiler informs linker to link these files with program
• Example: #include <stdio.h>
– “stdio.h” contains prototype definition of printf() and scanf()
– # define const_name const_value
• Defines Symbolic constants
• Example: # define PI 3.14
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
19 of 74
Function Definition
• Definition: Function/Procedure/Sub-routine
– Set of Instructions that are executed to achieve a particular
task
– Code reusability
• Types
– Built-in Functions (System Defined)
• E.g. printf(), scanf()
– User-defined Functions
• Syntax
return_type function_name(Argument list)
{ //fn. Definition starts
Set of Instructions //fn. body
} //fn. Definition ends
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
20 of 74
Program Input
• scanf() function
– Input from Standard Input
– Prototype definition available in stdio.h
• E.g. scanf(“%d %d”, &a, &b);
– Format String
• Specifies the expected data type format of the input variables
using corresponding format specifiers
– Address-of Operator (&)
• Provides the memory address of the input variable were the
input value is to be stored
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
21 of 74
Program Output
• printf() function
– Output to Standard Output
– Prototype definition available in stdio.h
– Format
• Print a String
– E.g. printf(“Hello”); //Output String “Hello”
• Print a value within a String
– E.g. printf(“Sum: %d”, sum); //Value of sum is printed with String
– Format specifier (E.g. %d)
» Specifies the expected data type format of the output variable
whose value is to be printed within the Output String
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
22 of 74
Format Specifiers in C (1/2)
Format Specifier printf() scanf()
%c Prints a single character Read a character
%d Prints a signed decimal integer Read a signed decimal integer
%i
Prints a signed decimal
integer
Read a signed integer depending
upon the prefix
- a hexadecimal integer if prefix
is 0x
- a signed octal if prefix is 0
- if no prefix read signed decimal
integer
%u
Prints an unsigned decimal
integer
Unsigned decimal integer
%o Prints an unsigned octal integer Unsigned octal integer
%x
Prints an unsigned hexadecimal
using a,b,c,d,e,f
Read unsigned hexadecimal integer
%X
Prints an unsigned hexadecimal
using A,B,C,D,E,F
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
23 of 74
Format Specifiers in C (2/2)
Format Specifier printf() scanf()
%f
A floating point with 6 digits after
decimal point
Read floating point number
%e
Floating point in exponential
format with e
Read floating point number
%E
Floating point in exponential
format with E
%g
Float in %f or %e whichever is
shorter
Floating point number
%G
Float in %f or %E whichever is
shorter
%s String Read a string (till null character)
%% Prints % sign Not applicable
%p
Prints address in hexadecimal
format
Not applicable
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
24 of 74
Program Execution (1/7)
01-Sep-2017
Preprocessing
Program Compilation
Linking
Locating
• Steps involved
Loading
Program Execution
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
Program Creation
Source Code (*.c)
Expanded Source Code (*.i)
Object Code
Executable Object Code
Assembly Code
25 of 74
Program Execution (2/7)
• Preprocessor Stage
– The following occurs to the Source code
• Header Files are expanded
• Comments are removed
• Macros are replaced
– To print the expanded code on terminal after preprocessing,
the command in Linux is
• $cc -E file_name.c
– To obtain the expanded code in a file the command in Linux is
• $cc -E file_name.c -o file_name.i
– After above said command is executed, a file “file_name.i” is
generated in present working directory and whole code obtained
after pre-processing stage is stored in this file. This file can be
opened for viewing the whole content.
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
26 of 74
Program Execution (3/7)
• Compilation Stage
– Considered to include (1) Compiler Stage & (2) Assembler Stage
• Compiler Stage
– At compilation stage an assembly code (with .s as extension) is
generated on the basis of Source code i.e. depending upon the
functions and structures used in the C program.
– To obtain the assembly code the foll. commands can be used
• $cc –S file_name.c OR $cc –S file_name.i
• Note: Assembler accept files with .i or .c extension
– The obtained file name can be specified as follows:
• $cc –S file_name.i -o file_name.s
– Note: The contents of .s file are not understandable but can be
viewed
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
27 of 74
Program Execution (4/7)
• Assembler Stage
– Assembler converts the assembly code to an object code
(machine code) with .obj(windows) or .o(Linux) extension.
– The commands are
• $cc –c file_name.c or $cc –c file_name.i or $cc -c file_name.s
– The obtained file name can be specified as follows:
• $cc –c file_name.s –o file_name.o
– Note: The contents of object file are not understandable but
can be viewed
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
28 of 74
Program Execution (5/7)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
29 of 74
Program Execution (6/7)
• Linker Stage
– At linker stage other object files (E.g. Library Files) are linked
and an executable file with .exe(windows) or a.out(Linux) is
generated.
• Library files are pre-compiled files which can be directly linked
– Command
• $gcc file_name.c
– Note:
• Executing above command generates a file with name a.out
which is the executable format of last compiled c-program.
• Every time a new file is compiled the a.out is replaced with new
executable file
• To retain the executable content, the obtained file name can be
specified as follows $ gcc file_name.c -o file_name
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
30 of 74
Program Execution (7/7)
• To run the executable place ./ preceding to file_name
– $./a.out or $./file_name
• Note
– To get all the files in one shot the following command can be
used
• $gcc –save-temps file_name.c -o file_name
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
31 of 74
Program Execution & Errors
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
32 of 74
01-Sep-2017
Editor
• Software used to create and modify source programs
– Has commands that change, delete or insert lines/characters
• Available with Assembly Language programming and High
level language programming
• Example
– Linux OS: vi, gedit, nano, pico, vim
– Windows OS: notepad
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
34 of 74
Translators
• Software that translates the user program into the machine
readable/understandable program
• Types
– Assemblers
– Compilers
– Interpreters
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
35 of 74
Assembler
• Software that translates Assembly language program into
Machine language program
• Reads the source program more than once
• Creates two files after translation
– Object File (Binary code of Instruction & Information about
Instruction Addresses)
– Assembler List File (Assembly language statements, Binary
code for each Instruction & Offset for each instruction)
• Types
– Self/Resident Assembler & Cross Assembler
– One-pass & Two-pass Assembler
– Meta Assembler
– Macro Assembler
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
36 of 74
Compiler (1/6)
• Software that translates High level language program into
Machine language program
• Compiler goes through the entire program – then translates
the entire program into machine codes
• Types
– Self or Resident Compiler
– Cross Compiler
• Vs. Assembler
– A compiler is more intelligent – It checks all kinds of limits,
ranges, errors etc.
– Program run time is more & It has slow speed01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
37 of 74
Compiler (2/6)
Steps involved during
Compilation
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
38 of 74
Compiler (3/6)
• Lexical Analysis or Tokenizer
– Produces Tokens from Source Code, Symbol Table and Literal
Table
• E.g. Seeing a variable, the tokenizer would generate the token
“var” and then associate the variable name (lexeme) with it in
the symbol table.
– Removes whitespace and comments.
– Handles compiler directives (i.e., as a preprocessor).
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
39 of 74
Compiler (4/6)
• Syntactic Analysis
– Tokens are parsed to produce Parse Tree while checking that
tokens conform with the rules of the language.
– Note: Parse trees are often converted into a simplified form
known as a Syntax tree.
• E.g. Consider expression 3+4*5
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
40 of 74
Compiler (5/6)
• Syntactic Analysis
– Tokens are parsed to produce Parse Tree while checking that
tokens conform with the rules of the language.
– Note:
• Parse trees are often converted into a simplified form known as a
Syntax tree.
• Semantic Analysis
– Produces Annotated Tree from Syntax tree.
– In addition to this, a literal table (contains information on the
strings and constants used in the program), and a symbol table
(stores information on the identifiers used in the program) are
produced by the various stages of the process.
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
41 of 74
Compiler (6/6)
• Intermediate Code Generator & Code Optimizer
– Intermediate code generator receives input from semantic
analyzer in the form of an annotated syntax tree.
– Need of Intermediate Code Generator instead of direct
translation into target machine code?
• Intermediate code eliminates the need of a new full compiler for
every unique machine by keeping the analysis portion same for
all the compilers. The second part of compiler, synthesis, is
changed according to the target machine. It is easier to apply the
source code modifications to improve code performance by
applying code optimization techniques on the intermediate code.
• Note: Intermediate code can be either language specific or
language independent.
• Error handler
– Catch errors generated by any stages of the program.
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
42 of 74
Interpreter
• Software that translates High level language program into
Machine language program (same as Compiler)
• Vs. Compiler
– Translates only one statement of the program at a time and
executes it. In this way it proceeds further till all the
statements are translated and executed
– A compiler is 5 to 25 times faster than an interpreter
– Compiler saves the machine codes permanently for future
reference. Interpreter does not save the machine codes
– An interpreter is a small program as compared to compiler. It
occupies less memory space, so it can be used in a smaller
system which has limited memory space
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
43 of 74
01-Sep-2017
Flow Chart (Operation of Interpreter vs. Compiler)
CSEG1001/1002
Instructor: Mr.S.Christalin
Nelson|SoCSE|UPES
44 of 74
Linker
• Software that links built-in functions to the System libraries
& user-defined functions to the User-defined libraries
– If linker does not find a library of a function then it informs to
compiler and then compiler generates an error
• Linker is used to combine the program modules for program
execution
– Modules: Smaller subprograms that together make a longer
program
• Compiler automatically invokes linker as the last step in
program compilation
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
45 of 74
Locator
• Software that assigns specific addresses at which the
Executable Object code should be loaded in the System
Memory
• Example
– IBM-PC DOS has a locator called EXE2BIN
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
46 of 74
Loader
• Software that loads machine codes (executable object code)
of a program into the system memory and prepares them for
execution
– i.e. Reads contents of executable file into memory
• Integral part of Operating System (OS)
– Once loading is complete, the OS starts the program by passing
control to the loaded program code
– In many OS, the loader is permanently resident in memory
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
47 of 74
01-Sep-2017
Software Engineering
• Definition
• Need of Paradigms
• Types
– Classical Life Cycle Model or Waterfall Model
– Iterative Model
– Rapid Application Development Model (RAD)
– Prototyping Model
– Spiral Model
– V Model
• CASE Tools
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
49 of 74
Waterfall Model
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
50 of 74
Software Development Life Cycle (SDLC)
• (1) Problem Statement
• (2) Requirement Gathering & Analysis
• (3) Propose Solution & Select an best feasible solution
• (4) Design
– Problem solving methods (Flow Chart, Algorithm, Pseudo Code)
• (5) Test the Design: If Test fails – Repeat from Step(4)
• (6) Coding
– Choice of Programming Lang. Paradigm – Language Selection
• (7) Test the Code: If Test fails – Repeat from Step(6)
• (8) Deployment
• (9) Maintenance
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
51 of 74
01-Sep-2017
Flow Chart
CSEG1001/1002
Instructor: Mr.S.Christalin
Nelson|SoCSE|UPES
52 of 74
Flow Chart Notation (1/3)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
53 of 74
Flow Chart Notation (2/3)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
54 of 74
Flow Chart Notation (3/3)
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
55 of 74
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin
Nelson|SoCSE|UPES
56 of 74
Program Execution using Flow Chart (1/2)
01-Sep-2017
System Ready
Enter ProgramProgram Code
Edit Source Program
Compile Source
Program
C Compiler
Syntax
Errors?
A
Source Program
Object Code
B
Logic Error
Syntax Error
No Errors
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
57 of 74
Program Execution using Flow Chart (2/2)
01-Sep-2017 Stop
System Library
Link with System
Library
Execute Object Code
Input
Data
Correct
Output
Logic &
Data
Errors?
Object Code
A
Executable Object Code
No Errors
Data Error
B
Logic Error
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
58 of 74
Algorithm
• Algorithm
– Set of operations to be performed Step-by-step.
– Written in natural language.
– Starting from an initial state and initial input the instructions
describe a computation that, when executed, proceeds
through a finite number of well-defined successive states,
eventually producing "output" and terminating at a final
ending state.
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
59 of 74
Example - 1
• Problem: Snakes and Ladders Game
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
60 of 74
01-Sep-2017
Problem: Snakes and Ladders Game
Flow Chart
CSEG1001/1002
Instructor: Mr.S.Christalin
Nelson|SoCSE|UPES
61 of 74
Example – 1 (contd.)
• Problem: Snakes and Ladders Game
– Algorithm
• (1) Game begins
• (2) Roll the dice
• (3) Move ahead as many steps as the number on the dice
– Is there a ladder? If YES, climb it & goto step 5. If NO goto step 4
• (4) Is there a snake?
– If YES, go down & goto step 6
– If NO, remain where your are & goto step 5
• (5) Have you reached End?
– If YES, You have won the game. Goto step 7
– If NO, goto step 6
• (6) Next player's turn – goto step 2
• (7) Game Over
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
62 of 74
Example - 2
• Problem: Find Sum of two Numbers
– Algorithm
• (1) Start Operation
• (2) Declare integer variable A, B, Sum
• (3) Read two numbers A, B
• (4) Find Sum = A+B
• (5) Print Sum
• (6) End Operation
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
63 of 74
01-Sep-2017
Problem: Finding sum of two numbers
Flow Chart Testing the Flow Chart
CSEG1001/1002
Instructor: Mr.S.Christalin
Nelson|SoCSE|UPES
64 of 74
Pseudocode
• Written in a format that is closely related to high level
programming language structures.
• Uses syntax common to many programming languages.
– These include standard looping structures like FOR … ENDFOR,
WHILE…ENDWHILE
– There are also some terms for standard conditional clauses like
IF-THEN … ENDIF, WHILE … ENDWHILE (this is both a loop and
a conditional clause by the way), CASE … ENDCASE
• Transforming pseudocode to programming code could be
much easier than converting an algorithm written in natural
language.
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
65 of 74
Example
• Problem: Accept two numbers from the keyboard and
calculate the sum and product.
– Pseudocode
• Use variables sum, product, number1, number2 of type real
• Display “Input two numbers”
• Accept number1, number2
• sum = number1 + number2
• Print “The sum is “, sum
• product = number1 * number2
• Print “The Product is “, product
• End program
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
66 of 74
Program Control Flow
• Order of Execution of Instructions
• Types
– Sequence (Default: Top to Bottom)
– Loop
• Iteration & Recursion
– Branching
• Conditional & Un-Conditional Branching or Jump
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
67 of 74
Activity-3
• Solve the following problems with a suitable formulae/logic
and provide the design using Algorithm and Flow chart.
– (1) Find the maximum of three numbers.
– (2) Find area and circumference of a Circle.
– (3) Convert temperature into Centigrade/Fahrenheit/Kelvin.
– (4) Find Simple Interest.
– (5) Swap two numbers with/without using third variable.
– (6) Find the given number is Even or Odd.
– (7) Find the given number is Prime number or not.
– (8) Check whether the given year is Leap year or not.
– (9) Multiply two numbers without using *operator.
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
68 of 74
Tips to Solve Activity-3
• Area = π x radius2 Circumference = π x d (d = 2r)
• °F = °C x (9/5) + 32, °C = (°F - 32) x (5/9), K = °C + 273.15
• Simple Interest = Principal x Duration x Interest Rate
• Swapping using temporary variable
– temp = a; a = b; b = temp;
• Swapping without temporary variable
– Method 1: x = x + y; y = x - y; x = x - y;
– Method 2: x = x * y; y = x / y; x = x / y;
– Method 3: x = x ^ y; y = x ^ y; x = x ^ y;
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
69 of 74
01-Sep-2017
Some Inclusions from C99
• Variables can be defined anywhere inside the function block
• New Data Types
– Boolean data type “bool” used with “stdbool.h” and “_Bool”
• Constants: “true” and “false”.
• Boolean values still behave as integers (Can be stored in integer
variables, and used anywhere integers would be valid)
– Complex data type “complex” used with “complex.h”
• Functions: creal(), cimag(), conj()
• Support to “static inline” functions
• Universal character names allows user variables to contain
other characters than the standard character set which is its
Unicode value in the form: uXXXX or UXXXXXXXX
– XXXX or XXXXXXXX - Unicode code point in hexadecimal notation
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
71 of 74
Inclusions from C11
• Use of Anonymous Structure and Anonymous Union when
nested inside another structure.
– i.e. Structure/Union without an Identifier
• Caution with usage of gets(), deprecated in the previous C
language standard revision, ISO/IEC 9899:1999/Cor.3:2007(E)
– Using gets() may lead to buffer overflow. Hence, fgets() and
gets_s() are preferred.
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
72 of 74
Peep into the next Module
• Programming using C Language - I
– Programming Fundamentals
– Literals & Variables
– Data Types & Modifiers
– Qualifiers & Constants
– Storage Class Specifiers
– Operators & Expressions
– Data Type Conversions
– Loops & Decision Making
01-Sep-2017
CSEG1001/1002
Instructor: Mr.S.Christalin Nelson|SoCSE|UPES
73 of 74
01-Sep-2017

More Related Content

What's hot

WinSock Asynchronous Input/Output
WinSock Asynchronous Input/OutputWinSock Asynchronous Input/Output
WinSock Asynchronous Input/Output
calophatpho
 

What's hot (20)

Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
intro_dgital_TV
intro_dgital_TVintro_dgital_TV
intro_dgital_TV
 
Closing the RISC-V compliance gap via fuzzing
Closing the RISC-V compliance gap via fuzzingClosing the RISC-V compliance gap via fuzzing
Closing the RISC-V compliance gap via fuzzing
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Accelerating HPC Applications on NVIDIA GPUs with OpenACC
Accelerating HPC Applications on NVIDIA GPUs with OpenACCAccelerating HPC Applications on NVIDIA GPUs with OpenACC
Accelerating HPC Applications on NVIDIA GPUs with OpenACC
 
dld 01-introduction
dld 01-introductiondld 01-introduction
dld 01-introduction
 
GDPS and System Complex
GDPS and System ComplexGDPS and System Complex
GDPS and System Complex
 
Continguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelContinguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux Kernel
 
Karnaugh Map
Karnaugh MapKarnaugh Map
Karnaugh Map
 
WinSock Asynchronous Input/Output
WinSock Asynchronous Input/OutputWinSock Asynchronous Input/Output
WinSock Asynchronous Input/Output
 
Data representation
Data representationData representation
Data representation
 
Number systems
Number systemsNumber systems
Number systems
 
Kernel Recipes 2015: Representing device-tree peripherals in ACPI
Kernel Recipes 2015: Representing device-tree peripherals in ACPIKernel Recipes 2015: Representing device-tree peripherals in ACPI
Kernel Recipes 2015: Representing device-tree peripherals in ACPI
 
ASIC Design and Implementation
ASIC Design and ImplementationASIC Design and Implementation
ASIC Design and Implementation
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux Interrupts
 
RISC-V Foundation Overview
RISC-V Foundation OverviewRISC-V Foundation Overview
RISC-V Foundation Overview
 
Synopsys Fusion Compiler-Comprehensive RTL-to-GDSII Implementation System
Synopsys Fusion Compiler-Comprehensive RTL-to-GDSII Implementation SystemSynopsys Fusion Compiler-Comprehensive RTL-to-GDSII Implementation System
Synopsys Fusion Compiler-Comprehensive RTL-to-GDSII Implementation System
 
Linux Kernel Crashdump
Linux Kernel CrashdumpLinux Kernel Crashdump
Linux Kernel Crashdump
 
OpenCL Programming 101
OpenCL Programming 101OpenCL Programming 101
OpenCL Programming 101
 
It's Time to ROCm!
It's Time to ROCm!It's Time to ROCm!
It's Time to ROCm!
 

Similar to Computer Fundamentals-2

Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
Kumar Gaurav
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
Harish Kumawat
 

Similar to Computer Fundamentals-2 (20)

Lecture01-Introduction-to-C-programming-converted (4).pptx
Lecture01-Introduction-to-C-programming-converted (4).pptxLecture01-Introduction-to-C-programming-converted (4).pptx
Lecture01-Introduction-to-C-programming-converted (4).pptx
 
Telephone directory in c
Telephone directory in cTelephone directory in c
Telephone directory in c
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
C som-programmeringssprog-bt
C som-programmeringssprog-btC som-programmeringssprog-bt
C som-programmeringssprog-bt
 
C.ppt
C.pptC.ppt
C.ppt
 
Telephone directory
Telephone directoryTelephone directory
Telephone directory
 
Brief introduction to the c programming language
Brief introduction to the c programming languageBrief introduction to the c programming language
Brief introduction to the c programming language
 
C_Intro.ppt
C_Intro.pptC_Intro.ppt
C_Intro.ppt
 
Basics of C Prog Lang.pdf
Basics of C Prog Lang.pdfBasics of C Prog Lang.pdf
Basics of C Prog Lang.pdf
 
CocomoModels MGK .ppt
CocomoModels MGK .pptCocomoModels MGK .ppt
CocomoModels MGK .ppt
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Ums in c
Ums in cUms in c
Ums in c
 
01 c
01 c01 c
01 c
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
Paradigms
ParadigmsParadigms
Paradigms
 
Student record
Student recordStudent record
Student record
 
Presentation 2.ppt
Presentation 2.pptPresentation 2.ppt
Presentation 2.ppt
 
IP Lab Manual for Kerala University 3 Year UG Programme
IP Lab Manual for Kerala University 3 Year UG ProgrammeIP Lab Manual for Kerala University 3 Year UG Programme
IP Lab Manual for Kerala University 3 Year UG Programme
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
 

More from Christalin Nelson

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
Christalin Nelson
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
Christalin Nelson
 
Relational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdfRelational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdf
Christalin Nelson
 
Data Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdfData Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdf
Christalin Nelson
 

More from Christalin Nelson (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
Relational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdfRelational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdf
 
Data Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdfData Modeling - Enhanced ER diagrams & Mapping.pdf
Data Modeling - Enhanced ER diagrams & Mapping.pdf
 
Data Modeling - Entity Relationship Diagrams-1.pdf
Data Modeling - Entity Relationship Diagrams-1.pdfData Modeling - Entity Relationship Diagrams-1.pdf
Data Modeling - Entity Relationship Diagrams-1.pdf
 
Overview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdfOverview of Databases and Data Modelling-2.pdf
Overview of Databases and Data Modelling-2.pdf
 
Overview of Databases and Data Modelling-1.pdf
Overview of Databases and Data Modelling-1.pdfOverview of Databases and Data Modelling-1.pdf
Overview of Databases and Data Modelling-1.pdf
 
Packages and Subpackages in Java
Packages and Subpackages in JavaPackages and Subpackages in Java
Packages and Subpackages in Java
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 
Applications of Stack
Applications of StackApplications of Stack
Applications of Stack
 
Storage system architecture
Storage system architectureStorage system architecture
Storage system architecture
 
Data Storage and Information Management
Data Storage and Information ManagementData Storage and Information Management
Data Storage and Information Management
 
Application Middleware Overview
Application Middleware OverviewApplication Middleware Overview
Application Middleware Overview
 
Network security
Network securityNetwork security
Network security
 
Directory services
Directory servicesDirectory services
Directory services
 
System overview
System overviewSystem overview
System overview
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Computer Fundamentals-2

  • 2. At a Glance • Generation of Programming Languages • Programming Paradigms • Structure & Execution Environment of a Basic C Program • Need of Translators, Linkers, Locaters, Loaders & Editors • Software Engineering & Problem Solving Methods • Few Inclusions from C99 and C11 Standards 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 2 of 74
  • 4. Generations • Generation -1: Machine Level Programming Languages (late 1940s) • Generation -2: Assembly Languages (early 1950s) • Generation -3: High-Level Languages (mid 1950s to present) • Generation -4: Specification/Query Language, Report Generators (1970s to present) • Generation -5: Artificial Intelligence 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 4 of 74
  • 5. Features of Languages (1/2) • Machine Level Language – Fast and efficient, executed directly on the CPU – Consists only of 0s and 1s – Difficult for humans to read, write, and debug • Assembly Language – Also included Interpreting Routines & early Compilers – Simple mnemonic instructions <opcode> <operands> – Assembler translates into machine code – Hand coding in assembly only for low-level needs 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 5 of 74
  • 6. Features of Languages (2/2) • High Level Languages – Easier for humans to read, write, debug – Compiler translates into machine code before running – Interpreter translates into machine code at runtime – Examples: FORTRAN, LISP, COBOL, ALGOL (Ada, Basic, C, C++, Java, Pascal, Smalltalk, …) • Fourth Generation – Examples: Maple, Mathematica, Postscript, SPSS, SQL • Fifth Generation – Examples: Prolog 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 6 of 74
  • 7. Family Tree of Languages 01-Sep-2017 Fortran BASIC Cobol LISP Scheme ML Prolog PL/1 Algol 60 Algol 68 Pascal Modula 3 Ada C C++ Simula Smalltalk Java Dylan Ruby Perl PythonC# CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 7 of 74
  • 9. Procedural Paradigm • Procedures, sequential execution of code are basic building blocks of program. • Examples – FORTRAN (FORmula TRANslating; John Backus, IBM, 1950s) – ALGOL (ALGOrithmic Language, 1958) – COBOL (COmmon Business Oriented Language, 1960) – BASIC (Beginner's All-purpose Symbolic Instruction Code, John Kemeny and Thomas Kurtz, Dartmouth, 1963) – Pascal (Niklaus Wirth, 1970) – C (Dennis Ritchie, Bell Labs, 1972) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 9 of 74
  • 10. Object Oriented Paradigm • Program is designed around the objects and classes required to solve the problem. • Examples – Smalltalk (Alan Kay, Xerox PARC, 1971) – Ada (US Dept of Defense, 1975) – C++ (Bjarne Stroustrup, Bell Labs, 1983) – Java (James Gosling, Sun Microsystems, 1995) – C# (Microsoft, 2000) 01-Sep-2017 Alan Kay B. Stroustrup J. Gosling CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 10 of 74
  • 11. Functional Paradigm • Program is designed around the evaluation of functions, rather than modifying state • Examples – LISP (John McCarthy, MIT, 1958) – Common Lisp – Dylan – Logo – Scheme – ML (Robin Milner et al, Edinburgh, 1970s) – Haskell (purely functional language, 1990) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 11 of 74
  • 12. Logic & Scripting Paradigms • Logic – Program is declarative, based on mathematical logic – Example: Prolog (1972) • Scripting – Used for text processing, shells, HTML, CGI – Examples • awk (Aho, Weinberger, Kerningham, Bell labs, 1978) • Perl (Larry Wall, NASA, 1987) • Tcl/Tk (John Ousterhout, 1988) • Python (Guido van Rossum, CWI, 1991) • PHP (Rasmus Lerdorf, 1995) • Ruby (Yukihiro Matsumoto, 1996) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 12 of 74
  • 15. History of C Language • Kenneth Thompson and Dennis Ritchie at Bell Labs in 1972 • History – International Group, ALGOL (1960) – Martin Richards, BCPL (1967) – Ken Thompson, B (1970) – Dennis Ritchie, Traditional C (1972) – Kernighan & Ritchie, K&R C (1978) – Standardization (incompatible code of different compilers) • In 1983, ANSI (American National Standard Institute) started defining a standard for C called ANSI-C. ANSI-C was adopted by ISO in 1990 renamed to C89 (Officially ANSI/ISO 9989:1990) • C99 (Officially ISO/IEC 9899:1999) • C11 (Officially ISO/IEC 9899:2011) 01-Sep-2017 Brian Kernighan and D. Ritchie CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 15 of 74
  • 16. C Language • High Level Procedural Language • Features – Coded routines of UNIX OS – Concise syntax – Programs a Efficient and Fast – Highly portable – Case-sensitive – No built-in operations for handling composite data types such as strings, sets, and lists • Suited for writing both System software and Business packages (E.g. C Compilers) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 16 of 74
  • 17. Basic Structure of C Program Documentation Section (Comments) Link Section (Link functions from System Library) Definition Section (Define Symbolic Constants) Global Declaration Section (Global Variables & User Defined Fn.) main() Function Section { Declaration Part Executable Part } Subprogram Section User-Defined Functions 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 17 of 74
  • 18. Sample C Program • Problem: Sum of two Numbers //my first program to explain the structure #include <stdio.h> //preprocessor directive void main() //Program Entry: main() fn. { //Start of Program int a, b, sum; //declaration of variables printf(“Enter value of a & b: ”); //Output: printf() built-in fn. scanf(“%d %d”, &a, &b); //Input: scanf() built-in fn. sum = a + b; //Arithmetic expression/stmt. printf(“Sum: %d”, sum); } //End of Program 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 18 of 74
  • 19. Preprocessor Directives • Provides instruction to the compiler before the actual program execution begins • Hash symbol (#) • Types (partial list) – # include <headerfile.h> • Specify header file which holds prototype definition of built-in functions. Compiler informs linker to link these files with program • Example: #include <stdio.h> – “stdio.h” contains prototype definition of printf() and scanf() – # define const_name const_value • Defines Symbolic constants • Example: # define PI 3.14 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 19 of 74
  • 20. Function Definition • Definition: Function/Procedure/Sub-routine – Set of Instructions that are executed to achieve a particular task – Code reusability • Types – Built-in Functions (System Defined) • E.g. printf(), scanf() – User-defined Functions • Syntax return_type function_name(Argument list) { //fn. Definition starts Set of Instructions //fn. body } //fn. Definition ends 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 20 of 74
  • 21. Program Input • scanf() function – Input from Standard Input – Prototype definition available in stdio.h • E.g. scanf(“%d %d”, &a, &b); – Format String • Specifies the expected data type format of the input variables using corresponding format specifiers – Address-of Operator (&) • Provides the memory address of the input variable were the input value is to be stored 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 21 of 74
  • 22. Program Output • printf() function – Output to Standard Output – Prototype definition available in stdio.h – Format • Print a String – E.g. printf(“Hello”); //Output String “Hello” • Print a value within a String – E.g. printf(“Sum: %d”, sum); //Value of sum is printed with String – Format specifier (E.g. %d) » Specifies the expected data type format of the output variable whose value is to be printed within the Output String 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 22 of 74
  • 23. Format Specifiers in C (1/2) Format Specifier printf() scanf() %c Prints a single character Read a character %d Prints a signed decimal integer Read a signed decimal integer %i Prints a signed decimal integer Read a signed integer depending upon the prefix - a hexadecimal integer if prefix is 0x - a signed octal if prefix is 0 - if no prefix read signed decimal integer %u Prints an unsigned decimal integer Unsigned decimal integer %o Prints an unsigned octal integer Unsigned octal integer %x Prints an unsigned hexadecimal using a,b,c,d,e,f Read unsigned hexadecimal integer %X Prints an unsigned hexadecimal using A,B,C,D,E,F 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 23 of 74
  • 24. Format Specifiers in C (2/2) Format Specifier printf() scanf() %f A floating point with 6 digits after decimal point Read floating point number %e Floating point in exponential format with e Read floating point number %E Floating point in exponential format with E %g Float in %f or %e whichever is shorter Floating point number %G Float in %f or %E whichever is shorter %s String Read a string (till null character) %% Prints % sign Not applicable %p Prints address in hexadecimal format Not applicable 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 24 of 74
  • 25. Program Execution (1/7) 01-Sep-2017 Preprocessing Program Compilation Linking Locating • Steps involved Loading Program Execution CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES Program Creation Source Code (*.c) Expanded Source Code (*.i) Object Code Executable Object Code Assembly Code 25 of 74
  • 26. Program Execution (2/7) • Preprocessor Stage – The following occurs to the Source code • Header Files are expanded • Comments are removed • Macros are replaced – To print the expanded code on terminal after preprocessing, the command in Linux is • $cc -E file_name.c – To obtain the expanded code in a file the command in Linux is • $cc -E file_name.c -o file_name.i – After above said command is executed, a file “file_name.i” is generated in present working directory and whole code obtained after pre-processing stage is stored in this file. This file can be opened for viewing the whole content. 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 26 of 74
  • 27. Program Execution (3/7) • Compilation Stage – Considered to include (1) Compiler Stage & (2) Assembler Stage • Compiler Stage – At compilation stage an assembly code (with .s as extension) is generated on the basis of Source code i.e. depending upon the functions and structures used in the C program. – To obtain the assembly code the foll. commands can be used • $cc –S file_name.c OR $cc –S file_name.i • Note: Assembler accept files with .i or .c extension – The obtained file name can be specified as follows: • $cc –S file_name.i -o file_name.s – Note: The contents of .s file are not understandable but can be viewed 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 27 of 74
  • 28. Program Execution (4/7) • Assembler Stage – Assembler converts the assembly code to an object code (machine code) with .obj(windows) or .o(Linux) extension. – The commands are • $cc –c file_name.c or $cc –c file_name.i or $cc -c file_name.s – The obtained file name can be specified as follows: • $cc –c file_name.s –o file_name.o – Note: The contents of object file are not understandable but can be viewed 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 28 of 74
  • 29. Program Execution (5/7) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 29 of 74
  • 30. Program Execution (6/7) • Linker Stage – At linker stage other object files (E.g. Library Files) are linked and an executable file with .exe(windows) or a.out(Linux) is generated. • Library files are pre-compiled files which can be directly linked – Command • $gcc file_name.c – Note: • Executing above command generates a file with name a.out which is the executable format of last compiled c-program. • Every time a new file is compiled the a.out is replaced with new executable file • To retain the executable content, the obtained file name can be specified as follows $ gcc file_name.c -o file_name 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 30 of 74
  • 31. Program Execution (7/7) • To run the executable place ./ preceding to file_name – $./a.out or $./file_name • Note – To get all the files in one shot the following command can be used • $gcc –save-temps file_name.c -o file_name 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 31 of 74
  • 32. Program Execution & Errors 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 32 of 74
  • 34. Editor • Software used to create and modify source programs – Has commands that change, delete or insert lines/characters • Available with Assembly Language programming and High level language programming • Example – Linux OS: vi, gedit, nano, pico, vim – Windows OS: notepad 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 34 of 74
  • 35. Translators • Software that translates the user program into the machine readable/understandable program • Types – Assemblers – Compilers – Interpreters 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 35 of 74
  • 36. Assembler • Software that translates Assembly language program into Machine language program • Reads the source program more than once • Creates two files after translation – Object File (Binary code of Instruction & Information about Instruction Addresses) – Assembler List File (Assembly language statements, Binary code for each Instruction & Offset for each instruction) • Types – Self/Resident Assembler & Cross Assembler – One-pass & Two-pass Assembler – Meta Assembler – Macro Assembler 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 36 of 74
  • 37. Compiler (1/6) • Software that translates High level language program into Machine language program • Compiler goes through the entire program – then translates the entire program into machine codes • Types – Self or Resident Compiler – Cross Compiler • Vs. Assembler – A compiler is more intelligent – It checks all kinds of limits, ranges, errors etc. – Program run time is more & It has slow speed01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 37 of 74
  • 38. Compiler (2/6) Steps involved during Compilation 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 38 of 74
  • 39. Compiler (3/6) • Lexical Analysis or Tokenizer – Produces Tokens from Source Code, Symbol Table and Literal Table • E.g. Seeing a variable, the tokenizer would generate the token “var” and then associate the variable name (lexeme) with it in the symbol table. – Removes whitespace and comments. – Handles compiler directives (i.e., as a preprocessor). 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 39 of 74
  • 40. Compiler (4/6) • Syntactic Analysis – Tokens are parsed to produce Parse Tree while checking that tokens conform with the rules of the language. – Note: Parse trees are often converted into a simplified form known as a Syntax tree. • E.g. Consider expression 3+4*5 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 40 of 74
  • 41. Compiler (5/6) • Syntactic Analysis – Tokens are parsed to produce Parse Tree while checking that tokens conform with the rules of the language. – Note: • Parse trees are often converted into a simplified form known as a Syntax tree. • Semantic Analysis – Produces Annotated Tree from Syntax tree. – In addition to this, a literal table (contains information on the strings and constants used in the program), and a symbol table (stores information on the identifiers used in the program) are produced by the various stages of the process. 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 41 of 74
  • 42. Compiler (6/6) • Intermediate Code Generator & Code Optimizer – Intermediate code generator receives input from semantic analyzer in the form of an annotated syntax tree. – Need of Intermediate Code Generator instead of direct translation into target machine code? • Intermediate code eliminates the need of a new full compiler for every unique machine by keeping the analysis portion same for all the compilers. The second part of compiler, synthesis, is changed according to the target machine. It is easier to apply the source code modifications to improve code performance by applying code optimization techniques on the intermediate code. • Note: Intermediate code can be either language specific or language independent. • Error handler – Catch errors generated by any stages of the program. 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 42 of 74
  • 43. Interpreter • Software that translates High level language program into Machine language program (same as Compiler) • Vs. Compiler – Translates only one statement of the program at a time and executes it. In this way it proceeds further till all the statements are translated and executed – A compiler is 5 to 25 times faster than an interpreter – Compiler saves the machine codes permanently for future reference. Interpreter does not save the machine codes – An interpreter is a small program as compared to compiler. It occupies less memory space, so it can be used in a smaller system which has limited memory space 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 43 of 74
  • 44. 01-Sep-2017 Flow Chart (Operation of Interpreter vs. Compiler) CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 44 of 74
  • 45. Linker • Software that links built-in functions to the System libraries & user-defined functions to the User-defined libraries – If linker does not find a library of a function then it informs to compiler and then compiler generates an error • Linker is used to combine the program modules for program execution – Modules: Smaller subprograms that together make a longer program • Compiler automatically invokes linker as the last step in program compilation 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 45 of 74
  • 46. Locator • Software that assigns specific addresses at which the Executable Object code should be loaded in the System Memory • Example – IBM-PC DOS has a locator called EXE2BIN 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 46 of 74
  • 47. Loader • Software that loads machine codes (executable object code) of a program into the system memory and prepares them for execution – i.e. Reads contents of executable file into memory • Integral part of Operating System (OS) – Once loading is complete, the OS starts the program by passing control to the loaded program code – In many OS, the loader is permanently resident in memory 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 47 of 74
  • 49. Software Engineering • Definition • Need of Paradigms • Types – Classical Life Cycle Model or Waterfall Model – Iterative Model – Rapid Application Development Model (RAD) – Prototyping Model – Spiral Model – V Model • CASE Tools 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 49 of 74
  • 51. Software Development Life Cycle (SDLC) • (1) Problem Statement • (2) Requirement Gathering & Analysis • (3) Propose Solution & Select an best feasible solution • (4) Design – Problem solving methods (Flow Chart, Algorithm, Pseudo Code) • (5) Test the Design: If Test fails – Repeat from Step(4) • (6) Coding – Choice of Programming Lang. Paradigm – Language Selection • (7) Test the Code: If Test fails – Repeat from Step(6) • (8) Deployment • (9) Maintenance 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 51 of 74
  • 53. Flow Chart Notation (1/3) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 53 of 74
  • 54. Flow Chart Notation (2/3) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 54 of 74
  • 55. Flow Chart Notation (3/3) 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 55 of 74
  • 57. Program Execution using Flow Chart (1/2) 01-Sep-2017 System Ready Enter ProgramProgram Code Edit Source Program Compile Source Program C Compiler Syntax Errors? A Source Program Object Code B Logic Error Syntax Error No Errors CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 57 of 74
  • 58. Program Execution using Flow Chart (2/2) 01-Sep-2017 Stop System Library Link with System Library Execute Object Code Input Data Correct Output Logic & Data Errors? Object Code A Executable Object Code No Errors Data Error B Logic Error CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 58 of 74
  • 59. Algorithm • Algorithm – Set of operations to be performed Step-by-step. – Written in natural language. – Starting from an initial state and initial input the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 59 of 74
  • 60. Example - 1 • Problem: Snakes and Ladders Game 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 60 of 74
  • 61. 01-Sep-2017 Problem: Snakes and Ladders Game Flow Chart CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 61 of 74
  • 62. Example – 1 (contd.) • Problem: Snakes and Ladders Game – Algorithm • (1) Game begins • (2) Roll the dice • (3) Move ahead as many steps as the number on the dice – Is there a ladder? If YES, climb it & goto step 5. If NO goto step 4 • (4) Is there a snake? – If YES, go down & goto step 6 – If NO, remain where your are & goto step 5 • (5) Have you reached End? – If YES, You have won the game. Goto step 7 – If NO, goto step 6 • (6) Next player's turn – goto step 2 • (7) Game Over 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 62 of 74
  • 63. Example - 2 • Problem: Find Sum of two Numbers – Algorithm • (1) Start Operation • (2) Declare integer variable A, B, Sum • (3) Read two numbers A, B • (4) Find Sum = A+B • (5) Print Sum • (6) End Operation 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 63 of 74
  • 64. 01-Sep-2017 Problem: Finding sum of two numbers Flow Chart Testing the Flow Chart CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 64 of 74
  • 65. Pseudocode • Written in a format that is closely related to high level programming language structures. • Uses syntax common to many programming languages. – These include standard looping structures like FOR … ENDFOR, WHILE…ENDWHILE – There are also some terms for standard conditional clauses like IF-THEN … ENDIF, WHILE … ENDWHILE (this is both a loop and a conditional clause by the way), CASE … ENDCASE • Transforming pseudocode to programming code could be much easier than converting an algorithm written in natural language. 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 65 of 74
  • 66. Example • Problem: Accept two numbers from the keyboard and calculate the sum and product. – Pseudocode • Use variables sum, product, number1, number2 of type real • Display “Input two numbers” • Accept number1, number2 • sum = number1 + number2 • Print “The sum is “, sum • product = number1 * number2 • Print “The Product is “, product • End program 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 66 of 74
  • 67. Program Control Flow • Order of Execution of Instructions • Types – Sequence (Default: Top to Bottom) – Loop • Iteration & Recursion – Branching • Conditional & Un-Conditional Branching or Jump 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 67 of 74
  • 68. Activity-3 • Solve the following problems with a suitable formulae/logic and provide the design using Algorithm and Flow chart. – (1) Find the maximum of three numbers. – (2) Find area and circumference of a Circle. – (3) Convert temperature into Centigrade/Fahrenheit/Kelvin. – (4) Find Simple Interest. – (5) Swap two numbers with/without using third variable. – (6) Find the given number is Even or Odd. – (7) Find the given number is Prime number or not. – (8) Check whether the given year is Leap year or not. – (9) Multiply two numbers without using *operator. 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 68 of 74
  • 69. Tips to Solve Activity-3 • Area = π x radius2 Circumference = π x d (d = 2r) • °F = °C x (9/5) + 32, °C = (°F - 32) x (5/9), K = °C + 273.15 • Simple Interest = Principal x Duration x Interest Rate • Swapping using temporary variable – temp = a; a = b; b = temp; • Swapping without temporary variable – Method 1: x = x + y; y = x - y; x = x - y; – Method 2: x = x * y; y = x / y; x = x / y; – Method 3: x = x ^ y; y = x ^ y; x = x ^ y; 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 69 of 74
  • 71. Some Inclusions from C99 • Variables can be defined anywhere inside the function block • New Data Types – Boolean data type “bool” used with “stdbool.h” and “_Bool” • Constants: “true” and “false”. • Boolean values still behave as integers (Can be stored in integer variables, and used anywhere integers would be valid) – Complex data type “complex” used with “complex.h” • Functions: creal(), cimag(), conj() • Support to “static inline” functions • Universal character names allows user variables to contain other characters than the standard character set which is its Unicode value in the form: uXXXX or UXXXXXXXX – XXXX or XXXXXXXX - Unicode code point in hexadecimal notation 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 71 of 74
  • 72. Inclusions from C11 • Use of Anonymous Structure and Anonymous Union when nested inside another structure. – i.e. Structure/Union without an Identifier • Caution with usage of gets(), deprecated in the previous C language standard revision, ISO/IEC 9899:1999/Cor.3:2007(E) – Using gets() may lead to buffer overflow. Hence, fgets() and gets_s() are preferred. 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 72 of 74
  • 73. Peep into the next Module • Programming using C Language - I – Programming Fundamentals – Literals & Variables – Data Types & Modifiers – Qualifiers & Constants – Storage Class Specifiers – Operators & Expressions – Data Type Conversions – Loops & Decision Making 01-Sep-2017 CSEG1001/1002 Instructor: Mr.S.Christalin Nelson|SoCSE|UPES 73 of 74