0% found this document useful (0 votes)
10 views9 pages

c notes

An algorithm is a set of well-defined instructions to solve a problem, characterized by input, output, definiteness, finiteness, and effectiveness. Flowcharts visually represent the sequence of logical steps in a program using geometric shapes and arrows. A C program consists of six sections: documentation, pre-processor, global declaration, main function, and sub-programs, each serving a specific purpose in program execution.

Uploaded by

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

c notes

An algorithm is a set of well-defined instructions to solve a problem, characterized by input, output, definiteness, finiteness, and effectiveness. Flowcharts visually represent the sequence of logical steps in a program using geometric shapes and arrows. A C program consists of six sections: documentation, pre-processor, global declaration, main function, and sub-programs, each serving a specific purpose in program execution.

Uploaded by

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

1. What is Algorithm? Explain characteristics of algorithm.

An algorithm is a set of well-defined instructions to solve a particular problem. It takes a set of


input(s) and produces the desired output. For example,

An algorithm to add two numbers:

1. Take two number inputs

2. Add numbers using the + operator

3. Display the result

Characteristics of an Algorithm

 Input: An algorithm has zero or more inputs. Each that contains a fundamental operator
must accept zero or more inputs.

 Output: An algorithm produces at least one output. Every instruction that contains a
fundamental operator must accept zero or more inputs.

 Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to


interpret. Every fundamental operator in instruction must be defined without any
ambiguity.

 Finiteness: An algorithm must terminate after a finite number of steps in all test cases.
Infinite loops or recursive functions without base conditions do not possess finiteness.

 Effectiveness: An algorithm must be developed by using very basic, simple, and feasible
operations so that one can trace it out by using just paper and pencil.

Properties of Algorithm:

 It should terminate after a finite time.

 It should produce at least one output.

 It should take zero or more input.

 It should be deterministic means giving the same output for the same input case.

 Every step in the algorithm must be effective i.e. every step should do some work.

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(imp).

Example

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

2. What is flowchart? Explain with example.


Flowchart is a diagrammatic representation of sequence of logical steps of a program.
Flowcharts use simple geometric shapes to depict processes and arrows to show relationships
and process/data flow.
Flowchart Symbols
Here is a chart for some of the common symbols used in drawing flowcharts.
These are some points to keep in mind while developing a flowchart
 Flowchart can have only one start and one stop symbol
 On-page connectors are referenced using numbers
 Off-page connectors are referenced using alphabets
 General flow of processes is top to bottom or left to right
 Arrows should not cross each other
Example:
flowchart to calculate the average of two numbers.

3. Explain Structure of the C Program.

C program must follow the below-mentioned outline in order to successfully compile and
execute. Debugging is easier in a well-structured C program.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are
mentioned below:
 Documentation.
 Pre-processor Section.
 Global Declaration
 Main() Function
 Sub Programs
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. Documentation can be
represented as:

Example:

// description, name of the program, programmer name, date, time etc.

1. Preprocessor Section

 All the header files of the program will be declared in the pre-processor section of the
program.
 Header files help us to access other’s improved code into our code.
 A copy of these multiple files is inserted into our program before the process of
compilation.
Example:
#include<stdio.h>
#include<math.h>

1. Definition

 Pre-processors 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.
Pre-processor directives start with the ‘#’ symbol.
 The #define pre-processor 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.

Example:

#define long long ll

1. Global Declaration
 The global declaration section contains global variables, function declaration, and static
variables.
 Variables and functions which are declared in this scope can be used anywhere in the
program.
Example:
int num = 18;

1. Main() Function
 Every C program must have a main function.
 The main() function of the program is written in this section.
 Operations like declaration and execution are performed inside the curly braces of the main
program.
 The return type of the main() function can be int as well as void too.
 void() main tells the compiler that the program will not return any value.
 The int main() tells the compiler that the program will return an integer value.

Example:
void main()
or
int main()

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.

Example:

int sum(int x, int y)


{
return x+y;
}

Example Program

/ Documentation
/**
* file: sum.c
* author: you
* description: program to find sum.
*/

// Link
#include <stdio.h>

// Definition
#define X 20

// Global Declaration
int sum(int y);

// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}

// Subprogram
int sum(int y)
{
return y + X;
}

Variables in C:

 A variable is like a container (storage space) with a name in which you can store data.
 When you create a variable in C programming a new memory space gets assigned to the
variable.
 While creating a new variable you have to provide it a name and specify data type for the
variable.
 Once you have created the variable, you cannot change its data type.

 When we create a variable in a C program, the C compiler allocates a storage space,


depending upon the data type of the variable(8 bits for char, 16/32 bits for int, etc.)
 Then that storage space is given a name which is the variable name.
 Once a variable is created, we can store value in it.
 We can change the value stored in a variable, but we cannot change the datatype of the
value stored in it.
Syntax for using Variable:
Here is how you can create or declare a new variable in the C language,
data_type var_name;
.
For example,
int marks;

Here, marks is the name of the variable, and it can store values of int type.
Assign value to Variable
Once we have declared or created the variable, then we can assign a value to it. This is
called variable definition.
// variable declaration
int marks;
// variable definition
marks = 10;
Copy
We can do declaration and definition in a single step too, like this (recommended).
int marks = 10;

Constant in c:

C Constant value Variables - const Keyword

 In C programming, a constant value is a value that cannot be changed.


 Using constants, you can assign a fixed value to a variable, so that the value of the variable is
not changed by mistake during program execution.
 To define a constant value variable you can use the const keyword.
Types of Constants
In C programming Following are the different types of constants in C programming:
1. Integer Constants
Integer values are values that have no fractional part or decimal point. For example, 7, 0, -7,
etc.
2. Floating-point Constants
Floating-point values are real numbers with fractional parts or decimal point. For example,
3.14, -2.65, etc.
3. Character Constants
These are single-character constants enclosed within single quotes, for example, 'A', 'a', '#',
etc.
4. String Constants
These are set of characters, or text, enclosed within double quotes, for example,
"Studytonight", "I love C", etc.
5. Enumeration Constants
An enumeration in C language is a set of named integer values. Enumeration or Enum is called
user-defined constants.
Using const Keyword
 The const keyword is used to create a variable as a constant.
 When you create a variable using the const keyword, you have to assign it a value while
creating the variable.
 Once you have assigned a value to a const variable, the value stored in the variable cannot
be changed.
 You can use the const keyword with any type of variable.

example of using the const keyword,


const int totalmarks = 100;

4. Explain data types in c.


Each variable in C has an associated data type. It specifies the type of data that the variable can
store like integer, character, floating, double, etc.
C is a statically type language where each variable’s type must be specified at the declaration
and once specified, it cannot be changed. This is because each data type requires different
amounts of memory and allows type specific operations.

Type Example

Basic character, integer, floating-point, double.

Derived Array, structure, union, etc.

Enumeration enums

Bool type true or false

void Empty value

You might also like