Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
53 views

EEE3801 Computer Programming For Engineering - Chapter - 01

This document provides an introduction to the C programming language. It discusses C as a high-level language and its history, which began as the B programming language developed by Ken Thompson for Unix in the 1970s. C is commonly used for embedded systems due to its portability and ability to produce efficient code. The document outlines the software development process and C compilation model. It also describes basic C syntax elements like variables, functions, comments, and preprocessor directives.

Uploaded by

Ted Chan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

EEE3801 Computer Programming For Engineering - Chapter - 01

This document provides an introduction to the C programming language. It discusses C as a high-level language and its history, which began as the B programming language developed by Ken Thompson for Unix in the 1970s. C is commonly used for embedded systems due to its portability and ability to produce efficient code. The document outlines the software development process and C compilation model. It also describes basic C syntax elements like variables, functions, comments, and preprocessor directives.

Uploaded by

Ted Chan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 01 – Introduction to C

Programming
Textbook / Reference
A Structured Programming
Approach Using C
written by
Behrouz A. Forouzan,
published by Course Technology;
3rd edition

Department of Engineering 2
High-Level Language
 Use more English words. They try to resemble
English sentences. Therefore, it is easier to
program in these languages
 The programming structure is problem oriented
- does not need to know how the computer
actually executes the instructions
 Processor independent - the same code can be
run on different processors
 Examples: Basic, Fortran, Pascal, Cobol, C,
C++, Java
 A high level language needs to be analyzed by
the compiler ( 編譯器 ) and then compiled into
machine code so that it can be executed by the
processor
Department of Engineering 3
History of C
 UNIX was originally written in assembly code, which was
difficult to debug and develop, and horribly painful to
enhance
 A higher-level language with greater abstraction was needed
 Ken Thompson (original author of UNIX) thus developed a
small language named B, based on BCPL (Basic Combined
Programming Language), as systems programming language
from the 1960s
 Dennis Ritchie soon joined the
development of the UNIX project and
began developing programs in B
 In 1970, Bell Labs acquired a PDP-11
and Thompson rewrote a portion of
UNIX in B
Department of Engineering 4
History of C (Cont.)
 By 1971 it was clear that B was not well suited to the PDP-
11, so Ritchie began to develop a modified version of B
which he named NB ("new B")
 NB soon began to diverge greatly from B, so Ritchie renamed
it C
 By 1973 the language was stable enough that UNIX was
completely rewritten in C
 In 1978, the first milestone towards
standardization of C was achieved when The
C Programming Language, by Brian
Kernighan and Dennis Ritchie was published
 Known as the "K&R Book" or "The White
Book"
 Absent a formal standardization, this
served as the defacto standard
 Original K&R version of C often called
"K&R C" or "Classic Department
C" of Engineering 5
History of C (Cont.)
 Standardization
 Many slight variations of C existed, and were incompatible
 Committee formed to create a "unambiguous, machine
independent“ definition
 American National Standards Institute (ANSI) approved
formal standardization in 1989. Known as "ANSI C" or
just "Standard C"
 Standard C is completely backwards compatible with
Classic C
 As part of the normal evolution process the standard was
updated in 1995 (C95) and 1999 (C99)

Department of Engineering 6
C and Embedded Systems
 A P-based system used in a device (e.g. a car
engine) performing control and monitoring
functions is referred to as an embedded system
 The embedded system is invisible to the user
 The user only indirectly interacts with the embedded
system by using the device that contains the P
 Most programs for embedded systems are written
in C
 Portable – code can be retargeted to different processors
 Clarity – C is easier to understand than assembly
 Compilers produce code that is close to manually-
tweaked assembly language in both code size and
performance

Department of Engineering 7
Software Development Method
 Requirement Specification
 Problem Definition
 Analysis
 Refine, Generalize, Decompose the problem definition
 Design
 Develop Algorithm
 Implementation
 Write Code
 Verification and Testing
 Test and Debug the code

Department of Engineering 8
The C compilation model
 The Preprocessor accepts source
code as input and
 Removes comments
 Extends the code according to the
