KEMBAR78
Intro Unit1 Lang | PDF | Programming | Computer Program
0% found this document useful (0 votes)
17 views47 pages

Intro Unit1 Lang

The document provides an introduction to computer programming, covering programming languages, including machine, assembly, and high-level languages, as well as their features and limitations. It focuses on the C programming language, its structure, character set, variable assignment, and the process of compiling and executing programs. Additionally, it discusses procedural and non-procedural languages, along with the roles of compilers, interpreters, and linkers in program execution.

Uploaded by

astudent0072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views47 pages

Intro Unit1 Lang

The document provides an introduction to computer programming, covering programming languages, including machine, assembly, and high-level languages, as well as their features and limitations. It focuses on the C programming language, its structure, character set, variable assignment, and the process of compiling and executing programs. Additionally, it discusses procedural and non-procedural languages, along with the roles of compilers, interpreters, and linkers in program execution.

Uploaded by

astudent0072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Introduction to Computer

Programming
UNIT 1
Introduction to Programming Languages: Introduction to Machine level
language, Assembly language, High- level language, Limitations and
Features - Classification of Computer Language -
Procedural Language and Non- Procedural Language.
Introduction of C Language: History of C, Basic Structure of C,
Executing C program, Character set & C Tokens, Identifiers &
Keywords, Data Types, Storage Class, Constants and Variables, Type
Casting, Comments
Console based I/O and related built-in I/O function: Formatted functions:
printf(), scanf() -
Unformatted functions: getch(), getchar(), putchar(), getche( ), putch(),
gets(), puts() -
Concept of Header files and #include, #define
Operators & Expression: Types of Operators and Expression,
Precedence & Associativity -
Decision Making Structure-If, If-else, Nested If-else, Switch
Outline of Topics
• Hardware/Software interface
– Layers of the Machine
– Kinds of Software
• Computer Languages
• Syntax, Semantics, Grammars
• What happens to your program?
– Compilation, Linking, Execution
– Program errors
• Compilation vs. Interpretation etc.
Software Categories

• System SW
– Programs written for computer systems
• Compilers, operating systems, …

• Application SW
– Programs written for computer users
• Word-processors, spreadsheets, & other
application packages
A Layered View of the Computer
Application Programs
Word-Processors, Spreadsheets,
Database Software, IDEs,
etc…
System Software
Compilers, Interpreters,Preprocessors,
etc.
Operating System, Device Drivers
Machine with all its hardware
Operating System (OS)
∙ Provides several essential services:
– Loading & running application programs
– Allocating memory & processor time
– Providing input & output facilities
– Managing files of information
Programs
• Programs are written in programming languages
– PL = programming language
– Pieces of the same program can be written in different
PLs
• Languages closer to the machine can be more efficient
• As long as they agree on how to communicate
• A PL is
– A special purpose and limited language
– A set of rules and symbols used to construct a
computer program
– A language used to interact with the computer
Computer Languages
– Machine Language
• Uses binary code[0 and 1]
• Machine-dependent
• Not portable, difficult, time consuming, prone to errors
• Assembly Language
– Uses mnemonics-simple codes like ADD, SUB..etc
– Machine-dependent
– user needs to remember codes and registers of CPU
– Not usually portable
• High-Level Language (HLL)
– Uses English-like language, fast program
development
– Machine independent
– Portable (but must be compiled for different platforms)
– easy testing and debugging.
– Examples: Pascal, C, C++, Java, Fortran, . . .
Machine Language
• The representation of a computer program which is
actually read and understood by the computer.
– A program in machine code consists of a sequence of machine
instructions.
• Instructions:
– Machine instructions are in binary code
– Instructions specify operations and memory cells involved in the
operation
Example: Operation Address

0010 0000 0000 0100


0100 0000 0000 0101

0011 0000 0000 0110


