Vous êtes sur la page 1sur 50

Software Development

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

Testing and Debugging


o To identify the position of errors. o To remove the errors.

Software Development Cont


Implementation Modification and Maintenance

Design a program to add two numbers.


/* program to add two numbers */
#include <stdio.h> //include header file int main( ) { int a, b, sum; printf(Enter two values: ); //display statement scanf(%d%d, &a, &b); //input statement sum = a + b; printf(The sum is: %d, sum); //output statement return 0; }

Some commands:
F1 details of error & help Ctrl+F1 Detailed help of a construct Alt + F9 Compilation only Ctrl + F9 Run Alt + F5 user / result screen F2 Save F9 Make exe file on hard disk F3 to open a new or existing file

The text inside /* and */ is called comment or documentation. The statement starting with # (hash sign) is called pre-processor statement. stdio.h is a header file.
o Prototype or declaration only of the library functions o Predefined constants

Header file does not contain the code of library functions. It only contains the header or prototype. A program may contain many functions, but it essentially contains a main function. The return type of main function is kept int type. A program should return value.
o 0 (zero) in case of normal termination.Non-zero in case of abnormal termination, i.e. termination due to some error.

Steps of Program Execution


Pre-processing Compilation
Linking Loading Execution
Alt + F9

Ctrl+ F9

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 Linking: Symbolic Links are resolved in this phase
o If there is no error the compiler generates the object code (.obj) 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 o o o o Instruction Fetch Instruction Decode Operand Fetch Operand Execute

Execution: The Instruction of object code are executed one by one. It has four major steps

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 Logical Error: Produced due to wrong logic of program. Tough to identify and even tougher to remove. o Variables not initialized o Boundary case errors in loops o Misunderstanding of priority and associativity of operators

Types of Statements in C Prog.


Preprocessing Statements: Also called compiler directives. The purpose depends on the type of commands. o # include statement inserts a specified file in our program o They don't exist after compilation Declarative Statements: Used to declare user identifier i.e. to declare data types for exampe: int a, b; o These statements do not exist in the object code. o Only used to produce symbol table

Types of Statements in C Prog. Cont...


Executable Statements: The statements for which the executable or binary code is generated. For o Input/Output Statements like printf(), scanf() o Assignment Statements. The syntax is lvalue=rvalue o Conditional Statements like if(a>b) max =a; else max=b; o Looping Statements. Also called Iterative statements or repetitive statements. For while do while o Function Call like y=sin(x);

Types of Statements in C Prog. Cont...


Special Statements: There are four special statements o break o continue o return o exit

Keywords & Identifiers


The meaning of some words is reserved in a language which the programmer can use in predefined manner. Theses are called keywords or reserve words. For example: do, while, for, if, break, etc The programmer uses his words for preparing a program. These words are used to store final or intermediate results. These words are called identifiers. The programmer has to define the type of identifier he is using. For example: a, b, sum, etc.. These are two categories of identifiers.
o Variable eg. int a; o Constant literal eg #define PI 3.14

Rules and Conventions of Making Identifier


The first character must be an alphabet. Remaining characters may be alpha- numerals (alphabets + numeric digits): No special character other than underscore __ is permitted. Space is not permitted. The maximum length is compiler defined. Reserve words can not be taken as identifiers.

Data Types in C
Standard data type
o Simple data type
char Int float

o Structured/composite data type


array struct union

o Pointer

Data Types in C
User Defined data type o enum o typedef The char holds a character or a small integer. It has two modifies: o Unsigned o Signed The int holds a whole number. It has two types of modifiers: o Type 1: Signed unsigned o Type 2: Short Int long

Data Types in C
The float can hold a number with fraction parts. It has three modifiers:
o float o double o long double

Name
Char

Description
Character

Size
1byte

Range
signed: -128 to 127 unsigned: 0 to 255 signed: -32768 to 32767 unsigned: 0 to 65535 signed: -32768 to 32767 unsigned:- 0 to 65535 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295

short int (short) Int

Short Integer. Integral data type

2bytes

Integer

2bytes/ 4 bytes

long int (long)

Long integer.

4bytes

float

Floating point number.

4bytes

3.4e +/- 38 (7 digits)

double

Double precision floating point number. Long double precision floating point number.

8bytes

1.7e +/- 308 (15 digits)

long double

10bytes

??

Character and integer data types use the same internal format. The only difference is the number of bytes allocated.

#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 left-to-right right-to-left left-to-right left-to-right left-to-right left-to-right left-to-right left-to-right left-to-right left-to-right left-to-right left-to-right right-to-left right-to-left left-to-right
Decreasing Priority

Operators . () []

Description Highest priority operators all unary operators arithmetic operators arithmetic operators

++, --, !, ~, +, -, *, &, sizeof( ) * / %

+ << >>

bit shift operators relational operator relational operator

<, <=, >, >=, = =, != & ampersand ^ caret | pipeline

