0% found this document useful (0 votes)
8 views

UNIT 2

The document covers structured programming principles, including algorithms, modular programming, and the structure of a C program. It explains the importance of organized code, control structures, data types, and the programming process, emphasizing readability and maintainability. Additionally, it details various data types in C, operators, and expressions, providing examples and guidelines for effective programming.

Uploaded by

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

UNIT 2

The document covers structured programming principles, including algorithms, modular programming, and the structure of a C program. It explains the importance of organized code, control structures, data types, and the programming process, emphasizing readability and maintainability. Additionally, it details various data types in C, operators, and expressions, providing examples and guidelines for effective programming.

Uploaded by

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

UNIT 2 - DATATYPES AND FLOW

OF CONTROL
Structured programming – Algorithms – Structure of
a C program – Variables – Data types – Operators
and expressions – Input and Output statements –
Tokens –Type Conversion – Control statements.
STRUCTURED PROGRAMMING
•In 1968, Edsger Dijkstra, a computer scientist from the
Netherlands, published an goto statement
•The goto statement allows a program to jump to
different parts of the code, which can make the program
difficult to follow, understand, and maintain. Dijkstra
advocated for a more organized and systematic way of
writing code, which he called structured programming.
•Structured programming is a method of writing
programs that focuses on making code easier to
understand, maintain, and debug. It avoids chaotic
control flows (like using goto), and instead organizes
code into logical structures.
CONCEPT OF STRUCTURED PROGRAMMING INCLUDE:
•Top-down approach: Programs are broken down into smaller,
manageable parts (modules). This helps solve complex problems
step by step, starting with the big picture and working down to the
details.
•Modularization: Programs are divided into self-contained blocks or
modules that perform specific tasks. Each module can be developed
and tested independently. This helps make programs easier to read,
modify, and reuse.
•Structured code: Instead of using random jumps (like with goto),
structured programming uses three basic control structures:
•Sequence: Executing statements one after the other.
•Selection: Using structures like if or switch to make decisions in the
code.
•Iteration: Using loops like for and while to repeat certain
operations.
Example of Structured Programming

if (condition) {
// do something
} else {
// do something else
}

for (int i = 0; i < 10; i++) {


// repeat this code 10 times
}
1. Top-Down Analysis:
Top-down analysis is a problem-solving approach where a large,
complex problem is broken down into smaller, more manageable
tasks or parts. This makes it easier to analyze and solve each part
individually.
Subdivision: The problem is divided into smaller problems. This
continues until no further division is possible, creating a hierarchy of
tasks.
Steps in Top-Down Analysis:
Define the problem: Identify the input, process, and output
requirements.
Divide the problem: Break the problem into two or more
smaller parts.
Define tasks: Define and further subdivide the tasks.
Repeat until the lowest level: Continue subdividing until the
tasks cannot be broken down further.
2. Modular Programming:
Modular programming is a method where a program is divided
into independent sections called modules. These modules can be
written and tested separately, making programs more organized
and easier to maintain.
Characteristics of Modules:
Each module consists of a series of program instructions.
Modules have a unique name and only one entry and exit
point.
Advantages:
Complex programs are easier to manage by dividing them into
smaller parts.
Different programmers can work on different modules
simultaneously.
Errors can be isolated to specific modules, making debugging
easier.
Modules can be reused in other programs.
3. Structured Code
After using top-down analysis and modular design,
the final step is to write structured code. Structured
programming uses control structures like loops (e.g.,
for, while) and conditionals (e.g., if, case) to ensure
the program follows a clear, organized flow.
•Example: A goto statement in BASIC can make a
program hard to follow by breaking the normal flow
of control. A structured version of the same program
using a while loop is easier to understand and
maintain.
Structured programming avoids goto statements and
emphasizes readable, maintainable code.
4. The Process of Programming
The process of programming involves more than just writing code.
The programmer follows several steps to ensure the program works
correctly:
Understand the problem: The programmer must fully grasp the
problem to solve it, often by interacting with users to define the
requirements.
Design the solution: Develop the logic and steps to solve the
problem.
Write the program: Use a programming language to write the
solution, following the syntax and rules of that language.
Translate to machine code: The program is converted to machine
code (binary) using a translator, so the computer can execute it.
Test the program: Run the program with test data to find and fix any
errors in the logic or code.
Put the program into operation: The program is put into actual use.
Any errors found during operation are corrected to meet the user’s
needs.
Algorithms
An algorithm is a set of instructions or steps designed to solve a problem in a
finite number of steps. It is effective, meaning that it will always reach a result,
whether it's the solution to the problem or the conclusion that there is no
solution. An algorithm must eventually terminate, or finish, rather than run
indefinitely.
In the words of Niklaus Wirth, a Swiss computer scientist:
Program = Algorithms + Data
This means that a computer program is built by combining algorithms (the
instructions to solve problems) with data (the information that the program
processes).
Different Ways of Representing Algorithms:
Algorithms can be represented in several different ways. Each method has its
own advantages and uses:
Step-form:
This representation uses plain language to describe the step-by-step actions
required to solve a problem.
Each statement builds on the previous one, and together they provide a
solution.
It’s simple and easy to understand since it uses everyday language
Pseudo-code:
Pseudo-code is like step-form, but it uses a restricted, more formal
vocabulary.
It is written in human-readable language, but closer to programming
language, ensuring more precision and clarity compared to natural
language.
It helps bridge the gap between plain language and actual code by
providing clearer instructions that can be easily converted into a
programming language.
Flowcharts:
Flowcharts are a graphical representation of the algorithm. They use
symbols to represent different steps like sequence, decisions, and loops
(repetitions).
A flowchart helps visually map the flow of the process, making it easier to
understand the logic of the algorithm at a glance.
Nassi-Schneiderman Diagrams:
This is another graphical representation similar to flowcharts, used to
represent sequences, decisions, and loops.
It is more structured than flowcharts but isn’t explained in detail in this
particular text.
Features of an Algorithm
An algorithm can be stated using three basic constructs:
1. Sequence (also known as process)
2. Decision (also known as selection)
3. Repetition (also known as iteration or looping)

