Title: SOFTWARE DEVELOPMENT IN C CM9041
1SOFTWARE DEVELOPMENT IN CCM904-1
- Lecture 1
- Introduction to C
2Module Structure
- 1 Lecture, 1 Tutorial , 1 Practical per week
- Module Handbook contains details
- teaching team
- class contact,
- reading list
- Assessment
- Web site-www.soc.staffs.ac.uk/rgh1
- lecture notes and practical exercises
- Module handbook to download
- Assignment details
3Origins of C
- 1971 Ritchie and Thompson develop Unix written in
assembly language - This Unix wasnt portable, a suitable HLL with
low level facilities was required - CPL had been developed in the 60s
- CPL was a large language, later pared down -gt
BCPL - Thompson further reduced BCPL -gt B
- Ritchie enhanced B -gt C. Unix was rewritten in C
- Unix C became widely available
- Unix is now portable any machine with a C
compiler can run Unix
4C Standards and Variants
- 1978 Kernighan Ritchie publish The C
Programming Language (Traditional C) - 1990 ANSI Standard (ANSI C)
- Anything written in traditional C will run on an
ANSI C compiler - Also available Borland C, Microsoft C
5Characteristics of C
- Suitable for systems programming
- Suitable for applications programming
- Cryptic syntax programs can be written quickly
but can be difficult to read - Permissive data typing gives flexibility but
reduces the number of errors detected at
compilation time
6Developing a C Program
- Fully understand the problem
- Design a solution
- Test
- Implement in a suitable programming language
- Test
- Release
- Maintain
7Overall Structure of a C program
- include ltstdio.hgt
- DEFINITION OF ANY CONSTANTS
- int main(void) / some explanatory comment /
-
- DECLARATIONS
- STATEMENTS
- return 0
-
8The Pre-Processor
C
Pure C
C Compiler
Object Code
Pre-processor
Object code is executed when the program runs
9Data Types
- Basic Data Types
- int i,j 4 bytes
- float x,y 4 bytes
- double p,q 8 bytes
- char c 1 byte (ASCII)
10- Sizeof Operator
- int i declare an integer variable called i
- sizeof(int) - Gives 4
- sizeof(i) - Gives 4
- Additional Data Types
- long int 8 bytes (263 1) greater range
- short int 2 bytes less storage
- unsigned int 4 bytes greater range, positive
numbers
11More about the Structure of a C Program
- The Function main
- In C, all code is packaged in functions
- main is the function that the system calls when
you run your program - Every program must have one and only one main
- All functions may return a value
Function 1
Function 2
Main statement Call function1 statement statement
Call function 2 return
12Function Components
- A function definition consists of two
- parts
- A Specification ( or header or interface)
Specifies the function name, the return value and
argument list - A Body ( or block or implementation) Code to be
executed when the function is called - A sequence of statements enclosed in braces
13 Expressions and Statements
- A section of code such as n1n2 is called an
expression - A statement is a step in a program
- An expression becomes a statement when it
- is terminated by a semicolon
14Comments
- / An example of a C comment /
- A multiline comment can also be used
- /
- This is a multiline
- comment. Indentation is not compulsory
- but it makes the code more readable
- /
15Consideration of two simple examples of a C
program
- include ltstdio.hgt
- define PI 3.14
- int main(void) / calculates the area and the
circumference of a circle of radius 3 / -
- int radius
- float circum, area
- radius3
- circum2PIradius
- areaPIradiusradius
- printf(circumference f\n, circum)
- printf(area f\n, area)
- return 0
-
16Output (printf statements)
- The printf statement consists of any text to be
printed plus a format specifier for each argument -
- Format Specifiers d, 6d, f, 6.2f, ld, lf,
c, s -
- Special Character Constants for use in control
strings - \n newline
- \t tab
- \\ backslash
- \ single quote
17Input (scanf statements)
- Input
- scanf() function is used
- Format scanf(control string, arguments)
- The arguments must be addresses
- e.g.
- int n
- scanf(d, n)
- The is used to obtain the address of n
18include ltstdio.hgt define PI 3.14 int
main(void) / reads the radius of a circle and
then calculates its area and
circumference/ int radius float
circum,area printf(input radius
) scanf(d, radius) circum2PIradius area
PIradiusradius printf(\n\nresults\n\n) pri
ntf(Circle of radius d,radius) printf(\nCircu
mference f and area f\n, circum,area) return
0