Tutorial on C LanguageProgrammingTeodor Rus
rus@cs.uiowa.edu
The University of Iowa, Department of Computer Science
Introduction to System Software – p.1/64
Tutorial on C programmingC program structure:
Data structure
Control structure
Program structure
Introduction to System Software – p.2/64
Data structuresPrede?ned data types:
integer (int), small integers (short), large integers
(long)
real numbers (?oat), large real numbers (double)
character data (char)
User de?ned data types using type constructors
array,
record, pointer, ?leIntroduction to System Software – p.3/64
DeclarationsA data object of a de?ned type T is declared using the
construct of the form
T data where T is a type expression
and data is the data object name
Example:int x declares x an object of type integer
short x declares x an object of type small integer
long x declares x an object of type large integer
?oat x declares x an object of type real
double x declares x an object of type large real
char x declares x an object of type character
Introduction to System Software – p.4/64
De?nitionsAn object of a user de?ned type T is constructed using
one of the type constructors
struct, [], *, FILE that takes
as arguments objects of already de?ned types.
A new user de?ned type T is constructed using the
meta-constructor
typedef and a type or a type
constructor
Introduction to System Software – p.5/64
Record type de?nitionA record type is de?ned using the
struct constructor
following the template:
struct TypeName
{
component1;
component2;
component3;
}
Components are object declarations of the form
T
ObjName;Note: TypeName is an abstraction
Introduction to System Software – p.6/64
Record object declarationAn object of type TypeName is obtained by the
declaration
TypeName MyRecord
One can put together the de?nition and the declaration
getting:
struct TypeName
{
component1;
component2;
component3;
} MyRecord;
Introduction to System Software – p.7/64
Example recordExample of a record type de?nition and declaration is:
struct Data
{
int Day;
int Month;
int Year;
} MyData, *MyPT, MyArray[Max];
Note: type expressions are obtained by combining the
type constructors struct, *, [], in a well de?ned manner
Introduction to System Software – p.8/64
Reference to record componentsMyData.Year, MyData.Month, MyData.Day are
references at the components of the data object
MyData
M yP T ? > Y ear, M yP T ? > M onth, M yP T ? > Day are
pointer reference to the same components.
Note, we need to use MyPT = &MyData before this
reference make sense; i.e.,
M yP T ? > Y ear ? (?M yP T ).Y ear.
Introduction to System Software – p.9/64
Memory representation of recordsConsider the following de?nition and declarations:
struct example
{
int x;
int *y;
} Obj, *PtObj;
Introduction to System Software – p.10/64
Document Outline
- Tutorial on C programming
- Data structures
- Declarations
- Definitions
- Record type definition
- Record object declaration
- Example record
- Reference to record components
- Memory representation of records
- Memory representation
- Memory representation of PtObj
- Facts about records
- Array data type
- Array memory representation
- Union data type
- Reference to union components
- Example of a union usage
- Reference to union components
- Pointer data type
- Example pointers
- Pointer references
- File data type
- Operations with file
- User defined types
- Examples
- Control Flow Structures
- C language computation units
- Assignment statement
- Branching statements
- If-statement
- If-else statement
- Switch statement
- Break statement
- Continue statement
- Unconditional jump statement
- Looping statements
- While statement
- Do-while statement
- For statement
- Block statement
- Function definition
- Example
- Function declaration
- Function call
- Parameter passing
- Remember
- Function memory representation
- Activation record
- Structure of a function in memory
- Structure of a C language program
- Note
- Macro definition component
- Global declarations
- Main function of the program
- Program execution
- Other function components
- Example program
- Bonus point assignment
- Program memory representation
- Data segment
- Text segment
- Stack segment
- Development of a C program
Add New Comment
Showing 1 comment