preprocessor directives included in the
source code (lines starting with #)
 The Compiler takes the output of
the preprocessor and produces
assembly code
 The Assembler takes the assembly
code and produces machine code
(or object code)
 The Linker takes the object code,
joins it with other pieces of object
code and libraries and produces
code that can be executed
Department of Engineering 9
Structure of a C program
#include <stdio.h>
 A C program contains the following #define TIMES 10 /* upper bound */
elements:
double myfunction(float);
 Preprocessor Commands
/* Function prototype –
 Type definitions
Declaration */
 Function prototypes  int main() {
declarations of function types double wert;
and arguments double pi = 3.14;
 Variables printf(“Multiply by 10\n”);
 Functions wert = myfunction(pi);
 All programs must contain a single printf("%d * %f = %f\n", TIMES, pi,
main() function. All function, wert);
including main, have the following return 0;
format: }
type function_name (parameters){
double myfunction(double var1){
local variables
int i;
C Statements double count = 0;
} count = TIMES * var1;
Department return count;
of Engineering 10

}
A Simple C Program

Department of Engineering 11
Preprocessor Directives
#include <stdio.h>
 As part of compilation, the C compiler runs a program
called the C preprocessor which copy contents of
header file (stdio.h) into source code
 Header files typically contain descriptions of functions
and variables needed by the program
 In this case, the directive #include tells the
preprocessor to include code from the file stdio.h
 This file contains declarations for functions that
the program needs to use. A declaration for the
printf function is in this file

Department of Engineering 12
Preprocessor Directives (Cont.)
#define STOP 0
 Before compiling, replace all instances of the
string "STOP" with the string "0"
 Called a macro
 Used for values that won't change during
execution, but might change if the program is
reused (Must recompile)

Department of Engineering 13
Comments
 Begins with /* and ends with */
 Can span multiple lines
 Cannot have a comment within a comment
 Comments are not recognized within a string
 Example: "my/*don't print this*/string" would be
printed as: my/*don't print this*/string
 Use comments to help reader, not to confuse or
to restate the obvious
 Some C compiler allow C++ line commenting
"//"

Department of Engineering 14
main Function
 A C program can contain many functions but
must always have one main function
 This is the code that is executed when the
program is start running
 The code for the function lives within brackets:
int main()
{
/* code goes here */
}
 The "int" specifies the return type of main( )
function
 Usually, if a program runs successfully it will
return zero to the operating system or -1 if an
error occurs Department of Engineering 15
The curly braces { }
 Identify a segment / body of a program
 The start and end of a function
 The start and end of the selection or repetition
block
 Since the opening brace indicates the
start of a segment with the closing brace
indicating the end of a segment, there
must be just as many opening braces
as closing braces (this is a common
mistake of beginners)

Department of Engineering 16
Variable Concepts
 A most fundamental way that a C program stores
an item of data in memory is with a variable
 A variable is a named storage location in the
computer’s memory
 The data stored in a variable may change while
the program is running (hence the name variable)
 Each variable has a type, which tells the compiler
how the data is to be interpreted (and how much
space it needs, etc.)
int counter;
int startPoint;
 int is a predefined integer type in C
Department of Engineering 17
Declaring Variables
 Before using a variable, you must give the
compiler some information about the
variable; i.e. you must declare it
otherwise the variables contain a random
value
 The declaration statement introduces
the data type of the variable
 Example of variable declarations:
int meatballs;
float area;
Department of Engineering 18
Naming Variables
 Variable names (The name of a variable is
called an identifier) in C
 May only consist of letters, digits, and
underscores ( _ )
 May be as long as you like, but only the first
31 characters are significant
 Cannot begin with a digit, i.e. 1st character
must be a letter
 May not be a C reserved word (keyword)
 Legal: times10 get_next_char _done
 Illegal: 10times get-next-char -done

Department of Engineering 19
Keywords
 Keywords that have special meaning in C and
hence are reserved are as follows:

 These keywords are reserved (restricted) and


cannot be used as identifiers. Identifiers in the
standard library also cannot
Department be used
of Engineering 20
Naming Conventions
 C programmers generally agree on the
following conventions for naming variables
 Begin variable names with lowercase letters
 Use Meaningful identifiers
 Separate "word" within identifiers with
underscores or mixed upper and lower case
 Examples: surfaceArea surface_Area
surface_area
 Be consistent!

Department of Engineering 21
Naming Conventions (Cont.)
 Use all uppercase for symbolic constants
(used in #define preprocessor directives)
 Note: symbolic constants are not
variables, but make the program easier to
read
 Examples:

#define PI 3.14159
#define AGE 52

Department of Engineering 22
Case Sensitivity
 C is case sensitive
 It matters whether an identifier, such as a
variable name, is uppercase or lowercase
 Example:
area
Area
AREA
ArEa
are all seen as different variables by the
compiler

Department of Engineering 23
Data Type
 C has the following basic data types

 The sizes of the data types are not standardized (depend


on the implementation)
 The type void is used to indicate functions that return no
value or null pointers

Department of Engineering 24

You might also like