Assembly Language
• A symbolic representation of the machine language of a
specific processor.
• Is converted to machine code by an assembler.
• Usually, each line of assembly code produces one
machine instruction (One-to-one correspondence).
• Programming in assembly language is slow and
error-prone but is more efficient in terms of hardware
performance.
• Mnemonic representation of the instructions and data
• Example:
Load Price
AddTax
Store Cost
High-level language
• A programming language which use statements
consisting of English-like keywords such as "FOR",
"PRINT" or “IF“, ... etc.
• Each statement corresponds to several machine
language instructions (one-to-many correspondence).
• Much easier to program than in assembly language.
• Data are referenced using descriptive names
• Operations can be described using familiar symbols
• Example:
Cost := Price + Tax
Syntax & Semantics
• Syntax:
– The structure of strings in some language. A
language's syntax is described by a grammar.
– Examples:
• Binary number
<binary_number> = <bit> | <bit> <binary_number>
<bit> =0|1
• Identifier
<identifier> = <letter> {<letter> | <digit> }
<letter> =a|b|...|z
<digit =0|1|...|9
• Semantics:
– The meaning of the language
Syntax & Grammars
• Syntax descriptions for a PL are
themselves written in a formal language.
– E.g. Backus-Naur Form (BNF)
• The formal language is not a PL but it can
be implemented by a compiler to enforce
grammar restrictions.
• Some PLs look more like grammar
descriptions than like instructions.
Compilers & Programs
• Compiler
– A program that converts another program from
some source language (or high-level
programming language / HLL) to machine
language (object code).
– Some compilers output assembly language
which is then converted to machine language by
a separate assembler.
– Is distinguished from an assembler by the fact
that each input statement, in general,
correspond to more than one machine
instruction.
Compilation into Assembly L

Source Assembly
Program Compiler Language

Assembly Assembler Machine


Language Language
Compilers & Programs
• Source program
– The form in which a computer program,
written in some formal programming
language, is written by the programmer.
– Can be compiled automatically into object
code or machine code or executed by an
interpreter.
– C source programs have extension ‘.c’
Compilers & Programs
• Object program
– Output from the compiler
– Equivalent machine language translation of the
source program
– Files usually have extension ‘.obj’

• Executable program
– Output from linker/loader
– Machine language program linked with necessary
libraries & other files
– Files usually have extension ‘.exe’
What is a Linker?
• A program that pulls other programs together so
that they can run.
• Most programs are very large and consist of
several modules.
• Even small programs use existing code provided
by the programming environment called libraries.
• The linker pulls everything together, makes sure
that references to other parts of the program
(code) are resolved.
Running Programs
• Steps that the computer goes through to run a
program:
Memory

Machine language
program
(executable file)
Input Data Data entered CPU
during execution

Computed results
Program Output
Program Execution
• Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
Program Errors
• Syntax Errors:
– Errors in grammar of the language
• Runtime error:
– When there are no syntax errors, but the program
can’t complete execution
• Divide by zero
• Invalid input data
• Logical errors:
– The program completes execution, but delivers
incorrect results
– Incorrect usage of parentheses
Compilation
Source Target
Program Compiler Program

Input Target Program Output

• Compiler translates source into target (a machine


language program)
• Compiler goes away at execution time
• Compiler is itself a machine language program,
presumably created by compiling some other high-level
program
• Machine language, when written in a format understood
by the OS is object code
Interpretation

Source
Program

Interpreter Output

Input

• The interpreter stays around during execution


• It reads and executes statements one at a time
Compilation vs. Interpretation
• Compilation:
– Syntax errors caught before running the program
– Better performance
– Decisions made once, at compile time
• Interpretation:
– Better diagnostics (error messages)
– More flexibility
– Supports late binding (delaying decisions about
program implementation until runtime)
• Can better cope with PLs where type and size of
variables depend on input
– Supports creation/modification of program code on
the fly (e.g. python,Lisp, Prolog)
Mixture of C & I
Source Intermediate
Program Translator Program