bit wise operator

&& || ? : Logical operator ternary/conditional operator assignment operator sequencing operator

=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= ,

There are three types of operators available in C: unary that requires only one operand. o For example: &a, ++a, *a binary - that requires two operands. o For example: a + b, a*b ternary - that requires three operands. o For example: (a>b) ? a : b [ ? : ] Priority and associativity: The higher priority operator is operated first. If the priority or precedence is same then associativity is applied. o a+b*c o (a + b) * c o x=a+b o *a++ o a-b+c o a *= b += c o a / b*c o a+b+c o x = a++ ?

Token
In a C source program, the basic element recognized by the compiler is the token. Token is source-program text that the compiler does not break down into component elements.
o keyword, identifier, constant, string-literal, operator, punctuator

Operators in Details
The / (the division) operator:
Data type of op1 Data type of op2 Data type of result

int float

int int

int (fractional part truncated) float

int
float

float
float

float
float

Operators in Details
Modulus operator Increment / Decrement Operators ( ++, - -)
o o o o Pre-increment ++a Post increment a++ Pre-decrement - -a Post-decrement a - -

The expressions in which a single variable is incremented and decremented and used more than once, must be strictly avoided. These are confusing. Moreover, the compiler may behave in unpredictable manner.

Operators in Details
Assignment Operators: An assignment statement is used to assign the value of an expression to a single variable. The syntax is: lvalue = rvalue
It can only be a single variable a variable, a constant, an expression, function call

Printf()
Writes to the standard output (stdout) a sequence of data formatted as the format argument specifies. After the format parameter, the function expects at least as many additional arguments as specified in format. The format tags follow this prototype: %[flags][width][.precision][length]specifier

specifier

Output

Example

c d or i e E f g G o s u x X p n

Character Signed decimal integer Scientific notation (mantissa/exponent) using e character Scientific notation (mantissa/exponent) using E character Decimal floating point Use the shorter of %e or %f Use the shorter of %E or %f Signed octal String of characters Unsigned decimal integer Unsigned hexadecimal integer Unsigned hexadecimal integer (capital letters) Pointer address Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored. A % followed by another % character will write % to stdout.

a 392 3.9265e+2 3.9265E+2 392.65 392.65 392.65 610 sample 7235 7fa 7FA B800:0000

The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:
flags description

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. Used with o, x or X specifies the value is preceded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).

width

description
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.

(number)

.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

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).

Return Value: On success, the total number of characters written is returned. On failure, a negative number is returned.

Example
/* fprintf example */ #include <stdio.h> int main( ) { printf ("Characters: %c %c \n", 'a', 65); printf ("Decimals: %d %ld\n", 1977, 650000); printf ("Preceding with blanks: %10d \n", 1977); printf ("Preceding with zeros: %010d \n", 1977); printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100); printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416); printf ("Width trick: %*d \n", 5, 10); printf ("%s \n", "A string"); return 0; }

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.

Short circuit evaluation

The if Statement
It is used to selectively execute a set of statements. The set of statements is executed, if the condition is true
s0; if (c) s1; s2; s0; if (c) { s1; s2; } s3;

if..else structure:
s0; if (c) { s1; s2; } else { s3; s4; } s5;

//ex6.c #include <stdio.h> int main(){ int a = 0, b = 5, c = 0; if (c=a) printf(\n Hello); else printf(in world); if (c = b) printf(\nHello); else printf(\nWorld); return 0;}

OUTPUT ?

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; }
a: 2 bytes 11 11.0 temp: 4 bytes float type b: 2 bytes 2 This location is destroyed after calculation c: 4 bytes

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;

int a, b, c; b=10, c = 20; a = sizeof(b + c); printf( %d, a); return 0; }

The sizeof( ) operator:

It returns the size of its argument in bytes. The argument may be: An identifier A constant A standard data type User defines data type
#include <stdio.h> int main( ) { int a, b, c; double x; a = sizeof(int); b= sizeof( 4); c= sizeof(x); printf( %d%d%d%, a,b,c); return 0; }

#include <stdio.h> int main( ) { int a, b, c;

b=10, c = 20;
a = sizeof(b + c); printf( %d, a); return 0; }

Low Level Operators and Bit Fields


Many programs (e.g. systems type applications) must actually operate at a low level where individual bytes must be operated on. Table: Bitwise Bitwise Operators operators & AND X=00000010 | 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; s2) { for ( s1; 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;
s0;
while (c) { s1; s2; } s3;

s0;
while (c) s1; s2; s3;

s0;
while (c); s1; s2;

do .. while :
It is exit control and indefinite type loop.
s0; do s1; while (c); s2;

Difference between while and do-while : In while the condition is checked at the beginning and in do while the condition is checked at the last. It is possible that the stmt inside while may not be executed even for a single time, if the condition is false for the first time. In do-while the stmt inside the loop are executed at least once.

Vous aimerez peut-être aussi