Sequence:
Sequence means that each step or process in the algorithm is
executed in the specified order.

Decision:
The decision constructs—if ... then, if ... then …else...
The decision can also be stated as:
if (proposition)
then (process1)
else (process2)
Repetition:
Repetition can be implemented using constructs like the repeat loop, while
loop, and if.. then .. goto .. loop.
The Repeat loop is used to iterate or repeat a process or sequence of processes
until some condition becomes true. It has the general form:
Repeat
Process1
Process2
..............
………..
ProcessN
Until proposition.
Termination:
Termination is the area of study that investigates whether or not a given
algorithm or process will eventually stop. It’s not always possible to determine
in advance if an algorithm will halt, making this a complex problem.
Variable
The variable is a container for a value that may vary during the
execution of the program. For example, in the tea-making
algorithm.
Subroutines:
Flowcharts
A flowchart is a visual tool used to map out the steps of a process or a solution
to a problem.
It is created before writing the actual program, helping to plan the logic clearly.
How Flowcharts Work:
A flowchart is made up of standard shapes (like rectangles, diamonds, etc.) that
represent different actions.
These shapes are connected with arrows, showing the direction of the process
flow.
The activities are written inside the shapes in plain English, not code.
Why Use Flowcharts?
Flowcharts make it easier to understand the logic of a program, especially for
complicated problems.
They help programmers, users, and business people communicate better.
Once a flowchart is ready, writing the actual program becomes much easier.
Flowchart Standards:
Draw on white paper, with flow starting from the top and moving
down/right.
Use standard symbols and templates to ensure neatness and
clarity.
Keep the writing clear, brief, and in English.
Each subroutine (if any) should be on a separate page.
Guidelines for Drawing Flowcharts:
The flowchart must have a clear start and stop.
Follow a logical sequence, with steps going from left to right or
top to bottom.
Avoid too many flow lines crossing each other, and use
connectors if needed.
Test the flowchart with simple data to ensure it works correctly.
STRUCTURE OF A C PROGRAM
Program Structure:
•A C program is organized into sections like declarations and functions.
•Declarations communicate important information about variables and data
types to the compiler.
•Preprocessor directives (like #include) tell the compiler to include special
libraries or make changes to the code before compiling it.
Global Declaration:
•Global variables are declared outside of any function and can be accessed by
any part of the program.
•These variables are visible to all functions in the program, making them
globally accessible.
Local Definitions:
•Every function, including main(), has two sections:
• Local definitions (placed at the start of the function) specify the data
(like variables) the function will use.
• Statements (following the local definitions) are the instructions the
function will execute.
Local variables are only visible to the specific function in which they are
declared, unlike global variables.
Declaration vs. Definition:
•Declaration:
• This tells the compiler about a data object (like a variable) or a function,
including its type.
• It doesn’t allocate memory or provide the full implementation—just
information.
• The main purpose is type checking to ensure that variables or functions
are used correctly in the program.
•Definition:
•A definition actually allocates memory for a variable or provides the
body (implementation) of a function.
•In the case of a variable, it sets aside storage space. In the case of a
function, it provides the actual code that will be executed.
DATA TYPES IN C
“Primitive" data types are the building blocks for creating more complex data
structures. These types define the kind of data a variable can hold.
1.Character (char):
a) Keyword: char
b) Purpose: This type is used to store single characters such as letters, digits,
or symbols (e.g., 'a', 'Z', '1', '@').
c) Size: Typically, it takes 1 byte of memory and can hold values from -128 to
127 (or 0 to 255 if unsigned) based on the ASCII character set.
Integer (int):
a) Keyword: int
b) Purpose: Used for storing whole numbers (i.e., numbers without fractional
parts) like 1, -100, or 5000.
c) Size: Generally, it takes 4 bytes of memory, though this can vary based on the
system, and it can store values in a range like -2,147,483,648 to
2,147,483,647.

Floating-point (float):
d) Keyword: float
e) Purpose: This is used for numbers that have a decimal point or fractional part,
like 3.14 or -0.001.
f) Size: It usually occupies 4 bytes in memory and provides around 6-7 digits of
precision.

