Module01 ClassNotes
Module01 ClassNotes
1-4
A Brief History of C
• Designed by Dennis Ritchie of Bell Laboratories.
• Implemented on PDP-11 in 1972, as a systems programming language
for the UNIX operating system.
• Evolved from B and BCPL
– BCPL = type-less PL developed by Martin Richards (Camb., ’67).
– B was based on BCPL and was written by Ken Thompson in 1970 for the first
UNIX system on a PDP-7.
• C was formed with low-level programming + structured programming
issues in mind.
• C revised by ANSI (X3J11) to form new standard (1989).
• Internationalised and became ANSI/ISO standard in 1990.
• “C99” standard revision finalised in 1999.
1-5
Characteristics of C
– AI/Database/Graphics applications.
– Commercial (Business) applications.
– Embedded/Real-time applications.
– Engineering/Scientific applications.
1-7
Getting Started
• We will use these next few C program examples to begin our
study of C, and to highlight the following:
– Function main()
– Simple #define and #include
– Variable declarations
– Functions printf() and scanf()
– Function return from main -- exit()
1-8
#include <stdio.h>
Pre-processor directives
#include <stdlib.h>
(cont’d …) 1-10
while ( scanf("%d%d%d", &ass1, &ass2, &exam) == 3 )
{
studentCount++;
(cont’d …)
1-11
printf(" %d %3d %3d %3d %6.2f %c\n",
studentCount, ass1, ass2, exam, total, grade);
ass1Sum += ass1;
ass2Sum += ass2;
examSum += exam;
totalSum += total;
}/* end of while loop */
if ( studentCount == 0 )
printf("\n Empty input file\n");
else
{
printf(" ================ ======= ======\n");
printf("Averages: %6.2f %6.2f %6.2f %6.2f",
ass1Sum / (float) studentCount,
ass2Sum / (float) studentCount,
examSum / (float) studentCount,
totalSum / studentCount );
printf(" Total no. of students = %d\n", studentCount);
}
return EXIT_SUCCESS;
1-12
}
Data Types, Pointers, and Pitfalls
• Variables, types and values
• Type limits and sizeof()
• Type qualifiers
• Expressions and operators
• The (missing) boolean data type
• The ++ and -- operators
• Beware of Side effects
• Order of evaluation
• The pointer data type
1-13
Variables, Types and Values
• a variable is a named memory location where a value can be
stored
• each variable has a type which defines the range of legal values
for that variable
1-14
Variables, Types and Values
1-15
Type limits and sizeof()
1-16
Type limits and sizeof()
1-17
#include <stdio.h>
#include <stdlib.h> Program: to display size of the data types
int main(void)
{
int x;
char c;
float f;
double d;
return EXIT_SUCCESS;
}
1-18
Type qualifiers
C has the following data type qualifiers:
{unsigned} {long | short} int
{signed | unsigned} char
{long} double
1-19
Expressions and operators
• each type has a set of operators for the legal operations that can
be performed in expressions with those types - e.g.
integer arithmetic: + - * / % ++ --
real arithmetic: + - * /
relational: == > < ! || && !=
assignment: =
bit manipulation: ~ ^ | & << >>
float x;
int y = 5, z = 10;
x = y / z; /* problem! */
x = (float) y / (float) z; /* solution */
NB: strictly speaking, only one cast is required, which will “promote” the
expression being evaluated to that type
1-22
The (missing) boolean data type
• C (sadly) has no type “boolean”
yields: x == 10 x == 11
( the same applies for the pre- and post- decrement operator -- )
1-24
Beware of Side-Effects
• Unexpected and unwanted side-effects can occur when you have
both:
– multiple assignments in the one statement and
– the same variable used more than once
x = (y = 6) - (s = 3); /* OK */
x = (y *= y) - (s -= y); /* yikes! */
x = y++ + ++y; /* legal but yikes2 !! */
1-25
Order of evaluation
• C does not specify the order of evaluation of sub-expressions
within a statement - e.g. given:
x = (y *= y) - (s -= y);
1-26
The pointer data type
• there is one other basic data type in C that - we will see - is very
important and characteristic of C
• a pointer variable is a variable that can store a memory address
• a memory address is not just a simple integer
• there are two components to a pointer declaration
– that the variable is a pointer (indicated by a *)
– the type of what it can point to - i.e. the type for the data at
the address the pointer points to
1-27
The pointer data type
• the legal operators with pointers in C are:
– assigning a memory address to a pointer variable
– dereferencing the pointer - i.e. accessing the value that is
stored at the address that the pointer points to
– pointer arithmetic - addition and subtraction only
– comparison operators - e.g. == != > < etc.
• no other operations are legal - nor make sense!
• the pre-defined constant value of NULL can be assigned to a
pointer to indicate that the pointer doesn’t currently contain a
legal address value
1-28
The pointer data type
int x, y;
int *ptr; pointer declaration ptr
x = 2; 230
(238)
ptr = &x; assigning an address
y = *ptr + 3;
y
dereferencing 5
(234)
x 2
NB: this example assumes that an integer is (230)
32 bits (4 bytes) and we have 32 bit (4 byte)
addresses. 1-29