0% found this document useful (0 votes)
3 views49 pages

Introduction to c Programming 1

The document provides an introduction to C programming, focusing on algorithms, their representations, and the structure of a C program. It explains the importance of algorithms in problem-solving, outlines various algorithm representations (descriptive, natural language, pseudocode, and flowcharts), and details the setup of a C development environment. Additionally, it covers the basic structure of a C program, including header files, the main method, and the use of identifiers.

Uploaded by

wilford282006
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)
3 views49 pages

Introduction to c Programming 1

The document provides an introduction to C programming, focusing on algorithms, their representations, and the structure of a C program. It explains the importance of algorithms in problem-solving, outlines various algorithm representations (descriptive, natural language, pseudocode, and flowcharts), and details the setup of a C development environment. Additionally, it covers the basic structure of a C program, including header files, the main method, and the use of identifiers.

Uploaded by

wilford282006
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/ 49

INTRODUCTION TO

C PROGRAMMING
ALGORITHM
A programming algorithm is a procedure or formula used for
solving a problem. It is based on conducting a sequence of specified
actions in which these actions describe how to do something, and your
computer will do it exactly that way every time.

An algorithm works by following a procedure, made up of inputs.


Once it has followed all the inputs, it will see a result, also known as
output.
EXAMPLE
Consider the example to add three numbers and print the sum.
Step 1 Fulfilling the pre-requisites
As discussed above, to write an algorithm, its prerequisites must be fulfilled.

1. The problem that is to be solved by this algorithm.


- Add 3 numbers and print their sum.
2. The constraints of the problem that must be considered while solving the problem.
-The numbers must contain only digits and no other characters.
3.The input to be taken to solve the problem.
- The three numbers to be added.
4.The output to be expected when the problem is solved
- The sum of the three numbers taken as the input i.e. a single integer value.
5.The solution to this problem, in the given constraints.
- The solution consists of adding the 3 numbers. It can be done with the help of the ‘+’
operator, or bit-wise, or any other method.
Step 2: Designing the algorithm

Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:

1.START
2.Declare 3 integer variables num1, num2, and num3.
3.Take the three numbers, to be added, as inputs in variables num1, num2,
and num3 respectively.
4.Declare an integer variable sum to store the resultant sum of the 3
numbers.
5.Add the 3 numbers and store the result in the variable sum.
6.Print the value of the variable sum
7.END
ALGORITHM REPRESENTATION
Algorithm representation refers to the different ways of expressing an
algorithm to make it understandable and executable. There are three
main representations:

1. Descriptive Representation
2. Natural Language Representation
3. Pseudocode Representation
4. Flow Chart Representation
Descriptive Representation
Provides a detailed explanation of each step, including reasoning
and constraints.

EXAMPLE: Find the Sum of Two Numbers

1. The algorithm begins by instructing the user to provide two numeric


inputs.
2. These inputs are stored in memory for processing.
3. The system performs an addition operation on the two numbers.
4. The computed sum is then displayed as the output.
5. The process ends after showing the result.
Natural Language Representation
Describes the algorithm in simple and everyday language as step-
by-step instructions.

EXAMPLE: Find the Sum of Two Numbers

1.Start.
2.Ask the user to enter two numbers.
3.Add the two numbers.
4.Display the sum.
5.Stop.
Pseudocode Representation
Uses a structured and code-like format to express the algorithm
without specific programming syntax.

Pseudocode Syntax
Pseudocode uses a structured, high-level format that resembles
programming logic but is not bound to any specific programming
language.
Pseudocode Syntax
Start and Stop
- Used to define the beginning and end of the algorithm.
“START” and ”STOP”

Input and Output


“INPUT” or “READ” is used to take user input.
“OUTPUT”, “PRINT”, or “DISPLAY” is used to show results.

Arithmetic Operations
- Used on arithmetic operations
Operation Symbol Representation
Addition + sum ← num1 + num2
Subtraction - difference ← num1 - num2
Multiplication * product ← num1 * num2
Division / quotient ← num1 / num2
Modulus (Remainder) MOD remainder ← num1 MOD num2
Variable Assignment
- Used to define the beginning and end of the algorithm.
“(← or =)”
“sum ← num1 + num2 or sum = num 1+ num2”

Conditional Statements (If-Else)


- Used for decision making in algorithms.

“ IF num1 > num2 THEN


PRINT "num1 is greater"
ELSE
PRINT "num2 is greater"
ENDIF “
Loops (For, While, Repeat-Until)
Used for repeating actions multiple times.

For Loop (Fixed Iteration)


“ FOR i ← 1 TO 5 DO
PRINT "Iteration:", i
ENDFOR”

While Loop (Conditional Iteration)


