Title: Chapter 13 Introduction to C Language
1Chapter 13 Introduction to C Language
- By C. Shing
- ITEC Dept
- Radford University
2Objectives
- Understand C features
- Understand Program Structure in C
3C Features
- Type language (stronger type than C)
- Variable must be declared a type
- (declaration mixed with statements)
- has boolean data type bool (false, true)
- Class hide member data (only member function can
access them), - provide public interface by member function
- Overloading (operator and function)
- same function/operator name with different
- argument list
- Inheritance and Polymorphism virtual function
- Template
- Same function task for different data types
4Running C Program
- C source program -gt C Compiler
- -gt Object program -gt Link
- -gt(feed data) Excution
5Program Structures
- File Extension
- .cc (or .cpp)
- Compile link (Solaris) , then execute
- g mainfile.cc sub1filename.cc
sub2filename.cc - then
- a.out
- or
- g -o executable mainfile.cc
- sub1filename.cc sub2filename.cc
- then
- executable
6Program Structures (Cont.)
- Compile only (Solaris)
- g c filename.cc
7Program Structures (Cont.)
- mainfile.cc
- /
-
- This is a comment
- Block
-
- /
- // Comment line
- // Declaration for standard library
- // The following line is required
- include ltiostreamgt
8Program Structures (Cont.)
- // all C library declaration starts with c
- include ltcmathgt //if math library is used
- // The following file yourdeclaration.h
- // is in your current directory
- //include yourdeclaration.h
- using namespace std // for cin and cout
- // declare named global constants
- const int number 4
- const double PI 3.14159
- const char MY_MESSAGE Good Morning!
9Program Structures (Cont.)
- // declare types
- typedef char myCharacter
- // declare global variables
- int a
- char ba, c\n
- myCharacter e
- long d 14000L // this means long int (default
int) - float x-2.5F
- double y2.5
10Program Structures (Cont.)
- // declare function prototype
- int sub1(int)
- // define main function
- int main ()
-
- int lc 2
- // call function
- int lb sub1(lc)
- cout ltlt lb ltltendl
- return 0 // no error exit
-
11Program Structures (Cont.)
- // define function
- int sub1 (int l)
-
- // call function
- int la 5l
- return la
-
12Variable
- Variable declaration
- Form type variable initial value
- e.g. int number
- Pointer variable declaration
- Form type variable initial value
- e.g. int numberptr0
- // numberptr is initialized to null pointer
13Constant Variable
- Constant variable declaration
- Form const type variable
- e.g. const double PI 3.14159
- Constant address (or pointer variable)
- declaration
- Form pointer type const variable
- e.g. double const PIptr
14Variable (Cont.)
- Constant address points to constant variable
- declaration
- Form const pointer type const variable
- e.g. const double const PIptr
- Alias or reference variable
- (must initialize when declare it)
- Form type aliasvariable
- e.g. int number10
- int numberaliasnumber
15Difference Between Alias and Pointer
- Alias must have variable value (not null).
- Pointer can be null.
- Alias will not be reassigned once declared,
- only the value will be changed
- Alias can access the variable directly,
- pointer accesses the variable indirectly
16Difference Between Alias and Pointer (Cont.)
17Assignment Statement
- Casting
- Identifier static_castltleft hand side typegt
(expression) - Example int number static_castltintgt(3.1416)
- Then number contains 3
18Loop Statement for (Cont.)
- Example
- // loop index can be defined inside for loop
- // and only meaningful within the loop
- for (int i0ilt5i)
-
- cout ltlt TRUE is printed!\n // print 5
times -
19Format Data Stream
- Need include ltiomanipgt
- setw() set total of spaces to print
- if not enough, give exact of spaces needed
- setiosflags(iosfixediosshowpoint)
- print decimal point
- setprecision()
- set total of spaces after decimal point
- format.cc
20Example without Using Class
- Example 1
- hello.cc
- hello1.cc
- hello2.cc
21Program Structures (Using Class)
- Problem Write a class Dice to simulate
- flipping two dices.
- Example 2
22Program Structures (Using Class)- Cont.
- Problem Write a class Dice to simulate
- flipping two dices. (Cont.)
- Use Modules
- driver
- class implementation
- class specification
- header file
23Compare C to Java Program
- Problem Write a class Dice to simulate
- flipping two dices.
- Example 3
- dice.java
24Reference
- Deitel Deitel C How to Program, 4th ed.,
- Chapter 15, Prentice Hall