Act9 Sasi Raymart
Act9 Sasi Raymart
BS AEE – 1 C
PROGRAMMING
What is Programming?
Programming Language is a set of instructions and syntax used to create software programs.
Some of the key features of programming languages include:
1. SYNTAX: the specific rules and structure used to write code in a programming
language.
HARDWARE: refers to the physical components of a computer, such as the CPU, RAM,
storage, memory, and peripherals, that work together to support software and execute tasks.
SOFTWARE: a collection of programs and data that direct a computer’s operations, enabling
hardware functionality and user interactions such as Google Chrome, Microsoft Teams, and
other applications. This is also a set of instructions that tells a computer exactly what to do.
C LANGUAGE OVERVIEW
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of
C, now known as the K&R standard.
SYMBOL: Flowcharts use standardized symbols to represent different operations. Some of the
most common symbols include oval, diamond, rectangle, parallelogram, arrow, and circle.
RULES: There are some general rules to follow when drawing flowcharts to ensure they are
clear and easy to understand
1. Start and End: Every flowchart should have a clear start and at least one end point,
represented by the terminal symbol.
2. Direction of Flow: The flow of the algorithm should generally be from top to bottom or
left to right. Arrows should clearly indicate the direction.
3. Clarity: Use clear and concise descriptions within the symbols. Avoid ambiguity.
4. Single Entry, Single Exit (for processes and decisions): Generally, each process and
decision symbol should have one entry point and one exit point (except for the decision
symbol, which has multiple exit paths).
5. Avoid Crossing Lines: Try to arrange the flowchart in a way that minimizes or eliminates
the crossing of flow lines. If crossing is unavoidable, use connectors to maintain clarity.
6. Keep it Simple: For complex algorithms, it might be helpful to break them down into
smaller, more manageable flowcharts.
DRAWING TECHNIQUES:
1. Sequential Flow: Steps are executed in a linear order, one after the other. This is
represented by a straight line of process symbols connected by arrows.
2. Decision Making: The decision symbol is used to represent points where the flow of the
algorithm depends on a condition. There will be multiple paths (usually two,
representing "yes" or "no") leading out of the decision symbol.
3. Looping (Iteration): To repeat a set of steps, flowcharts use loops. This can be
represented by directing the flow back to an earlier point in the flowchart based on a
condition.
4. Connectors: When a flowchart becomes large or needs to be split across pages,
connectors (circles with labels) are used to link different parts. An "on-page connector"
links parts within the same page, while an "off-page connector" links parts on different
pages.
5. Subroutines (or Functions): For more complex algorithms, a process might represent a
call to another, more detailed flowchart (a subroutine or function). This helps in
modularizing the algorithm.
1. SEQUENCE: represents linear tasks sequentially performed one after the other.
2. WHILE: a loop with a condition at its beginning.
3. REPEAT-UNTIL: a loop with a condition at the bottom.
4. FOR: another way of looping.
5. IF-THEN-ELSE: a conditional statement that changes the flow of the algorithm.
6. CASE: the generalization form of IF-THEN-ELSE.
NOTE: When writing pseudocode, everyone has their own style of presenting since humans
are reading it and not a computer; pseudocode’s rules are less rigorous than those of a
programming language. However, there are some simple rules that help make pseudocode
more universally understood.
1. Always capitalize the initial word (often one of the six main constructs).
2. Make only one statement per line.
3. Indent to show hierarchy, improve readability, and show nested constructs.
4. Always end multi-line sections using any of the END keywords (ENDIF, ENDWHILE, etc.).
5. Keep your statements programming language independent.
6. Use the naming domain of the problem, not that of the implementation. For instance:
“Append the last name to the first name” instead of “name = first+last.”
7. Keep it simple, concise, and readable.
CONVERTING FLOWCHARTS TO PSEUDOCODE
Flowchart:
1. Start
2. Read the value of x
3. If x > 10
4. Output "x is greater than 10"
5. Else
6. Output "x is not greater than 10"
7. End
The basic structure of a C program is divided into 6 parts, which makes it easy to read, modify,
document, and understand in a particular format. A C program must follow the following
outline in order to successfully compile and execute. Debugging is easier in a well-structured
C program.
1. DOCUMENTATION: this section consists of the description of the program, the name of
the program, and the creation date and time of the program. It is specified at the start
of the program in the form of comments.
EX. // description, name of the program, programmer name, date, time etc.
2. PREPROCESSOR SECTION: All the header files of the program will be declared in the
preprocessor section of the program. Header files help us to access others’ improved
code in our code. A copy of these multiple files is inserted into our program before the
process of compilation.
EX. #include<stdio.h>
#include<math.h>
3. DEFINITION: Preprocessors are the programs that process our source code before the
process of compilation. There are multiple steps which are involved in the writing and
execution of the program. Preprocessor directives start with the ‘#’ symbol. The #define
preprocessor is used to create a constant throughout the program. Whenever this name
is encountered by the compiler, it is replaced by the actual piece of defined code.
6. SUB PROGRAMS: User-defined functions are called in this section of the program. The
control of the program is shifted to the called function whenever they are called from
the main or outside the main() function. These are specified as per the requirements of
the programmer.
return x+y;
“HELLO WORLD” PROGRAM: a first step towards learning any programming language, and is
also one of the most straightforward programs you will learn. It is the basic program that
demonstrates the working of the coding process. All you have to do is display the message
“Hello World” on the output screen.
COMPILATION AND EXECUTION PROCESS
COMPILATION: the process of converting the source code of the C language into machine
code. As C is a mid-level language, it needs a compiler to convert it into an executable code
so that the program can be run on our machine.
1. Source File
2. Preprocessor
3. Compiler
4. Assembler
5. Linker
A compiler converts a C program into an executable. There are four phases for a C program
to become an executable:
1. PRE-PROCESSING: the first phase through which source code is passed. This phase
includes the Removal of Comments, Expansion of Macros, Expansion of the included
files, and Conditional compilation.
2. COMPILATION: the next step is to compile filename.i and produce an intermediate
compiled output file filename.s. This file is in assembly-level instructions.
3. ASSEMBLY: In this phase, the filename.s is taken as input and turned into filename.o by
the assembler. This file contains machine-level instructions. At this phase, only existing
code is converted into machine language, and the function calls like printf() are not
resolved.
4. LINKING: This is the final phase in which all the linking of function calls with their
definitions is done. Linker knows where all these functions are implemented. Linker does
some extra work also: it adds some extra code to our program, which is required when
the program starts and ends. For example, there is a code that is required for setting
up the environment, like passing command line arguments. This task can be easily
verified by using the size filename.o and size filename. Through these commands, we
know how the output file increases from an object file to an executable file. This is
because of the extra code that the Linker adds to our program.
a. STATIC LINKING: All the code is copied to a single file, and then an executable
file is created.
b. DYNAMIC LINKING: Only the names of the shared libraries are added to the
code, and then it is referred to during the execution.
C DATA TYPES
INT (INTEGER): to store whole numbers without decimals (e.g., -10, 0, 100).
FLOAT (FLOATING POINT): store numbers with decimal points (e.g., 3.14, -2.71).
CHAR (CHARACTER): to store single characters, such as letters, numbers, symbols, or spaces
(e.g., 'A', '5', '*').
DOUBLE (DOUBLE PRECISION FLOATING POINT): similar to float, but provides more precision
for decimal numbers.
FOR C++
FOR C
CONSTANTS AND BASIC I/O (SCANF, PRINTF)
In C programming, printf() is one of the main output functions. The function sends formatted
output to the screen. There are many functions used for input and output in different situations,
but the most commonly used functions for Input/Output are scanf() and printf(), respectively.
Basic Output in C
The printf() function is used to print formatted output to the standard output stdout (which is
generally the console screen). It is one of the most commonly used functions in C.
The scanf() function is used to read user input from the console. It takes the format string and
the addresses of the variables where the input will be stored.
C Programming Operators
C ASSIGNMENT OPERATORS: used for assigning a value to a variable. The most common
assignment operator is = .
C RELATIONAL OPERATORS: checks the relationship between two operands. If the relation is
true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in
decision making and loops.
Operator precedence and associativity are rules that decide the order in which parts of an
expression are calculated.
OPERATOR ASSOCIATIVITY: used when two operators of the same precedence appear in an
expression. Associativity can be either from Left to Right or Right to Left.
DECISION MAKING IN C
In C, programs can choose which part of the code to execute based on some condition. This
ability is called decision making, and the statements used for it are called conditional
statements. These statements evaluate one or more conditions and make the decision whether
to execute a block of code or not.
SWITCH CASE
SWITCH STATEMENT: a control flow structure that allows you to execute one of many code
blocks based on the value of an expression. It is often used in place of the if-else ladder when
there are multiple conditional codes.
LOOPS IN C
In programming, loops are used to repeat a block of code until a specified condition is met.
WHILE LOOP: runs as long as a condition is true. It is used when you don’t know in advance
how many times you want to execute the block of code. It continues to execute as long as the
specified condition is true.
DO-WHILE LOOP: runs at least once and then continues if a condition is true. It is similar to the
while loop, but with one key difference: it guarantees that the block of code will execute at least
once before checking the condition.
LOOP CONTROL
BREAK STATEMENT: used to "jump out" of a switch statement. It can also be used to jump out
of a loop.
CONTINUE STATEMENT: it breaks one iteration (in the loop) if a specified condition occurs, and
continues with the next iteration in the loop.
FUNCTION IN C
A function in C is a set of statements that, when called, perform some specific tasks. It is the
basic building block of a C program that provides modularity and code reusability. The
programming statements of a function are enclosed within { } braces, having certain meanings
and performing certain operations. They are also called subroutines or procedures in other
languages.
FUNCTION DECLARATION: it must provide the function name, its return type, and the number
and type of its parameters. It also tells the compiler that there is a function with the given name
defined somewhere else in the program.
FUNCTION DEFINITION: consists of actual statements which are executed when the function is
called (i.e. when the program control comes to the function).
FUNCTION CALL: a statement that instructs the compiler to execute the function. We use the
function name and parameters in the function call.
C FUNCTION PARAMETERS
PARAMETER: specified after the function name, inside the parentheses. It can add as many
parameters as you want, just separate them with a comma.
RETURN VALUE: the output or result that a function produces and sends back to the code that
called the function.
SCOPE OF VARIABLES
SCOPE OF VARIABLES: it defines the region of the code where a particular variable can be
referenced or used. Scope holds the current state of variables along with their respective values.
Scope of a variable is depends on where the value is declared in the program. Once, a variable
is declared, we can access and manipulate its data according to our needs. Understanding how
the variable's value is accessed and used within its scope is essential to find out the program's
behavior.
• Local Scope or Block Scope: The variables declared within the local scope are called
local variables. Local variables are visible in the block they are declared in and other
blocks nested inside that block.
• Global Scope or File Scope: The variables declared in the global scope are called global
variables. Global variables are visible in every part of the program.
INTRODUCTION TO 1D ARRAYS
ARRAY: a fixed-size collection of similar data items stored in contiguous memory locations. It
can be used to store the collection of primitive data types such as int, char, float, etc., and also
derived and user-defined data types such as pointers, structures, etc.
ARRAY DECLARATION: It can declare an array by specifying its name, the type of its elements,
and the size of its dimensions. When we declare an array in C, the compiler allocates the
memory block of the specified size to the array name.
ARRAY INITIALIZATION: the process of assigning an initial value to the variable. When the array
is declared or allocated memory, the elements of the array contain some garbage value. So,
we need to initialize the array to some meaningful values by using an initializer list is a list of
values enclosed within braces { } separated by a comma.
ACCESSING: can access any element of the array by providing the position of the element,
called the index. Indexes start from 0. We pass the index inside square brackets [] with the
name of the array.
ARRAY OF STRINGS IN C