0% found this document useful (0 votes)
4 views56 pages

Unit 1

The document provides an overview of programming languages, focusing on the concepts of source code, object code, algorithms, and flowcharts. It explains the differences between algorithms and flowcharts, the structure of C programs, and the roles of editors, compilers, and interpreters in programming. Additionally, it covers data types, tokens, identifiers, keywords, constants, and the rules for defining variables in C.
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)
4 views56 pages

Unit 1

The document provides an overview of programming languages, focusing on the concepts of source code, object code, algorithms, and flowcharts. It explains the differences between algorithms and flowcharts, the structure of C programs, and the roles of editors, compilers, and interpreters in programming. Additionally, it covers data types, tokens, identifiers, keywords, constants, and the rules for defining variables in C.
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/ 56

Unit - 1

• A programming language is a formal language that specifies a set of


instructions for a computer to perform specific tasks. It’s used to write
software programs and applications, and to control and manipulate computer
systems. There are many different programming languages, each with its own
syntax, structure, and set of commands. Some of the most commonly used
programming languages include Java, Python, C++, JavaScript, and C#.
1.1 Introduction of Source Code, Object Code and executable code
• Source code refers to high level code or assembly code which is generated by
human/programmer. Source code is easy to read and modify. It is written by
programmer by using any High Level Language or Intermediate language
which is human-readable. Source code contains comments that programmer
puts for better understanding.
• Source code is provided to language translator which converts it into machine
understandable code which is called machine code or object code. Computer
can not understand direct source code, computer understands machine code
and executes it. It is considered as fundamental component of computer.
• In simple we can say source code is a set of instructions/commands and
statements which is written by a programmer by using a computer
programming language like C So statements written in any programming
language is termed as source code.
2. Object Code:
Object code refers to low level code which is understandable by machine.
Object code is generated from source code after going through compiler or
other translator. It is in executable machine code format. Object code contains
a sequence of machine understandable instructions to which Central
Processing Unit understands and executes.
Algorithm
• An algorithm is a step-by-step method of solving a problem. An algorithm is a
procedure or formula for solving a problem.

• To write a logical step-by-step method to solve the problem is called an


algorithm, in other words.

• In order to solve a mathematical or computer problem, this is the first step of


the procedure.

• An algorithm includes calculation, reasoning, and data processing.

• Algorithms can be presented by natural language like English.


Advantages of Algorithms:
• It is easy to understand.
• An algorithm is a step-wise representation of a solution to a given problem.
• In an Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual program.
Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-consuming.
• Understanding complex logic through algorithms can be very difficult.
• Branching and Looping statements are difficult to show in Algorithms.
Algorithm has the following characteristics:

• Input: An algorithm may or may not require input

• Output: Each algorithm is expected to produce at least one result

• Definiteness: Each instruction must be clear and unambiguous.

• Finiteness: If the instructions of an algorithm are executed, the algorithm should


terminate after a finite number of steps.

• Independent: An algorithm should have step-by-step directions, which should be


independent of any programming code.
The algorithm and flowchart include the following three types of control
structures:
1. Sequence: In the sequence structure, statements are placed one after the
other and the execution takes place starting from up to down.

2. Branching (Selection): In branch control, there is a condition and according to


a condition, a decision of either TRUE or FALSE is achieved. In the case of
TRUE, one of the two branches is explored; but in the case of the FALSE
condition, the other alternative is taken. Generally, the ‘IF-THEN’ is used to
represent branch control.

3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be


executed repeatedly based on certain loop conditions e.g. WHILE, FOR loops.
The following algorithm for making cup of tea :-

1. Start
2. Put the teabag in a cup
3. Fill the kettle with water
4. Boil the water in the kettle
5. Add milk to the cup
6. Add sugar to the cup
7. Stir the tea
8. Drink the tea
8. Stop
-> Like this, we can write an algorithm for any logic
Some more examples of an algorithm :

• Print Hello World.


Step 1. Start
Step 2. Print "Hello World”
Step 3. Stop

• Add two numbers.


