c notes
c notes
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.
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 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.
Disadvantages of Algorithms:
Example
1. START
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.
7. END
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:
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:
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
Example:
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.
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:
Type Example
Enumeration enums