Sunteți pe pagina 1din 30

PROGRAMMING IN C

UNIT-I:
Introduction:
 C was invented to write an operating
system called UNIX.
 C is a successor of B language which
was introduced around the early 1970s.
 The language was formalized in 1988 by
the American National Standard Institute
(ANSI).
 The UNIX OS was totally written in C.
 Today C is the most widely used and
popular System Programming Language.
 Most of the state-of-the-art software
have been implemented using C.
 Today's most popular Linux OS and
RDBMS MySQL have been written in C
Problem solving:
Algorithm design strategies are
techniques used to modify algorithm executions
and performance.
we can measure this modifications
through algorithm analysis. Let us discuss a
simple example to see the difference in
efficiency that we can make through algorithm
design techniques;
Consider the linear search algorithm, it is
searching for a key in an array then returning its
index if exists or failure otherwise. It is visiting
the whole array to get that index of the key!
Simple algorithms:
Exchanging the values or two variables or
swapping the values:
Sum of set of integer values:
#include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;
printf("Enter the number \n");
scanf("%ld", &num);
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp,
sum);
}
output:
Enter the number
300
Given number = 300
Sum of the digits 300 = 3
Fibonacci sequence program:
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are:\n",
n);
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}
return 0;
}
Output:
Flow chart:
A flowchart is a type of diagram that
represents an algorithm, workflow or process. The
flowchart shows the steps as boxes of various kinds,
and their order by connecting the boxes with arrows.
This diagrammatic representation
illustrates a solution model to a given problem.
Flowcharts are used in analyzing, designing,
documenting or managing a process or program in
various fields.
Flow chart symbols:
Oval(start/end):

This is the first and the last symbol in every flow


chart. I like to use ellipse for “begin” and “end”.
When I divide an algorithm in several parts I use
small circles for the start/end of each part.

Rectangle:

You will use a rectangle to perform an


action (also called "statement"). More precisely, we
use them for assignment statements - when you
change a value of a variable.
Parallelogram:

The parallelogram flow chart symbol serves for


input/output from the program.
Rhombus:
This is what we use when our program has to make
a decision. This is the only block that has more than
one exit arrow.
Arrows and junctions:

Arrows show the order of the blocks in the chart


flow. There is no convention for the direction of the
arrows.
We choose which direction is better for our graphics.
Overview of c:
 C was invented to write an operating
system called UNIX.
 C is a successor of B language which
was introduced around the early 1970s.
 The language was formalized in 1988 by
the American National Standard Institute
(ANSI).
 The UNIX OS was totally written in C.
 Today C is the most widely used and
popular System Programming Language.
 Most of the state-of-the-art software
have been implemented using C.
 Today's most popular Linux OS and
RDBMS MySQL have been written in C
Basic structure of c program:
Any C program is consists of 6 main sections.
Below you will find a brief explanation of each of
them.
1. Documentation Section
2. Link Section
3. Definition Section
4. Global Declaration Section
5. main() Function Section

6.Subprogram Section
Documentation Section:
This section consists of comment lines
which include the name of the program, the name of
the programmer, the author and other details like
time and date of writing the program.
/*Documentation Section:
Program Name: Program to find the area of
circle
Author: Rumman Ansari
Date : 12/01/2013
Time : 10 AM
*/
Link Section:
The link section consists of the header files of the
functions that are used in the program.
#include"stdio.h" //link section
#include"conio.h" //link section/
Definition Section:
All the symbolic constants are written in the
definition section. Macros are known as symbolic
constants.
#define PI 3.14 //definition section
Global Declaration Section:
The global variables that can be used
anywhere in the program are declared in the global
declaration section. This section also declares the
user defined functions.
float area; //global declaration
section
main() Function Section:
It is necessary to have one main() function
section in every C program. This section contains
two parts, declaration and executable part.
The declaration part declares all the variables
that are used in executable part. These two parts
must be written in between the opening and closing
braces.The execution of the program starts at
opening braces and ends at closing braces.
Declaration part:
The declaration part declares all the variables used
in the executable part.
Executable part:
There is at least one statement in the executable
part. These two parts must appear between the
opening and closing braces.
void main()
{
float r; //declaration part
printf("Enter the radius of the circle\n");
//executable part start here
scanf("%f",&r);
area=PI*r*r;
printf("Area of the circle=%f \n",area);
message();
}
Subprogram Section:
The subprogram section contains all the user
defined functions that are used to perform a specific
task. These user defined functions are called in the
main() function.
ex:
void message()
{
printf("This Sub Function \n");
printf("we can take more Sub Function \n");
}
Developed a C program:
The following steps are used in sequence for
developing an efficient program:
1. Specifying the problem statement
2. Designing an algorithm
3. Coding
4. Debugging
5. Testing and Validating
6. Documentation and Maintenance.
Specifying the Problem:
The Problem which has to be
implemented into a program must be thoroughly
understood before the program is written.
Designing an Algorithm:
With the problem statement
obtained in the previous step, various methods
available for obtaining the required solution are
analyzed and the best suitable method is designed
into algorithm.
Coding:
The actual program is written in the
required programming language with the help of
information depicted in flow charts and algorithms.
Debugging:
There is a possibility of occurrence of
errors in programs. These errors must be removed to
ensure proper working of programs.
Testing and Validating:
Testing and Validation is performed
to check whether the program is producing correct
results or not for different values of input.
Documentation and Maintenance:
Documentation is the process of
collecting, organizing and maintaining, in written
the complete information of the program for future
references. Maintenance is the process of upgrading
the program according to the changing
requirements.
Common programming error:
Types of errors that may occur in the
program are:
Syntactic Errors:
These errors occur due to the usage of wrong syntax
for the statements.
Syntax means rules of writing the program.