Double precision floating-point (double):


g) Keyword: double
h) Purpose: Similar to float, but it can store larger numbers and provides greater
precision, meaning it can represent more digits after the decimal point.
i) Size: Typically, it takes 8 bytes of memory and provides about 15-16 digits of
precision.
Valueless (void):
a) Keyword: void
b) Purpose: This type is used in situations where a function does not return
any value or when a pointer is declared but the type of the object it points
to is not yet specified. It essentially means "nothing" or "no data type."
c) Example: A function with a void return type doesn't give back a result after
execution.
Basic Concept of Type Modifiers and Qualifiers:
Type Specifiers (or Modifiers):
These change the size or the sign of basic data types.
C provides three main categories:
Size specifiers: short and long
Sign specifiers: signed and unsigned
Type Qualifiers:
These are used to modify the behavior of a variable.
Three qualifiers are:
const: Prevents the variable’s value from being changed after it is initialized.
volatile: Tells the compiler that the value of the variable can change unexpectedly (e.g.,
hardware registers).
restrict: Used in pointer declarations to indicate that the pointer is the only reference to
that memory location.
Type Modifiers and Qualifiers Explained
1. Size Specifiers (short and long)
•short: When used with int, it tells the compiler to store smaller
integers, saving memory space.
•Example: short int x = 32000;
•Used when you need small integer values (e.g., from -32,768
to 32,767) to conserve memory.
•long: Used for larger integer values.
•Example: long int y = 1000000;
•Suitable for larger number ranges than int can hold.
2. Sign Specifiers (signed and unsigned)
•signed: Allows both positive and negative values.
•Example: signed int x = -42;
•unsigned: Stores only non-negative values but doubles the
positive range.
•Example: unsigned int y = 50000;
These sign specifiers can also be applied to char for dealing with
ASCII values or small numbers.
3. Type Qualifiers (const, volatile, and restrict)
•const: Marks a variable as constant, meaning its value cannot be
changed after it is set.
• Example: const int x = 100;
•volatile: Informs the compiler that the variable can change
unexpectedly, often used in embedded programming when
dealing with hardware registers.
• Example: volatile int status_register;
•restrict: Used in pointers to indicate that the memory accessed by
this pointer is not accessed by any other pointer, allowing for
optimization by the compiler.
Rules for Sizes of Integer Types
In ANSI C compilers (which follow a specific standard), the sizes of
integer types like short, int, and long must follow these rules:
1.short int: Must be at least 2 bytes.
2.int: Must be at least as large as short int.
3.long int: Must be at least as large as int, and its minimum size is
4 bytes.
System-Specific Sizes of Integer Types
•On 16-bit systems (like older DOS compilers):
•short int and int are both 2 bytes.
•long int is 4 bytes.
•On 32-bit systems (like modern compilers and GNU C):
•short int is 2 bytes.
•int and long int are both 4 bytes.
•On UNIX-based compilers:
•short int is 2 bytes.
•long int is 4 bytes.
PROGRAM STATEMENT

1. A statement performs an action.


2. It must end with a semicolon in C.
Operators and expressions
•Operators are symbols that tell the computer to perform certain
actions.
•Expressions are combinations of operators and values that
calculate a result.
•The evaluation of an expression gives a value, but it can also
cause side effects (such as changing a variable).
•C has many types of operators, and it follows precedence rules
to decide the order in which operations are evaluated.
Arithmetic Operators in C
Binary operators

#include <stdio.h>
int main()
{
int a = 25;
int b = 2;
int result;
float c = 25.0;
float d = 2.0;
printf(“6 + a / 5 * b = %d \n”, 6 + a / 5
* b);
printf(“a / b * b = %d\n”, a / b * b);
printf(“c / d * d = %f\n”, c / d * d);
printf(“-a = %d\n”,-a);
return 0;
}
Relational Operators Logical Operators
‘A’ < ‘F’ /* gives 1 (is like 65 < 70) */

Bitwise Operators Conditional Operator


expression1 ? expression2 : expression3

int m = 1, n = 2, min;
min = (m < n ? m : n); /* min is assigned a
value 1 */
Comma Operator
expressionM = (expression1, expression2, …,expressionN);

sizeof Operator
#include <stdio.h>
int main() char size = 1 bytes
{ short size = 2 bytes
printf(“char size = %d bytes\n”, sizeof(char)); int size = 2 bytes
printf(“short size = %d bytes\n”, sizeof(short)); long size = 4 bytes
printf(“int size = %d bytes\n”, sizeof(int)); float size = 4 bytes
printf(“long size = %d bytes\n”, sizeof(long)); double size = 8 bytes
printf(“float size = %d bytes\n”, sizeof(float)); 1.55 size = 8 bytes
printf(“double size = %d bytes\n”, 1.55L size = 10 bytes
sizeof(double)); HELLO size = 6 bytes
printf(“1.55 size = %d bytes\n”, sizeof(1.55));
printf(“1.55L size = %d bytes\n”, sizeof(1.55L));
printf(“HELLO size = %d bytes\n”,
sizeof(“HELLO”));
return 0;
}
Expression

You might also like