Académique Documents
Professionnel Documents
Culture Documents
Problem Definition Analysis & Design Coding Testing and debugging Implementation Modification and maintenance
Analysis
o Interviewing the persons involved o Question/answer o Documents reading o Observation / monitoring etc.
Design
o Form Design GUI for input o Report Design for output o Database Design to store data
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.
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
Data Types in C
Standard data type
o Simple data type
char Int float
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
2bytes
Integer
2bytes/ 4 bytes
Long integer.
4bytes
float
4bytes
double
Double precision floating point number. Long double precision floating point number.
8bytes
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
+ << >>
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= ,
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
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.
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
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; }
b=10, c = 20;
a = sizeof(b + c); printf( %d, a); return 0; }
<< >>
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; }
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.