“WHILE num < 10 DO
num ← num + 1
PRINT num
ENDWHILE"

Repeat-Until Loop
“REPEAT
num ← num + 1
PRINT num
UNTIL num = 10”
EXAMPLE: Find the Sum of Two Numbers

1. Start
2.Input num1, num2
3. sum ← num1 + num2
4. Print sum
5. Stop
Flowchart
Flowcharts are the visual representations of an algorithm or a
process. Flowcharts use symbols/shapes like arrows, rectangles, and
diamonds to properly explain the sequence of steps involved in the
algorithm or process.

They provide clarity and simplification to the complex


processes and algorithms, which in turn helps other people to
understand them easily.

Flowcharts help in increasing the visualization of the problem being


solved which enables more informed and data-driven choices.
Symbols used in Flowchart Designs
1. Terminal/Terminator

The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A
pause/halt is generally used in a program logic under some error conditions. Terminal is the
first and last symbols in the flowchart.
2. Input/Output

A parallelogram denotes any function of input/output type. Program instructions that


take input from input devices and display output on output devices are indicated with
parallelogram in a flowchart.

3. Action/Process

A box represents arithmetic instructions, specific action or operation that occurs as a


part of the process. All arithmetic processes such as adding, subtracting, multiplication and
division are indicated by action/process symbol.
4. Decision

Diamond symbol represents a decision point. Decision based operations such as yes/no
question or true/false are indicated by diamond in flowchart.

5. On-Page Connector/Reference

Whenever flowchart becomes complex on one page, it is useful to use connectors to


avoid any confusions. connectors are used to indicate a jump from one part of the flowchart to
another without drawing long or complicated lines. On-Page Connector is represented by a
small circle
6. Off-Page Connector/Reference

Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use
connectors to avoid any confusions. connectors are used to indicate a jump from one part of the
flowchart to another without drawing long or complicated lines. Off-Page Connector is
represented by a pentagon.

7. Flow lines

Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the
direction of flow of control and relationship among different symbols of flowchart.
8. Document
Symbolizes a report or document or display, shown as a rectangle with a wavy
bottom. Used when the process involves creating, reading, or outputting a document.

9. Preparation
Represents an initialization or preparation step before execution begins. sed for
setting up initial conditions, declaring variables, or preparing values before a process starts.
OTHER SYMBOLS
Rules For Creating a Flowchart
A flowchart is a graphical representation of an algorithm. It should follow some
rules while creating a flowchart
Rule 1: Flowchart opening statement must be ‘start’ keyword.

Rule 2: Flowchart ending statement must be ‘end’ keyword.

Rule 3: All symbols in the flowchart must be connected with an arrow line.

Rule 4: Each decision point should have two or more distinct outcomes.

Rule 5: Flow should generally move from top to bottom or left to right.
Example of a Flowchart
Draw a flowchart to input two numbers from the user and display
the largest of two numbers.
Start: The process begins with the Start symbol,
indicating the start of the program.

Input num1. The first number, represented


as num1, is entered.

Input num2. The second number, represented


as num2, is entered.

Decision (num1 > num2). A decision point checks


if num1 is greater than num2.

• If True, the process moves to the next step


where num1 will be displayed.
• If False, the process moves to display num2.
Stop. The process ends with the Stop symbol,
signaling the conclusion of the program.
PROBLEM
Write an algorithm on finding the area of a rectangle using pseudocode
and flowchart

Pseudocode
1. Start
2. Declare area, length and width
3. Input length and width values
4. Calculate the area, area = length * width
5. Display Area
6. Stop
Flowchart
Write an algorithm for Calculating area and
circumference of circle
Pseudocode

1. Start
2. Declare variables radius, area, circumference
3. Define constant as PI = 3.14159
4. Input the radius of the circle
5. Calculate the area, area = PI * radius * radius
7. Calculate the circumference, circumference = 2 * PI * radius
8. Display the calculated area and circumference
8. End
Flowchart
Write an algorithm to check the least of two
numbers?
Pseudocode

1. Start
2. Declare variables of num1, num2, least
3. Input from the user to enter two numbers num1 and num2
4. Compare the two values
- If
num1 < num2 (least = num 1)
- Else
(least = num2)
5. Display the least number
6. End
Flowchart
GROUP ACTIVITY OF
PSEUDOCODE AND
FLOWCHART
1. Print the largest number among three numbers. Let the three
numbers be represented by A, B, and C.

2. Calculating the simple interest using the formula SI = (P * T * R)/100,


where P denotes the principal amount, T time, and R rate of interest.
Also, show the algorithm in step-form.
PROGRAMMING
C PROGRAMMING
C is a general-purpose programming language referred as the "mother of all
programming languages" because it influenced many modern programming languages
like C++, Java, Python and Go.
C is an excellent choice for beginners as it provides a strong foundation in
programming concepts.

