CPPM Unit-1 Introdunction
CPPM Unit-1 Introdunction
CPPM Unit-1 Introdunction
Introduction to C Programming
History of C Programming
Algorithm:
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 1
Computer Programming & Programming Methodology
Example:
Write an algorithm to display the sum of two numbers.
Step-1 Start
Step-5 Stop
Advantages:
▪ It is step-wise representation of a solution to a given
problem, which makes it easy to understand.
▪ It is not dependent on any programming language, so easy
to understand for anyone even without programming
knowledge.
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 2
Computer Programming & Programming Methodology
Disadvantages:
▪ Writing algorithm takes a long time.
▪ An algorithm is not a computer program, it is rather a
concept of how a program should be.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“ Hello World ”);
getch();
}
Practical: (how to do 1st program in C)
1) Open turbo C, by double click in icon.
2) Go to File menu and select New. Blue screen will be display.
3) Include #include<stdio.h> and #include<conio.h> header file.
4) Write void main()
5) Write your program within main().
6) Save your program by click on File menu and select save option or
Press F2 key to save your program.
7) Compile your program by click on Compile menu and select
compile option OR press Alt + F9. (If error occurs then solve
errors at compile time)
8) Run your program by click on Run menu and select Run option
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 3
Computer Programming & Programming Methodology
Flowchart:
A flowchart is the graphical or pictorial representation of a
problem or program or algorithm with the help of different symbols,
shapes and arrows in order to demonstrate a process or a program.
The main purpose of a flowchart is to analyze different processes.
Symbols Meaning
Start/ End
Input/ Output
Process/ Instruction
Decision
Connector / Arrow
Example:
Draw a flowchart for sum of two numbers.
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 4
Computer Programming & Programming Methodology
Start
Sum = a + b
End
Advantages:
▪ The flowchart is an excellent way of communicating the
logic of a program.
▪ It is easy and efficient to analyze problem using
flowchart.
▪ During program development cycle, the flowchart plays
the role to guide or a blueprint, which makes program
development process easier.
▪ It helps the programmer to write the program code.
▪ It is easy to convert the flowchart into any programming
language code as it does not use any specific
programming language concept.
Disadvantages:
▪ The flowchart can be complex when the logic of a
program is quite complicated.
▪ During flowchart is a time consuming task.
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 5
Computer Programming & Programming Methodology
Practical Programs:
Program-1 Write a Program to add two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;
int sum;
a=10;
b=5;
sum=a + b;
printf("Value of sum is %d",sum);
getch();
}
Program-2 Write a Program to Subtract Two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
float a;
float b;
float sub;
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 6
Computer Programming & Programming Methodology
a=10.5;
b=5.2;
sub=a - b;
printf("subtraction is %f",sub);
getch();
float a;
float b;
float div;
clrscr();
a=10;
b=3;
div=a / b;
printf("division is %f",div);
getch();
Structure Programming
In a structure programming, the program is divided into small parts or
functions and each part performs a specific job. The main importance is on
functions rather than data.
Structured Programming Constructs:
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 7
Computer Programming & Programming Methodology
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 8
Computer Programming & Programming Methodology
Interpreter Compiler
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 9
Computer Programming & Programming Methodology
Editor
An editor refers to any program capable of editing files. The
term editor is commonly used to refer to a text editor, which is a
software program that allows users to create or manipulate plain
text computer files.
• Syntax errors
• Run-time errors
• Logical error
Syntax Error:
A syntax error is a violation of the rules of the
programming language.
Example:
Printf(“ hi” )
Syntax errors are relatively easy to find and fix.
Run-time Error:
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 10
Computer Programming & Programming Methodology
Logical Error:
Logical error in program is any code that causes
incorrect output/results even though the program runs to
completion.
A program with logical error may give the correct
answer sometimes and the wrong answer sometimes.
Logical error typically are the most difficult type of the
error to find and correct.
Example:
The following code contains a logic error
because the > and >=.
if( age > 18)
{
printf(“you are eligible for voting”);
}
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 11
Computer Programming & Programming Methodology
Program testing:
• Testing is the process of finding errors in a program.
• Testing can find the presence of errors.
• Testing small program is much easier than testing large programs.
• You should test your program with expected and unexpected input.
o You should know how your program will perform with good
idea as well as bad data.
o Unexpected values may be negative, extremely large, or
extremely small values.
• You should test your program with boundary condition values, i.e
values at and around a value for which the behavior of a program
should change.
o For example, good boundary condition test values for age in the
code above would be 17, 18, 19.
• When testing loops, provide input that cause the loop to repeat zero,
one, and many times.
Program debugging:
Debugging, in computer programming and engineering, is a multistep
process that involves identifying a problem, isolating the source of the
problem, and then either correcting the problem or determining a way to
work around it.
o Debugging is the process of locating and correcting errors in a
program.
o Debugging id problem-solving and often can be very challenging.
o You can help minimize your time spent debugging by:
o Starting with a good program design
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 12
Computer Programming & Programming Methodology
o Coding carefully
o Testing your program as you write.
Documentation section:
Comment can be
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 13
Computer Programming & Programming Methodology
/*………………..
………………….*/
Link section:
#include<file_name>
The link section is also called include section, this section is prefixed by
# sign,
#include<stdio.h>
#include<conio.h>
#include<math.h>
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 14
Computer Programming & Programming Methodology
Definition section:
The definition section defines all symbolic constants using the #define
directive. Having the constants being defined here, we can use them elsewhere
in code.
#define pi 3.14
There are some variables that are used in more than one function; these
variables are called global variables and are declared in this global declaration
section which is outside the definition of any function. This section also
declares all the user-defined functions. As this global scope, these functions
and variables can be used from definition of other functions.
A C program must have one main function section in its structure. This
section contains two parts; declaration part and executable part. However, the
content of these two parts can be mixed.
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 15
Computer Programming & Programming Methodology
These two parts must appear between the opening and closing braces of
the main function. The program execution begins at the opening brace and
ends at the closing brace. The closing brace of the main function is the logical
end of the program. All statements in the declaration and executable part end
with a semicolon.
Subprogram section:
All section, except the main () function section may be absent when they
are not required.
Character Set
The various categories of characters are called the character set. In C, the
characters are grouped into the following categories:
a) Letters:
a. Uppercase: A………….Z
b. Lowercase: a…………..z
b) Digits:
a. All digits: 0…………..9
c) Special Characters, such as
a. , Comma
b. . Period
c. ; Semicolon
d. : Colon
C tokens
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 16
Computer Programming & Programming Methodology
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 17
Computer Programming & Programming Methodology
o Keywords:
Do If static while
o Identifiers:
Example:
Constant
Constants are the fixed values that never change during the
execution of a program. Following are the various types of constants:
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 19
Computer Programming & Programming Methodology
1) Integer constants
a) Decimal Integer
b) Octal Integer
c) Hexadecimal Integer
Decimal Integer:
Example:
o 123
o -321
o 0
o 89765
Octal Integer:
Example:
o 037
o 0
o 043
o 0551
Hexadecimal Integer:
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 20
Computer Programming & Programming Methodology
Example:
o 0X2
o 0x9F
o 0Xbcd
o 0x
2) Real constants
Real constants are used to represent quantities that are very
continuous such as distances, temperature etc.
These quantities are represented by numbers containing fractional
parts.
Example:
0.00832
-0.75
33.337
4) String constant
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 21
Computer Programming & Programming Methodology
Example:
“Hello !”
“1234”
Variables:
A variable is a data name that is used to store data values. A variable
may take different values at different times during execution.
Some examples are average, heights, class_strength.
Variable names may consist of letters, digits and the underscore character,
subject to following conditions.
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 22
Computer Programming & Programming Methodology
Data types:
C language is rich in its data types. ANSI supports three classes of
data types.
• Integer (int)
• Character (char)
• Floating point (float)
• Double-precision point (double)
• Void
i. Integer types:
Integer types are whole numbers with a range of
values supported by particular machine. If we
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 23
Computer Programming & Programming Methodology
• Short int
• Int
• Long int
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 24
Computer Programming & Programming Methodology
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 25
Computer Programming & Programming Methodology
Data_type variable_name;
• Example:
int sum;
Example:
float p, r;
• Initialization:
o Variable initialization means assigning a value to the variable.
Example:
int sum=30;
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 26
Computer Programming & Programming Methodology
Type Syntax
Data_type variable_name
Example:
int x, y, z;
char ch;
Variable initialization Syntax:
Example:
int x=50;
char ch=’h’;