Intermediate
Program
VM Output
Input

• Many programming languages implement this

• Interpreter implements a Virtual Machine (VM).


Procedural language
• The program code is written in the form of a sequence of instructions.
• The user would specify what has to be done and how it can be done, i.e the
step by step procedure of it.
• It is considered as a command-driven language.
• It works with the state of machine.
• Its semantics are tough in comparison to other paradigms.
• The size of the program would be large.
• These steps would be executed in a sequential method.
• It returns restricted data types and certain allowed values only.
• The overall efficiency is high.
• The instructions are written to solve a specific/set of problems.
• Examples of procedural languages include BASIC, FORTRAN, ALGOL, C,
COBOL, and Pascal.
• It is not suited for applications where time is a critical constraint.
• The iterative loops and recursive calls are used while working in procedural
languages.
Non-procedural Language
• The user would specify what has to be done but doesn't get into the how it
has to be done part.
• It is known as an applicative or functional language.
• It involves developing function based on other functions, in order to construct
other complicated functionalities.
• It works with the help of mathematical functions.
• Its semantics are simple in comparison to procedural languages.
• Examples of non-procedural languages include LISP, SQL, PROLOG.
• It is considered as a function-driven language
• It has the ability to return any datatype or value.
• The overall efficiency of non-procedural language is low in comparison to
procedural language.
• The programs are small in size.
• It is well-suited for applications where time is a critical factor.
• Recursive calls are used while working with non-procedural languages
Introduction to C
• C is a programming language developed at AT & T’s
Bell Laboratories of USA in 1972.
• It was designed and written by a man named Dennis
Ritchie.
• In the late seventies C began to replace the more
familiar languages of that time like PL/I, ALGOL, etc
• ANSI C standard emerged in the early 1980s
• Now the software tool as well as the C compiler is
written in C.
• Major parts of popular operating systems like
Windows, UNIX, Linux is still written in C. This is
because even today when it comes to performance
(speed of execution) nothing beats C.
Structure of C Language
program
i. Include Headerfile section
2. global declaration
3. Function main()
4. declaration part
5. Executable part
6. user defined function
7. body of the function
8. comments
Algorithm and Flowchart
An algorithm is a step-by-step set of instructions
for solving a problem, while a flowchart is a visual
representation of those steps using diagrams and
symbols
Character set
The C character set refers to the collection
of valid characters that can be used in the C
programming language to construct source
code.
These characters are the fundamental
building blocks for forming words,
expressions, and numbers within a C
program.
● The C character set primarily includes:
● Alphabets: Both uppercase (A-Z) and lowercase (a-z) English
alphabets.
● Digits: The decimal digits from 0 to 9.
● Special Symbols: A wide range of symbols used for various
purposes, including:
● Mathematical operators (+, -, *, /, %)
● Relational operators (<, >, <=, >=, ==, !=)
● Logical operators (&&, ||, !)
● Punctuation marks (., ,, ;, :, ?, !)
● Parentheses, brackets, and braces (( ), [ ], { })
● Other symbols (#, $, &, @, ^, |, ~, _, `\`)

Whitespace Characters: Characters that represent empty space, such as:
● Space (` `)
● Newline (\n)
● Tab (\t)
Variable Assignment
A variable assignment is a process of assigning a value to a variable.
Example:int width = 60; int age = 31;int AGE;

There are some rules on choosing variable names


A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits
0-9, and the underscore character.
The first character must be a letter or underscore.
Blank spaces cannot be used in variable names.
Special characters like #, $ are not allowed.
C keywords cannot be used as variable names.
Variable names are case sensitive.
Values of the variables can be numeric or alphabetic.
Variable type can be char, int, float, double, or void.
C Program to Print Value of a Variable
Example:
#include<stdio.h>
void main()
{
int age = 33; // c program to print value of a variable
printf("I am %d years old.\n", age);
}

You might also like