程序代写代做代考 compiler assembler assembly PowerPoint Presentation

PowerPoint Presentation

CSE 2421
The C Language – Part 1.1
Required Reading:
Computer Systems: A Programmer’s Perspective, 3rd Edition,
Chapter 1 thru Section 1.3
Pointers on C,
Chapter 5 thru Section 5.1.3, 5.3 through the end of the chapter

1

You must make 2 different submissions to complete lab 1:
lab1Readme in either Word or PDF format using the Lab 1 ReadMe File assignment
Lab1.c, lab1.input1, and lab1.input2 files in a .zip file using the Lab 1 Program Files assignment
I’m seeing some submissions with only one or the other.
Lab 1

% gcc –o hello hello.c
Source code

Assembly Code
Libraries
(e.g. printf() code) Object Code

Executable Code

% hello [executes the program]

The compilation (or build) system
Preprocessor

Compiler

Assembler
Link Editor
hello.c
hello

Type in program source code (file.c) using
an editor of your choice; plain text
.c + .h = .i which is the ultimate source
code – i.e., #includes expanded and
#defines replaced, comments removed
.i → .s which is assembler source code
.s → .o which is an object file; fragments of
machine code with unresolved symbols, i.e.,
some addresses not yet known (vars/subrs).
.o + library links a.out (default name);
resolves symbols, generates an executable

3

Basic Data Types
Constants
Variables
Identifiers
Keywords
Operator Precedence and Associativity
Basic I/O (covered in a separate set of slides)
Control structures (if, while, for, etc.) (covered in a separate set of slides)

Can you associate one of the 4 programming language categories to each of these???

C Language Overview

Integer Types
char – smallest addressable unit, *always* 8 bits (1 byte); each byte has its own address
short – not used as much; typically 16 bits (2 bytes)
int – default type for an integer constant value; typically 32 bits (4 bytes)
long – do you really need it?; typically 64 bits (8 bytes)
long long – at least 64 bits, sometimes 128 bits, (only supported in C99 and after)

Floating Point Types – (these are usually “inexact”, we’ll see why later)
float – single precision (about 6 decimal digits of precision), (4 bytes)
double – double precision (about 15 decimal digits of precision) (8 bytes)
long double – about 30 decimal digits of precision (only C99 and after) (16 bytes)
double is constant default unless suffixed with ‘f’

Note that variables of type char are guaranteed to always be one byte.

There is no fixed or maximum size for a type in C (except for char; otherwise, size depends on implementation), but the following relationships must hold:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)<=sizeof(long long) sizeof(float) <= sizeof(double) <= sizeof(long double) True size of data types is typically dependent upon the size of the processor being used. Basic Data Types Beside the basic types, there is a conceptually infinite class of derived types constructed from the fundamental types in the following ways: arrays of objects (variables or derived types) of a given type; pointers to objects of a given type; structures containing a sequence of objects (variables or derived types) of various types; unions capable of containing any of one of several objects of various types. In general these methods of constructing objects (variables or derived types) can be applied recursively An array of pointers to some type An array of characters (i.e. a string) Structures that contain pointers And so on. Derived Types Special characters Not convenient to type on a keyboard Use single quotes (e.g. ‘\n’) Looks like two characters but is really only one Constants \a alert (bell) character \\ backslash \b backspace \? question mark \f formfeed \’ single quote \n newline \" double quote \r carriage return \ooo octal number \t horizontal tab \xhh hexadecimal number \v vertical tab   2 ways to do it Put the const keyword after the type keyword, or before the type keyword Note: The compiler treats these as variables to which any assignment is invalid. This means the declared const must be initialized with its (constant) value as part of the declaration, because the compiler will not allow statements which make assignments to it later! Treated as a read-only variable. For program readability, pick one of the two ways and use it exclusively. Be consistent! Examples: float const PI = 3.141593f; const float PI = 3.141593f; Uppercase used for declared constants, and also for those with #define (see below) by convention. Symbolic constants (with the #define directive - below) can be used anywhere a literal constant can be used, but constants defined with the const keyword can only be used where variables can be used. More on this later (with examples). We will say more about constants as function parameters, pointers to constants, and constant pointers later. Page 8 Declaration of Constants 2 way, but BE CONSISTENT!!! A name that substitutes for a value that cannot be changed Can be used to define a: Constant Statement Mathematical expression Uses a preprocessor directive: #define
is a text string with no white space;
is any text string (so it can be a mathematical expression, for example 3.1415927 * r * r)
REMINDER: No semi-colon is used for preprocessor directives.
Coding convention is to use all capital letters for the name:
#define AREA (3.141593 * r * r)
#define AREA (PI*r*r)
What might happen if parentheses aren’t included in these define statements?
What if these statements used addition/subtraction rather than multiplication?
Can be used any place you would use the actual value
All occurrences are replaced by the preprocessor before the program is compiled by the compiler.
Examples:
The use of EXIT_SUCCESS in hello.c code
#define PI 3.141593
#define TRUE 1
Symbolic Constants

Purpose: define a variable (can also be a constant) before it is used.
Format: type identifier (, identifier) ; Note: the parentheses here indicate any number of identifiers, each preceded by a comma
Initial value: can be assigned, but is not required (unless it is a constant)
int i, j = 5, k;
char code, category;
int i = 123;
const float PI = 3.1415926535f;
double const PI = 3.1415926535;
Type conversion: aka, type casting
Directing the compiler to use a variable as a different type than the one used in the declaration.
Casting “larger” types to “smaller” types is dangerous (truncation occurs), and should be done with extreme caution!!!
To cast a variable to a different type explicitly, use: (type) identifier
int i = 65;
char ch; /* range -128 to 127 */
ch = (char) i; /* What is the value of ch? */
What happens if we change the initial value of i to 165?
Variable Declarations

Identifier Naming Rules: names for variables, constants, types and functions.
Can use a-z, A-Z, 0-9, and _ (i.e., alphanumeric, digits, and underscore)
No other characters can be used
Case sensitive
The first character must be a letter or _ (Don’t use _ , though, because it is used for system purposes).
Keywords are reserved words, and may not be used as identifiers
(See the following slide for C keywords)
No guarantee that any value past the 31st character will be recognized. (i.e. will let you use more characters, but no guarantee that it will parse it.)
Identifier Naming Style (the grader will enforce these)
Separate words with ‘_’ (this is the original style in C) OR capitalize the first character of each word after the first (e.g., char_count or charCount)
Use all UPPERCASE for symbolic constants or macro (code chunk)
definitions.
Be consistent. Be consistent. Be consistent.
Be meaningful: Write “self-documenting code”; i.e., identifiers should give a clear idea of what a variable, constant, type or function is being used for.
Sample Identifiers
i0, j1, student_name, studentName, student_score, studentScore…
Identifier Naming Style

Purpose: reserves a word or identifier to have a particular meaning
The meanings of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language.
You shouldn’t use them for any other purpose in a C program. They are allowed, of course, within double quotation marks (as part of a string to be assigned or printed, for example; this is not using an identifier, actually).
Page 12
Keywords – reserved identifiers

/docProps/thumbnail.jpeg