Step 1. Start
Step 2. Declare Variable num1,num2,and sum
Step 3. Read value num1 and num2
Step 4. Add num1 and num2 and assign the result to sum
sum  num1+num2
Step 5. Display sum
Step 6. Stop
FlowChart

• A flowchart is the graphical or pictorial representation of an algorithm with the


help of different symbols, shape and arrows in order to show a process or a
program.
• With an algorithm, we can easily understand a program.
• The main purpose of a flowchart is to analyse different processes.
Several standard graphics are applied
in a flowchart :
• Terminal Box-Start/End
Used to represent start and end
of flowchart

• Input/Output
Used for input and output operation

• Process/Instruction

Processing: used for arithmetic operations and


data manipulation
• Decision
Used to represent the operation in which
there are one/two alternatives, true and
false etc.

• Connector/Arrow

Flow line is used to indicate the


flow of logic by connecting
symbols
Advantages of flowchart:
• Flowchart is an excellent way of communicating the logic of a program.

• Easy and efficient to analyze the problem using a flowchart.

• During the program development cycle, the flowchart plays the role of a blueprint,
which makes the program development process easier.

• After the successful development of a program, it needs continuous timely


maintenance during the course of its operation. The flowchart makes program or
system maintenance easier.

• It is easy to convert the flowchart into any programming language code.


Flowchart to Add two numbers :
Start

Input
Number1,Number2

Sum=Number1+Number2

Print Sum

End
• Find A Simple Interest Algorithm
Step 1:Start
Step 2:Read Principle Amount, rate of interest, and time period
Step 3:Calculate Simple Interest
Simple Interest --- (Principle amount x rate of interest x
time period)/100
START
Step 4:Display Simple Interest
Step 5:Stop Input P,R,T

• Flowchart Calculate
I = (P*R*T)/100

Display = I

End
• Algorithm to Check Positive Number
Step 1:Start
Step 2: Read Number
Step 3:If Number >0
Step 4:Print Positive Number
Step 5:Stop START

Input Number

IF Number>0
No
Yes

Print Positive
Number

End
• Algorithm to check even or odd number
Step 1:Start Start
Step 2: Read Number
Step 3:If Number %2 == 0 Input Number
Step 4:Print Even Number
Step 3:Else Print Odd Number
If Number %
Step 5:Stop Yes 2 ==0 No
• Flowchart
Print Even Print Odd
Number Number

End
• Difference between Algorithm and Flowchart :

Algorithm Flowchart
Algorithm is step by step procedure to Flowchart is a diagram created by
solve the problem. different shapes to show the flow of data.
Algorithm is complex to understand. Flowchart is easy to understand.
In algorithm plain text are used. In flowchart, symbols/shapes are used.
Algorithm is easy to debug. Flowchart it is hard to debug.
Algorithm is difficult to construct. Flowchart is simple to construct.
Algorithm does not follow any rules. Flowchart follows rules to be constructed.
Algorithm is the pseudo code for the Flowchart is just graphical representation
program. of that logic.
Flow Of A C Program :
Character Set :
• As every language contains a set of characters used to construct words,
statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a total of
256characters.

• Every C program contains statements. These statements are constructed using


words and these words are constructed using characters from C character set.
C language character set contains the following set of characters...
1.Alphabet
2.Digits
3.Special Symbols
Tokens :

• In C Language, the smallest individual units are called tokens.

• C tokens can be classified as follows:

Identifiers
Keywords
Constants
Operators
String
Special Characters
Identifiers :
• Each program element in a C program is called an identifier. An identifier is a
variable that holds a value and stores it in the memory.

• Rules for declaring identifiers:


1.The identifier should consist of alphabets from a to z and A to Z.

2.It may contain the underscore value.

3.The starting character of an identifier must always be a letter. Example:


Variables, functions, labels, etc.
KEYWORDS :
• Reserved Words That have special meaning to the Compiler.

• 33 Keywords in C

• The keywords cannot be used as variable names because by doing so,we


are trying to assign a new meaning to the keyword which is not allowed.
auto else long switch

break enum register typedef

case extern return union

char float short unsigned

const for signed void

continue goto sizeof volatile

default if static while

do int struct _Packed

