Professional Documents
Culture Documents
Problem Definition
Analysis & Design
Coding
Testing and debugging
Implementation
Modification and maintenance
Software Development
Cont…
Problem Definition
o What is to be done?
o How much is to be done?
Analysis
o Interviewing the persons involved
o Question/answer
o Documents reading
o Observation / monitoring etc.
Software Development
Cont…
o Design
• ER diagram
• Data flow diagram
• Condition table
• System flow chart
Design
o Form Design – GUI – for input
o Report Design – for output
o Database Design – to store data
Software Development
Cont…
Coding
o As per the requirement
Pre-processing
Alt + F9
Compilation
Linking
Loading Ctrl+ F9
Execution
Pre-processing:
o Pre-processing statements are processed first
o All the statements starting with #(hash) sign are
preprocessing statements
o eg. #include and #define
Compilation: The syntactical validity of the
complete program is checked
o If there is no error the compiler generates the object
code (.obj)
Linking: Symbolic Links are resolved in this phase
o A function call is called symbolic link
o A function can be user defined or ready made function
in the library
o The linker substitutes the name of the function with the
address of the first instruction of the function
Execution: The Instruction of object code are
executed one by one. It has four major steps
o Instruction Fetch
o Instruction Decode
o Operand Fetch
o Operand Execute
Types of Errors
Syntax error: When there is the
violation of the grammatical ( Syntax)
rule. Detected at the compilation time.
o Semicolon not placed
o Standard Construct not in proper format
o Identifier not declared
Linker Error: When definition or code of
a program is not found.
o Path of header not properly defined
o Library file not found
o The function name is misspelled
Types of Errors Cont...
Runtime error: It is generated during execution phase. Due
to mathematical or some operation generated at the run
time.
o Division by zero
o Square root of negative number
o File not found
o Trying to write a read only file
double Double precision floating 8bytes 1.7e +/- 308 (15 digits)
point number.
#include <stdio.h>
int main( )
{
char ch = 97; // char ch = ‘a’;
int x = ‘A’;// int x = 65;
printf(“\nCharacter: %c, Equivalent integer:
%d”, ch, ch);
printf(“\nEquivalent Character: %c, integer:
%d”, x, x);
return 0;
}
Operators available in C
Associativity Operators Description
left-to-right . () [] Highest priority operators
c Character a
d or i Signed decimal integer 392
e Scientific notation (mantissa/exponent) using e character 3.9265e+2
n Nothing printed. The argument must be a pointer to a signed int, where the number of
characters written so far is stored.
- Left-justify within the given field width; Right justification is the default (see width
sub-specifier).
+ Forces to preceed the result with a plus or minus sign (+ or -) even for positive
numbers. By default, only negative numbers are preceded with a - sign.
(space) If no sign is going to be written, a blank space is inserted before the value.
0 Left-pads the number with zeroes (0) instead of spaces, where padding is
specified (see width sub-specifier).
width description
(number) Minimum number of characters to be printed. If the value to be printed is shorter than this number,
the result is padded with blank spaces. The value is not truncated even if the result is larger.
* The width is not specified in the format string, but as an additional integer value argument
preceding the argument that has to be formatted.
.precision description
.number For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be
written. If the value to be written is shorter than this number, the result is padded with leading
zeros. The value is not truncated even if the result is longer. A precision of 0 means that no
character is written for the value 0.
For e, E and f specifiers: this is the number of digits to be printed after the decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are
printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit
value for precision, 0 is assumed.
.* The precision is not specified in the format string, but as an additional integer value argument
preceding the argument thas has to be formatted.
length description
Characters: a A
Decimals: 1977 650000
Preceding with blanks: 1977
Preceding with zeros: 0000001977
Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick: 10
A string
scanf( ):
scanf( ) statement is used to read data from
standard input stream (stdin), the keyboard. The
syntax is:
scanf( format string with specifiers, variable
names)
The specifier list is same as that of printf( ).
Example:
char x;
int y;
float z;
scanf(“%c%d%f”, &x, &y, &z);
scanf( ) returns the number of values successfully
read.
Expression
There are two types of expressions:
Arithmetic expression: Returns a value
Relational / Logical / Conditional
Expression: Returns true or false
if a relational expression is true, it returns
numeric value 1
if a relational expression is false, it
returns numeric value 0
Logical Operator
There are three logical operators &&
(AND), | | (OR), ! (NOT). These are used
to combine the conditions.
s0; //ex6.c
if (c) { #include
s1;
<stdio.h>
s2;
} int main(){
else { int a = 0, b =
s3; 5, c = 0; OUTPUT ?
s4; if (c=a)
}
printf(“\n
s5;
Hello”);
else
printf(“in
world”);
if (c = b)
Type Conversion
A variable/value of one type can be converted into
variable/value of another type. There are two cases:
Implicit conversion: Values can be converted by assigning one
expression into the other. There are two types:
o widening conversion
o narrowing conversion
char short int longfloat double long double
Assignment in forward direction is widening conversion; no data is lost in it.
Assignment in backward direction is called narrowing conversion; data loss is
possible in this case.
char a = 10;
int b, c;
float d = 2.6;
b = a; // widening conversion, no data loss
c = d; // narrowing conversion, data loss, c gets value only 2
Narrowing conversion must be type casted. c = (int)d;
Explicit conversion (Type Casting): Type
casting is used to convert the data type of an
identifier to another data type temporarily.
#inlcude <stdio.h>
int main( ) {
int a=11, b=2;
float c;
c = (float)a / b;
printf(“\n%f”, c);
return 0;
}
The switch statement
The C allows multiple choice of a selection of
items at one level of a conditional where it is a
far neater way of writing multiple if statements:
switch (expression) {
case item1:
statement1;
break;
case item2:
statement2;
break; case itemn:
statementn;
break;
default:
statement;
break;
}
b=10, c =
20;
& AND
| OR
^ XOR
~ One's Compliment
10
01
<< Left shift
>> Right Shift
Shifting is much faster than actual multiplication (*) or division (/) by 2. So if you
want fast multiplications or division by 2 use shifts.
#include <stdio.h>
int main() {
char x = 12, y=10, a, b, c, d;
clrscr();
a = x | y;
printf("\n%d", a);
b = x & y;
printf("\n%d", b);
c = x ^ y;
printf("\n%d", c);
d = ~x;
printf("\n%d", d);
return 0;
}
The for loop
For loop consist of two parts.
Header
body – C statements that are to be executed repeatedly.
The header contains three sections:
o Initialization
o Condition
o Statement
s0;
for ( s1; c; s2)
s3;
s4;
Break and continue
Statement:
Break is used at two places:
• inside switch..case statement
• inside the loop
As soon as break statement is encountered, the execution comes out of the
structure immediately. If break is inside the loop, the loop is terminated.
As soon as continue statement is encountered, the execution comes to the
condition checking immediately, that is, the statements below continue are
skipped.
s0;
s0;
for ( s1; c1;
for ( s1;
s2) {
c1; s2) {
s3;
s3;
if (c2)
if (c2)
continue;
break;
s4;
s4;
}
}
s5;
s5;
While Loop:
It is an entry control indefinite type loop.
s0;
while (c)
s1;
s2;
#include <stdio.h>
/* prototypes */
int hcf(int x, int y );
main( ) {
int a, b, result;
printf( "\n enter two numbers: ");
scanf( "%d%d", &a, &b);
result = hcf(a, b);
printf( "\n The hcf is %d: ", result);
}
/* function to find hcf input : two numbers x and y output: hcf of x
and y*/
int hcf(int x, int y ){
int result, smaller;
smaller = ( x< y) ? x : y; /* get smaller number */
/* loop to generate numbers from smaller downto 1 */
for (result = smaller ; result >= 1; --result ) {
if ( ( x% result == 0) && ( y% result == 0) ) /* result
divides x & y bath */
return result; /* result is the hcf */
} /*for */
countDigit.c :
/* /* function to count digits of a
program to count digits of a number
number */ intput : a number x
output : count of digits of x*/
#include <stdio.h>
int countDigit(long x){
#include <conio.h> int count = 0;
int countDigit( long n ); /* repeat till x contains atleast 1
void main( ){ digit */
long n; while (x > 0){
int count; x = x/10;/* truncate last digit */
clrscr(); ++count;
printf("\n Enter number :"); }
scanf("%ld", &n); return count;
}
count = countDigit(n);
printf("\nThe number of digits
is: %d", count);
}
Scope & Lifetime:
Scope/Visibility: It is the area of a program in which an identifier is
accessible / visible or usable.
Lifetime: It is the time duration for which the space will remain allocated
for a variable.
Local/auto
Register
Static
Extern / global
Automatic: auto
storage is automatically allocated on
function/block entry and automatically freed
when the function/block is exited
may not be used with global variables (which
have storage space that exists for the life of the
program)
auto is the default for function/block variables
o auto int a is the same as int a
o because it is the default, it is almost never used
Register:
register provides a hint to the compiler that you
think a variable will be frequently used
compiler is free to ignore register hint
if ignored, the variable is equivalent to an auto
variable with the exception that you may not take
the address of a register (since, if put in a register,
the variable will not have an address)
rarely used, since any modern compiler will do a
better job of optimization than most programmers
Static Storage: static
if used inside a block or function, the
compiler will create space for the variable
which lasts for the life of the program
int counter(void) {
static int cnt = 0;
return cnt++;
}
causes the counter( ) function to return a
constantly increasing number
External References: extern
If a variable is declared (with global scope) in one file but
referenced in another, the extern keyword is used to
inform the compiler of the variable's existence:
In a file declare.h:
int x; //definition //space is allocted
In a file use.c:
{
extern int x; //declaration : no new variable is being
declared
int a;
a = x * 2;
}
Note that the extern keyword is for declarations, not
definitions
An extern declaration does not create any storage; that
must be done with a global definition
Private Variables: static
Another use for the static keyword is to ensure that code outside this file cannot
modify variables that are globally declared inside this file
If declare.c had declared farvar as:
static int farvar;
then the
extern int farvar
statement in use.c would cause an error.
This use of static is commonly used in situations where a group of functions need
to share information but do not want to risk other functions changing their
internal variables.
static int do_ping = 1; /* start with `PING' */
void ping(void) {
if (do_ping = = 1) {
printf("PING ");
do_ping = 0;
}
}
void pong(void) {
if (do_ping = = 0) {
printf("PONG\n");
do_ping = 1;
}
}
Variable Initialization
auto, register and static variables may be initialized at creation:
int
main(void)
{
int a = 0;
register int start = 1234;
static float pi = 3.141593;
}
Any global and static variables which have not been explicitly
initialized by the programmer are set to zero
If an auto or register variable has not been explicitly initialized, it
contains whatever was previously stored in the space that is
allocated to it
o this means that auto and register variables should always be
initialized before being used
o compiler may provide a switch to warn about uninitialized
variables
ARRAYS
Introduction: An array is a structured/composite data
type. We can store more than one element in an array
simultaneously. An array is also called subscript variable.
One Dimensional Array: Arrays are similar to matrices.
There can be single dimensional array or
multidimensional array.
Ex:
int a[5];
Data type maximum
Of element permitted size;
array name
i.e. maximum 5 elements can be stored in this array.
All element of the array are of same data type. In this sense, it is a
homogenous data structure.
Maximum permitted size is 5. It means that we can store maximum
five integers in this array. These five elements are referred to as:
indices / subscript
In general an element can be referred to as: a[ i ], where i varies from
0 to 4 here, i is called index or subscript.
The allocation information about an array is fixed at compile time. Its
entry is done in symbol table at compile time. It means that it is static
allocation.
During execution the memory space is allocated for array. For all
element of array contiguous space is allocated. The array name is the
start address of this memory.
<100> <102> <104> <106> <108> <110>
int main( )
{
int n =5;
int a[n];
maximum size is variable
} (not possible, syntax error)
Normally declaration is done in this manner.
#define MAX 10
void main( )
{
int a[MAX];
}
It is because #define is processed during preprocessing phase; so in
compilation phase the compiler gets 10 in place of MAX.
The maximum size of array remains fixed, once declared; but the current
size of the array may vary. But, the current size can never be greater than
maximum size. The space is reserved according to maximum size. We can
use all or less locations. If the maximum size is 10 and current size is 6,
then the remaining 4 locations will remain reserved and empty. These
locations can not be used for any other purpose.
An array can be initialized at the time of declaration. { Demo initArray.c }
int a[4] = {10, 20, 30, 40};
int b[4] = {10, 20};
int c[ ] = {10, 20, 30, 40};
int d[4] = {0};
Ex: Declare an array of integer; read its current size; prepare a loop to read; display the loop in another array.
#include <stdio.h>
#define MAX 50
int main( ) {
int a[max];
int i, n;
/* read current size of array
*/
printf( “Enter current
size:“ );
scanf( “%d”, &n);
/*loop to read array */
for (i = 0; i < n; ++i) {
printf( “\nEnter %dth
element: ”, i);
scanf(“%d”, &a[i]);}
/*loop to display array */
for (i = 0; i < n; ++i)
printf( “\nThe %dth
element is: %d”, i, a[i] );
There is no bound checking of array in C and C++ neither at compile time nor at
execution time. But, if the subscripts are beyond the valid limits, the results will be
garbage and the behavior of the program may be peculiar. Hence, it is the
responsibility of the programmer to keep the indices within the valid limit.
NOTES:
For calling a function in which an array is passed as parameter, only array name is
used.
The name reflects the start address, so only start address of the array is passed.
When array is passed the start address is passed, not the complete array. It
provides efficiency - it saves memory & time.
Since only the base address is passed, the original data locations are accessed by
the called function. So, any changes, made in the called function are reflected in
the calling function (pass-by-reference).
The contents of the array read in the called function read_1D() are available in the
calling main() function.
In called function header, when parameters are specified, there is no need of
specifying the maximum size of the array.
Ex-: Design a modular program to read and display a one dimensional array. The two functions to
be made are to read and display the array. The main() function calls these functions.
Maximum Maximum
numb of rows numb of columns
a[0][0] a[2][1]
70 63 59
51 43 72
63 82 95
22 84 18