Example: x=z*/b;
Runtime Errors:
These Errors are determined at the execution time of
the program.
Example: Divide by zero Range out of bounds.
Logical Errors:
These Errors occur due to incorrect usage of the
instruction in the program. These errors are neither
displayed during compilation or execution nor cause
any obstruction to the program execution.
Constants:
Constants refer to fixed values that
the program may not alter during its execution.
These fixed values are also called literals.
Constants can be of any of the basic
data types like an integer constant, a floating
constant, a character constant, or a string literal.
There are enumeration constants as well.
Constants are treated just like regular
variables except that their values cannot be
modified after their definition.
Variables:
C variable is a named location in a
memory where a program can manipulate the data.
This location is used to hold the value of the
variable.
The value of the C variable may get
change in the program.
C variable might be belonging to any of
the data type like int, float, char etc.
RULES FOR NAMING C VARIABLE:
 Variable name must begin with letter or
underscore.
 Variables are case sensitive
 They can be constructed with digits, letters.
DECLARING & INITIALIZING C VARIABLE:
 Variables should be declared in the C program
before to use.
 Memory space is not allocated for a variable
while declaration. It happens only on variable
definition.
 TYPES:
Local variable
Global variable
Environment variable
example:
#include<stdio.h>
void test();
int main()
{
int m = 22, n = 44;
// m, n are local variables of main
function
}
Data types:
Data types specify how we enter data
into our programs and what type of data we enter. C
language has some predefined set of data types to
handle various kinds of data that we can use in our
program.
These datatypes have different storage
capacities.
C language supports 2 different type of data
types:
Primary data types:
These are fundamental data types in C
namely integer(int), floating point(float),
character(char) and void.
Derived data types:
Derived data types are nothing but primary
datatypes but a little twisted or grouped together
like array, stucture, union and pointer.
Operators and expressions:
The symbols which are used to perform
logical and mathematical operations in a C program
are called C operators.
These C operators join individual constants
and variables to form expressions.
Operators, functions, constants and
variables are combined together to form
expressions.
Consider the expression A + B * 5. where, +,
* are operators, A, B are variables, 5 is constant
and A + B * 5 is an expression.
Types of operators:
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators (ternary operators)
7. Increment/decrement operators
8. Special operators
Arithmetic operator:
Arithmetic Operators
Example
Addition
A+B
Subtraction
A-B
multiplication
A*B
Division
A/B
Modulus
A%B
RELATIONAL OPERATORS IN C:
Relational operators are used to find the relation
between two variables. i.e. to compare the values of
two variables in a C program.

Operators
Example/Description
>
x > y (x is greater than y)
< x<y
(x is less than y)
>= x >=
y (x is greater than or equal to y)
<= x
<= y (x is less than or
equal to y)
==
x == y (x is equal to y)
!=
x != y (x is not equal to y)
RELATIONAL OPERATORS IN C:
Relational operators are used to find the
relation between two variables. i.e. to compare the
values of two variables in a C program.
Operators Example/Description
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or
equal to y)
<= x <= y (x is less than or equal
to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)
LOGICAL OPERATORS IN C:
These operators are used to perform logical
operations on the given expressions.
There are 3 logical operators in C language.
They are, logical AND (&&), logical OR (||) and
logical NOT (!).
BIT WISE OPERATORS IN C:
These operators are used to perform bit
operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise
operators work on these bits.
Bit wise operators in C language are &
(bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^
(XOR), << (left shift) and >> (right shift).
TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE
OPERATORS:
CONDITIONAL OR TERNARY OPERATORS IN C:
Conditional operators return one value if
condition is true and returns another value is
condition is false.
This operator is also called as ternary
operator.
Syntax : (Condition true_value:false_value);
Example : (A > 100 ? 0 : 1);
Increment or decrement operator:
Increment operators are used to increase
the value of the variable by one and decrement
operators are used to decrease the value of the
variable by one in C programs.
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : ––i; i––;
//Example for increment operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
SPECIAL OPERATORS IN C:
Below are some of the special operators that the C
programming language offers.
(&)This is used to get the address of the variable.
Example : &a will give address of a.
(*) This is used as pointer to a variable.
Example : * a where, * is pointer to the variable a.
Sizeof ():This gives the size of the variable.
Example : size of (char) will give us 1.

S-ar putea să vă placă și