double
Constants :
• A constant is a fixed value that cannot be altered during the
execution of a program.
• Values that don’t change (fixed).
Types

Integer Character
Constant Real Constant
1,2,3,0,-1,-2 Constant ‘a’, ‘b’, ‘A’,
1.0,2.0,3 ‘#’, ‘&’
.14,
-0.956
Special Characters :
• All the characters other than a to z, A to Z, and 0 to 9 are special
characters.
• Example: {},[],(),#,@,$,%,^,*.

String:
• A string is a group of characters that should be enclosed in double
quotations.
• For example: char string[]=“hello”; ,”abc85”;
WHAT IS A VARIABLE?

• A variable is the name of a memory location which store some data.

a b Memory
25 s
• Declared by writing type variable_name;

• Initialized and declared by type variable_name = value;


RULES FOR DEFINING A VARIABLE IN C

A variable name can contain alphabets, digits, and underscore (_) only.

The starting letter can not be a digit, A variable name can start with the
alphabet and underscore only

White spaces cannot be used.


The name should not be a reserved keyword or special
character.

We can declare and assign value to a variable in two ways int =a;
Int a; a = 12;

Valid variable names : int age; float price ;char _binal37;

Invalid variable names : $sachin; int 77sachin;char long;


DATA TYPES :
• Basic Data Type : int, char, float,double.

• Derived data Type : array, pointer,structure,union.

• Enumeration Type : enum

• Void Data Type : void


DATA TYPES :
DATA TYPE MEMORY (BYTES)
Char 1
signed char 1
unsigned char 1
short int 2
unsigned short int 2
unsigned int 4
int 2
long int 4
unsigned long int 4
float 4
double 8
long double 10
Instructions

• This are Statement in a program.

Type Declaration Control


Instruction Arithmetic Instruction
Instruction
• Type Declare Instruction :

int a=2;
int b=a;
int c=b+1;
int d=5,e;

int a,b,c;
int a = b = c = 1;
Comments:
• Lines that are not part of the program.

Single Line Multiple Line

// /*
*/
FOR EXAMPLE :
// Write A Program Hello World//
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello World\n”);
getch();
}
Output :
Hello World
WRITE A ALGORITHM TO PRINT THE FIBONACCI SERIES
STEP 1. START
STEP 2.Declare Variable i , a, b, show
a = 0 , b = 1 , show = 0
STEP 3.Enter the numbers of terms of Fibonacci Series (i)
STEP 4.if i < 2 then display message “Enter Number >2”,and go to STEP 8
STEP 5.Print the first two terms of the Fibonacci series
STEP 6.using loop show = a + b
a=b
b = show
increase value each time
until show < i-2
STEP 7.Print value of show
STEP 8.STOP
Concept of Editor, Interpreter, and compiler
• A language translator is software that translates the program from a source language that is
in human-readable form into an equivalent program in an object language.
• The Source language is generally a high-level programming language, and the object
language is typically the machine language of an actual compiler.
 Compiler :
• A compiler is a computer program that transforms code written in a high-level programming
language into the machine code.

• It is a program that translates the human-readable code to a language a computer processor


understands (binary 1 and 0 bits).

• The compiler is only a program and can not fix errors found in that program. So, if you make a
mistake, you need to make changes in the syntax of your program. Otherwise, it will not
compile.
Role of Compiler

• Compliers reads the source code, output executable code

• Translates software written in a higher-level language into instructions


that computer can understand. It converts the text that a programmer
writes into a format the CPU can understand.

• The process of compilation is relatively complicated. It spends a lot of


time analyzing and processing the program.

• The executable result is some form of machine-specific binary code.


Interpreter :

• An interpreter is a computer program, which converts each high-level


program statement into machine code. This includes source code, pre-
compiled code, and scripts.

• Both compiler and interpreters do the same job which is converting high-
level programming language to machine code. However, a compiler will
convert the code into machine code (create an exe) before program run.

• Interpreters convert code into machine code when the program is run.
Role of Interpreter

• The interpreter converts the source code line-by-line during RUN Time.

• Interpreter completely translates a program written in a high-level


language into machine-level language.

• Interpreter allows evaluation and modification of the program while it is


