Você está na página 1de 29

Elements of C

Programming
C Character Set
• The basic building blocks in C programs are
characters:
– Numeric
– English alphabet (lower and uppercase)
– Space
– All other printable special characters on the standard
English language keyboard
• Characters can be combined properly to some
syntactical rules to form tokens

CS 11 Introduction to Computer 2
Science
C - Tokens
• Are of six kinds:
– Reserved words
– Identifiers
– Constants
– String literals
– Punctuators
– Operators
• C language is case sensitive

CS 11 Introduction to Computer 3
Science
Reserved Words
• Are keywords with special meanings to the
compiler
• They must appear in the correct location in the
program, be typed correctly and be used in the
right context
• Should be in lowercase
• Examples:
– program control (if, else, switch, break, while, do, for,
continue, return, goto, etc.)
– data types (double, float, int, short, long, char, etc.)
CS 11 Introduction to Computer 4
Science
Identifiers
• Represent and reference program entities – for
variables, functions and other program contructs
• Also called programmer-defined words
• Guidelines:
– Consist of capital letters A to Z, small letters a to z,
digits 0 to 9, and the underscore (e.g. _exam)
– First character must be either a letter or a underscore
– No embedded blanks (e.g. student_number)
– Not a reserved word
– Are case sensitive
© Richard Bryann Chua CS 11 Introduction to Computer 5
Science
Identifiers (cont’d)
• Why are the following illegal identifiers?
– student age
– switch
– 10thcell
– Exam1+Exam2
– not#me
– 101_south
– -plus

CS 11 Introduction to Computer 6
Science
Identifiers
• Good programming style requires the
programmer to choose names that are
meaningful

• tax = price * tax_rate;

CS 11 Introduction to Computer 7
Science
Constants
• Entities that appear in the program code as fixed
values
– integer constants (includes octal and hexadecimal
integers in addition to decimal ones)
– floating-point constants
– character constants
– escape sequences (or the back slash)
• Example: const int age = 17;

CS 11 Introduction to Computer 8
Science
String Literals
• Sequence of any number of characters enclosed
by double quotation marks
• Example: “Hello World!”

• Note escape sequences, like for double quotes

CS 11 Introduction to Computer 9
Science
Punctuators
• Used to delimit various syntactical units
• Referred to as separators
• Includes
– []
– {}
– ()
– , ; :
– * #

CS 11 Introduction to Computer 10
Science
Operators
• Result to computations / actions when applied to
variables or other elements in an expression
• Example:
– classStanding = (LectureGrade * 0.50) + (LabGrade *
0.50)
– classStanding variable includes an assignment
operator =, the multiplication operator * and the
addition operator +

CS 11 Introduction to Computer 11
Science
C Program Components
• Program comments
• Preprocessor directives
• Type declarations
• Named constants
• Statements
• Function declarations (prototypes)
• Function definitions
• Function calls

CS 11 Introduction to Computer 12
Science
Program Comments
• Are explanations or annotations that are
included in a program for documentation and
clarification purposes
• Ignored by the compiler and have no effect in
program execution
• Enclosed in /* */

CS 11 Introduction to Computer 13
Science
Program Comments
• Comments should be written simultaneously with
program text, not as a last step:
– There is a tendency to omit or abbreviate comments
– Comments should serve as running commentary,
indicating program structure and contributing to
program clarity and correctness

CS 11 Introduction to Computer 14
Science
Program Comments
• Comments should occur at the top of the
program and at the head of major structural
groupings within the source code
• Short comments should occur to the right of
individual statements when the effect of the
statement is not obvious

CS 11 Introduction to Computer 15
Science
Program Comments
• Although in practice overcommenting of code
almost never occurs, comments should
nevertheless not clutter the program
• They should illuminate and give insight into what
the program is doing
– tax = price * rate; /* sales tax formula */
– tax = price * rate; /* multiply price by rate
*/

CS 11 Introduction to Computer 16
Science
Preprocessor Directive
• An instruction to the preprocessor
• One purpose is file inclusion, which adds the
content of a header file to a source program file
• Are of two types:
– Standard – supplied by the vendor of the C-compiler
• #include <stdio.h>
– Programmer-defined – written by the programmer and
stored in some directory
• #include “C:\mypath\myheader.c”

CS 11 Introduction to Computer 17
Science
Preprocessor Directive
• #define LIMIT 100
• If placed at the top of the file, replaces all
occurrences of the identifier LIMIT to 100
• If a constant has been defined symbolically by
the #define facility and then used throughout the
program, it can easily be changed later, if
necessary
• Normally placed at the beginning of the file, just
after the #include directives

CS 11 Introduction to Computer 18
Science
Data Types
• Are sets of data values and the set of operations
on those values
• Basic Types
Type Bit Width Range
– char
char 8 0 to 255
– int int 16 -32768 to 32767
– float float 32 3.4E-38 to 3.4E+38
– double double 64 1.7E-308 to 1.7E+308
void 0 valueless
– void

CS 11 Introduction to Computer 19
Science
Type Modifiers
• Alter the meaning of the data type to fit the
needs of various situations more precisely
• Examples:
– short
– long
– unsigned (if values are never negative, as these can
handle a larger number than its equivalent signed
type)
– signed (usually not used)

CS 11 Introduction to Computer 20
Science
Type Modifiers
• The compiler may provide less storage for a
short than for an int, although it is not required
to do so (e.g. if there is a concern on storage)
• Similarly, the compiler may provide more
storage for a long than for an int, although it is
not required to do so

CS 11 Introduction to Computer 21
Science
Integer Types
• The datatype int is the principal working type of
the C language
• An integer is a whole number and is never
written in C with a decimal point
• Are stored straightforwardly as binary numbers

CS 11 Introduction to Computer 22
Science
Floating-Point Types
• More or less correspond to “real numbers”
• Floating-point representation involves breaking
up a number into a fractional part and an
exponent part and storing the parts separately
• For example 7.0 is 0.7E1
• Floating-point numbers can represent both
whole and fractional numbers, and a much
larger range of values than integers
• Operations normally are slower than for integers

CS 11 Introduction to Computer 23
Science
Declaring Variables
• int x;
• float y = 10;
• char z = 'p';
• int x, y;

CS 11 Introduction to Computer 24
Science
Using Data Types
• Declare the variables that you need at the
beginning of the function which uses them
• Choose a name for the variable that suggests its
meaning
• When initializing a variable, match the constant
type to the variable type
– int apples = 3;
– int oranges = 3.0;

CS 11 Introduction to Computer 25
Science
Other Types
• C does have other types derived from basic
types
• These types include arrays, pointers and
structures (later topics)

CS 11 Introduction to Computer 26
Science
Named Constants
• Identifier whose value is fixed and does not
change during the execution of the program
• Keyword const

CS 11 Introduction to Computer 27
Science
Statements
• Action to be taken as program executes
• Examples:
– Assignment statements
– Selection statements
– Loop statements
– Compound statements

CS 11 Introduction to Computer 28
Science
Functions
• Modular programming
• Lab Topic on Subprograms

CS 11 Introduction to Computer 29
Science

Você também pode gostar