The main features of the C language include:


➢General Purpose and Portable
➢Procedural Programming Language
➢Low-level Memory Access
➢Fast Speed
➢Clean Syntax
SETTING UP THE C DEVELOPMENT
ENVIRONMENT

Using online IDE


Integrated Development Environments (IDEs) play a pivotal role in
streamlining the soft development process.

Onlinе IDEs offer a convenient option for those who choose not to
set up a local environment.
GeeksforGeeks
https://ide.geeksforgeeks.org/ide/online-cpp14-compiler
Programiz C Compiler
https://www.programiz.com/c-programming/online-compiler/
Jdoodle
https://www.jdoodle.com/c-online-compiler
SETTING UP A LOCAL ENVIRONMЕNT

For a comprehensive C development environment on your local


machine, two fundamental components are necessary: a compiler and a
text editor.

Must secured and installed a text editor and saved your program
with a '. c' extension and is acquiring a C compiler. This compiler is
responsible for translating your high-level C code into a machine-
understandable low-level language. In other words, we can say that it
converts the source code written in a programming language into another
computer language that the computer understands.
TEXT EDITOR
Codе::Blocks Installation
• Download Code::Blocks by selecting the setup package based on your OS
from this link.
• Open the downloaded Code::Blocks setup file.
• Follow the on-screen instructions for installation.
• After successfully installing Code::Blocks, open the application.
• Navigate to the File menu.
• Select "New" and choose "Empty file."
• In the newly created empty file, write your C program.
• Save the file with a '.c' extension.
• Go to the Build menu in Code::Blocks.
• Choose the "Build and Run" option.

Full Video: https://www.youtube.com/watch?v=8DRt2CRD6X8


WRITING THE FIRST PROGRAM IN C
The following code is one of the simplest C programs that will help us
understand the basic syntax structure of a C program.
Structure of the C program

After the above discussion, we can formally assess the basic structure of a C
program. By structure, it is meant that any program can be written in this
structure only. Writing a C program in any other structure will lead to a
Compilation Error. The structure of a C program is as follows:
Header Files Inclusion – Line 1 [#include <stdio.h>]
The first and foremost component is the inclusion of the header files in a C program.

A header file is a file with extension .h which contains C function declarations and macro
definitions to be shared between several source files. All lines that start with # are processed by a
preprocessor which is a program invoked by the compiler.
In the above example, the preprocessor copies the preprocesses code of stdio.h to our file.
The .h files are called header files in C.

Some of the C Header files:


• stddef.h – Defines several useful types and macros.
• stdint.h – Defines exact width integer types.
• stdio.h – Defines core input and output functions
• stdlib.h – Defines numeric conversion functions, pseudo-random number generator, and memory
allocation
• string.h – Defines string handling functions
• math.h – Defines common mathematical functions.
Main Method Declaration – Line 2 [int main()]

The next part of a C program is to declare the main()


function.
It is the entry point of a C program and the execution
typically begins with the first line of the main(). The empty brackets
indicate that the main doesn’t take any parameter.
The int that was written before the main indicates the return
type of main(). The value returned by the main indicates the status
of program termination.
Body of Main Method – Line 3 to Line 6 [enclosed in {}]
The body of a function in the C program refers to
statements that are a part of that function. It can be anything
like manipulations, searching, sorting, printing, etc. A pair of
curly brackets define the body of a function. All functions must
start and end with curly brackets.

Comment – Line 7[// This prints “Hello World”]


It is a comment. They are used for the documentation of
the code and are ignored by the compiler.
Statement – Line 4 [printf(“Hello World”);]
Statements are the instructions given to the compiler. In C,
a statement is always terminated by a semicolon (;). In this
particular case, we use printf () function to instruct the compiler
to display “Hello World” text on the screen.

Return Statement – Line 5 [return 0;]


The last part of any C function is the return statement. The
return statement refers to the return values from a function. This
return statement and return value depend upon the return type of
the function.
The return statement in our program returns the value from
main(). The returned value may be used by an operating system to
know the termination status of your program. The value 0 typically
C - IDENTIFIERS
In C programming, identifiers are the names used to
identify variables, functions, arrays, structures, or any other user-
defined items. It is a name that uniquely identifies a program
element and can be used to refer to it later in the program.
Use of Identifiers in C

1. Declare Variables – Naming variables to store data.

2. Define Functions – Naming functions to perform specific tasks


3. Name Arrays and Structures – Identifying complex data types.

4. Use as Constants – Naming constants for fixed values.

You might also like