executing.

• Relatively less time spent for analyzing and processing the program

• Program execution is relatively slow compared to compiler


Difference Between Compiler and Interpreter
Interpreter Compiler

Translates program one statement at a Scans the entire program and translates
time. it as a whole into machine code.

Interpreters usually take less amount Compilers usually take a large amount
of time to analyze the source code. of time to analyze the source code.
However, the overall execution time is However, the overall execution time is
comparatively slower than compilers. comparatively faster than interpreters.

Generates Object Code which further


No Object Code is generated, hence
requires linking, hence requires more
are memory efficient.
memory.
Programming languages like
Programming languages like C, C++, Java
JavaScript, Python, Ruby use
Editor :
• The Code is Written into one or more text files, which you can open,
read and edit in any text editor, such as Notepad in windows, TextEdit
on a Mac, And gedit in Linux.
• The Process of developing software using the c programming
language involves a number of tasks/phases.
• To successfully develop an application/program we need to have an
editor, a Compiler, a Linker, and a Loader.
• We can use either a plain text editor (e.g. Notepad) or the editor
provided by the IDE.
• The source code must be written as required by the C language
syntax. After the source file is ready, it must be saved as an a*.c file.
• For writing, compiling, and executing our program we normally use
TC (TurboC)editor.
TC EDITOR :
• TC Editor is very simple and easy to use; here will give you all tips related to
TC Editor and some shortcut keys related to TC Editor which are very useful at
the time of coding.

• TurboC is a most common C language Compiler.

• The interface of Turbo C is very simple. when the IDE screen appears, the
menu bar is activated. It contains various menus such as;
• File: This menu contains a group of commands used for save, edit , print program, exit
from Turbo C editor etc.

• Edit: This menu contains a group of commands used for editing C program source code.
Example Copy, Cut, Paste, Undo etc.

• Search: This menu contains group of commands used for searching specific word as well
as replacing it with another one.

• Run: This menu contains group of commands used for running C program.

• Compile: This menu contains group of commands used for compiling C program.

• Debug: This menu contains group of commands used for debugging C program.

• Project: This menu contains group of commands used for opening, closing and creating
projects.
• Options: This menu contains group of commands used for configuring IDE of Turbo C and setting up
directories etc.

• Windows: This menu contains group of commands used for opening, closing various windows of IDE.

• Help: This menu is used to get help about a specific topic of C language. Similarly to get help about a specific
keyword or identifier of C.

• Shortcut keys Related to TC Editor


• Alt + x : Close TC Editor.
• Clt + f9 : Run C Program.
• Alt + f9 : Compile C Code.
• Alt + Enter : Get Full Screen or Half Screen TC Editor.
• Clt + y : Delete complete line above the cursor.
• Shift + Right arrow : Select Line of Code.
• Clt + Insert : Copy.
• Shift + Insert : Paste.
• Shift + Delete : Delete.
Basic Structure(Skeleton) of C Program

#include <stdio.h>

int main()
{
printf(“Hello World\n”);

return 0;
}
• Documentation section : The documentation section consists of a set of
comment lines giving the name of the program, the author and other
details, which the programmer would like to use later.

• Link section : The link section provides instructions to the compiler to link
functions from the system library.

• Definition section : The definition section defines all symbolic constants.

• Global declaration section : There are some variables that are used in
more than one function. Such variables are called global variables and are
declared in the global declaration section that is outside of all the
functions. This section also declares all the user-defined functions.
• main () function section: Every C program must have one main function
section. This section contains two parts; the declaration part and the
executable part
• Declaration part: The declaration part declares all the variables used in the
executable part.
• Executable part: There is at least one statement in the executable part. These
two parts must appear between the opening and closing braces. The program
execution begins at the opening brace and ends at the closing brace. The
closing brace of the main function is the logical end of the program. All
statements in the declaration and executable part end with a semicolon.

• Subprogram section: The subprogram section contains all the user-defined


functions that are called in the main () function. User-defined functions are
generally placed immediately after the main () function, although they may
appear in any order. Note: All sections, except the main () function section,
may be absent when they are not required.

You might also like