Lec 02 introToC PDF
Lec 02 introToC PDF
C Programming Language
Introduction to C 1
Software Life Cycle
Problem statement
Clear and unambiguous
Problem analysis and design
Analyse the method to solve the problem. Develop
an algorithm. Represent it as flowchart or
pseudocode
Analysis of the algorithm
Check for correctness and required amount of time
and memory to complete the task
Coding
Introduction to C 2
Software Life Cycle ...
Documentation
Write explanations within the program so that
anyone can understand the working, use of
variables, etc. and should be able to modify it.
Testing
Give several test inputs and check if output is as
specified. Detects inconsistencies in the program
Maintenance
Depending on the application, the program may
require changes and updates.
Introduction to C 3
Algorithms
An algorithm is well defined, finite set of computational instructions
that accomplishes a particular task, which may or may not have inputs
and produces some value or a set of values as output. In addition all
algorihtms must satisfy the following criteria:
Zero or more quantities are externally supplied: inputs
At least one quantity is produced: output
Each instruction is clear and unambigious: definiteness
No uncertainity in the instruction
The algorithm terminates after a finite number of steps: finiteness
Must terminate after finite number of steps and reasonable time
For every input instance, it halts with the correct output: correct
Introduction to C 4
Algorithm: Example
Algorithm: Largest
This algorithm computes the largest of three numbers. The variables used are:
x, y, z : type integer
big : storing the value of the largest number, type integer
Step-1 : [Input the three numbers]
read x, y, z
Step-2: [Compute the largest of three numbers]
big = x;
if(y > big) big=y
if(z > big) big=z
Step-3: [write the largest number]
write (big)
Step-4: [Finished]
exit
Introduction to C 5
Flowcharts
Introduction to C 6
Flowchart: Example
Introduction to C 7
Flochart: Example
To find average of three numbers
Introduction to C 8
Pseudocode
English like statements describing the algorithm
Written in structured manner using indentation
Example: to find maximum of n given numbers
Algorithm Max(A,n)
/*A is an array of size n, index starts from 1*/
{
result = A[i];
for i=2 to n do
if A[i] > result then result := A[i];
return result
}
Introduction to C 9
First C program
/* hello.c: hello world program, first example */
#include <stdio.h>
void main()
{
printf(Hello World\n);
printf(Bye.);
}
Introduction to C 10
First C program
Comment
Preprocessor directive
#include <stdio.h>
Function Name
void main()
{
printf(Hello World\n); Body of the
function main
printf(Bye.);
}
Introduction to C 11
First C program
/* hello.c: hello world program, first example */
#include <stdio.h>
$> gcc firstCprog.c
$> ./a.out
void main()
{
printf(Hello World\n);
printf(Bye.);
}
Hello World
Bye.
Introduction to C 12
First C program
/* hello.c: hello world Statement enclosed in curly
program, first example */
braces form a compound
statement
#include <stdio.h>
2 statements in braces after
main() form the function main()
void main()
{ void: Does not return value
printf(Hello World\n);
printf(Bye.);
main(): special function in C
} main is executed first among
all functions in a file
Introduction to C 13
Preprocessor directive
#include <stdio.h>
stdio.h is a file supplied by the C compiler which has
information about various functions used for input-output
Introduction to C 14
Case sensitiveness
Upper and Lower case are recognised
differently
Following are different functions:
Introduction to C 15
Standard input and output devices
The printf function outputs the text on standard output
device.
Usually it is the screen
We can re-direct the output to a file
$> a.out > outputMessages.txt
Here all statements will go to file outputMessages.txt
Output redirection operator: >
User input using scanf can be done from the keyboard or
from a file.
$> a.out < inputStrings.txt
Input redirection operator: <
Introduction to C 16
Statement separation
Separated by semicolons
Can write multiple statements in one line
printf(Hello World \n); printf(Bye.);
Can also split a statement across lines
printf(
Hello World \n);
printf(
Bye.);
C uses semincolon (;) to separate them
Introduction to C 17
Variables and Data types
Character Set
Alphabets
A to Z
a to z
Digits
0 to 9
Special characters
Next slide lists all
White space characters
Blank spaces, formfeed, newline, horizontal tab,
carriage return, vertical tab
Special characters
, comma & ampersand
. period ^ caret
; semicolon * asterisk
: colon - minus sign
? question mark + plus sign
' apostrophe < opening angular bracket or less than sign
" quotation mark > closing angular bracket or greater than sign
! exclamation mark ( left parenthesis
| vertical bar ) right parenthesis
/ slash [ left square bracket
\ backslash ] right square bracket
~ tilde { left brace
_ under score } right brace
$ doller sign % percent sign
# number sign
What is a variable?
A variable is a storage unit, which sets a space in memory to
hold a value and can take different values at different times
during program execution.
Rules to construct variable names:
A variable name may consists of letters, digits and the underscore ( _ )
characters.
A variable name must begin with a letter. Some systems allow to start
the variable name with underscore as first character.
Real constants
0.0045 -.71 +45.203 0.45e3
.478. -4.69E-4
Data types
Four fundamental data types
Type Range of values Description
Char (Characters) -128 to 127 A single byte (8 bits) can
store one character type
data
Int (Integers, whole -32768 to 32767 To represent a whole
numbers) number in the specified
range
Float (Floating point, real 3.4e-38 to 3.4e+38 Single precision floating
numbers) point
Double (Double) 1.7e-308 to 1.7e+308 Double-precision floating
point
Data types ...
Char
8 bits
Qualifier
Signed: -128 to +127
Unsigned: 0 to 255
Int
short int, int, long int in both signed and unsigned
Signed uses 1 bit for sign and 15 bits for magnitude of a 16-bit number
Float
32 bits with 6 digit precision
For more accuracy use double
Double
64 bit number with 14 digits precision
For more precision use long double with 80 bits
Data types: size and range
Type Size(bits) Range
char or signed char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to 32767
unsigned int 16 0 to 65535
short int 8 -128 to 127
signed int 8 -128 to 127
unsigned short int 8 0 to 255
long int 32 -2147483648 to 2147483647
signed long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
float 32 3.4E-38 TO 3.4E+38
double 64 1.7E-308 TO 1.7E+308
long double 80 3.4E-4932 TO 1.1E+4932