Title: Distance Learning Center
1Distance Learning Center
- Lecture 3
- C Programming Language with Applications
- Melissa Lin, IT Consultant
- HenEm, Inc.
- Parkville, Missouri
- http//www.ecet.ipfw.edu/linm
2Lecture 3 Introduction to C Programming Language
- Review
- C programming Environments MS Visual Studio.NET,
Linux, and Visual C 6.0 - Write a C program under Windows Environment
- Characteristics of C
- Basic Elements of a C Programming
- Data Types
- Example 3-1 Adding Two Integers
- Example 3-2 Running Adding Two Integers As DOS
Command - Reserved Keywords
3C Programming Environments
- C Programming Environments
- Linux/Unix Console Applications
(Character-oriented) using ANSI C libraries and
API (Application Program Interface) functions - Windows 32 Console Applications
(Character-oriented) using ANSI C standard
libraries - Windows Applications using API functions
4Writing C Programs Using MS Visual Studio .NET in
Windows Environment
- Edit a new program
- Use the mouse to click on the File menu and then
choose - File ? New ? Project (to create a new project)
- Edit a C program (Example - welcome.c)
- Save the program
- Save the program (from the File menu)
- File ? Save or Save As
- File Name welcome.c
- Add this program to the project
5Writing C Programs Using MS Visual Studio .NET in
Windows Environment (continue)
- Compile and build the program
- From Build menu, choose
- Build ? Build Solution
- When syntax error occurs, click on the error line
and correct errors - Run and test the program
- Debug ? Start without Debugging, to run program
- Or run the executable code directly under DOS
environment
6Characteristics of C
- Basic C Characteristics
- A General Purpose Programming Language
- Highly Portable
- Generate Fast Codes
- Small Size Language
- Relative Low-level
- Memory access ( address), register
- C Standard library functions
7Characteristics of C (continue)
- Modern Control Structures
- - if, if-else, for, while, do-while, switch,
case, - Bit-wise operations
- (AND)
- (OR)
- (NOT)
- (EXOR)
- ltlt (Left Shift)
- gtgt (Right Shift)
- Logical operations
- Logical AND
- Logical OR
8Characteristics of C (continue)
- Pointer Implementation
- Data Types
- char (signed or unsigned, small integer)
- int (signed or unsigned, short 16-bit, long)
- float and double
- Arrays char arrays (strings), int arrays, float
arrays, etc - Pointers char, int, float, struct, functions,
pointers - struct (record)
- bit-field struct and union
9Commonly used Terms
- Statements
- Describe some program actions
- Example printf(Welcome to C \n)
- Execute for its effect
- Type of Statements
- Declaration statement
- Expression statement (arithmetic operations,
assignment, function calls) - Compound statements (a series of statement to be
treated as a single statement) - Selection statement (if, else, switch)
- Iteration statement (while, do, for)
- Jump statement (break, continue, return)
10C Identifiers (names)
- A sequence of letters, digits, underscores
- Must begin with a letter (not a digit)
- Cannot be a key word
- Can be any length, but only the first 31
characters are recognized - Case sensitive (lower and upper -case letters are
treated differently) - Examples
- x x1 name
- Number accountNumber value
11Escape Sequence
- The backslash (\) is called an escape character.
When encountering a backslash in a string, the
complier looks ahead for the next character and
combines it with the backslash to form an escape
sequence. - Example Printf(Welcome to C \n)
- Escape Sequence Description
- \n New Line
- \t Horizontal Tab
- \a Alert Sound the system bell
- \\ Insert a backslash in a string
- \ Insert a double quote in a string
- \000 octal number
- \xhh hexadecimal number
12lvalues rvalues
- A lvalue is an expression referring to an object
that may be altered - An object is a region of memory that can be
examined and stored into - lvalues means left values, variable names are
said to be lvalues because they can be used on
the left side of an assignment operator - rvalues means right values, constants are said to
be rvalues because they can be used only on the
right side of an assignment operator
13Basic Elements of C
- Basic Elements include
- Character set
- Constants
- Variables
- Data types
- Reserved Words
- Expressions
- Preprocessor
- Functions
- Operators
14Basic Elements of C (continue)
- Character set
- A, Z, a, . z, 0, 1, 2, , 9, and special
symbols - Constants
- Integer constants
- Octal 0, 1, 2, 3, 4, 5, 6, 7
- Decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- hexadecimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b,
c, d, e, f or (A, B, C, D, E, F) - Example decimal 31 can be written as octal 037,
or as hexadecimal 0x1f
15Basic Elements of C (continue)
- ASCII character constants
- a, A, .. z, Z, 0, 1, 2, 9
- String constants
- W or The C programming Language
- Floating-point number constants
- 123.5 0.0 0.1E-6
- Variables
- int, long, char, float, double, array, etc.
- Data types
- char, int, long, float, constant, etc.
16Data Types
- Integer types include char, int, short, and
long - Type Description
- char byte (8-bit) or 32-bit integer (-128 ..
0..127) - int integer (32-bit) integer 16-bit (-32,768
.. 0 .. 32,767) - short 16-bit integer signed
- long long integer (double size of integer)
- unsigned char
- unsigned int
- unsigned short
- unsigned long
17Data Types (continue)
- Floating-point types represent floating-point
(real) numbers - Type Description
- float single precision floating point number
- double double precision floating point number
- long double
- Constants types includes literal constants,
Symbolic constants - Type Description
- literal actual number or characters
- Symbolic named associated with literal case
sensitive
18Example 3-1 Adding Two Integers
- Adding two integers
- Using scanf() function to obtain two integer
typed numbers entered by a user at the keyboard - Calculating the sum of both values
- Printing the result using printf() function
19Example 3-1 Adding Two Integers (cont.)
20Example 3-1 Adding Two Integers (cont.)
21Example 3-1 Adding Two Integers (cont.)
Solution Explorer
22Example 3-1 Adding Two Integers (cont.)
23Example 3-1 Adding Two Integers (cont.)
24Example 3-1 Adding Two Integers (cont.)
25Example 3-1 Adding Two Integers (cont.)
26Example 3-1 Adding Two Integers (cont.)
27Example 3-1 Adding Two Integers (cont.)
28Example 3-1 Adding Two Integers (cont.)
29Example 3-1 Adding Two Integers (cont.)
30Example 3-2 Running Compiled Adding Two Integers
Program as DOS Command
31Example 3-2 Running Compiled Adding Two Integers
Program as DOS Command
32Example 3-2 Running Compiled Adding Two Integers
Program as DOS Command
33Reserved Keywords
- Cs Keywords
- auto double int struct
- break else long switch
- case enum register typedef
- char extern return union
- const float short unsigned
- continue for signed void
- default goto sizeof volatile
- do if static while
34Reserved Keywords for Visibility and Life Time
Controls
- Auto -- dynamic, local to a program or function
module short term variables on stack memory - Extern -- to be available at the program linking
time - Static -- long term variables on data memory
- Volatile -- advise an optimizing compiler that
the variable is important and do not attempt to
remove it if it not used - Const -- longest life, cannot be altered after
the definition read only
35Expressions
- Expression is a statement or a part of a
statement. - Simple expression
- -- x y
- Simple decision making
- -- If else
- Multiple decision making
- -- switch
- -- default
- -- case
- -- break
36Iteration Control
- Iteration uses a repetition structure
- Examples
- for - for loop iteration
- while - while loop
- do/while - do/while
- continue - exit the inner loop and restart
from the outer loop - break - exit a block
37Preprocessors
- Preprocessing occurs before a program is
compiled. Its actions are the inclusion of other
files, definition of symbolic constants and
macros, conditional compilation of program code
and conditional execution of preprocessor
directives. - include preprocessor directive
- define preprocessor directive
- Symbolic constants
- Macros
- Conditional compilation
- error and program preprocessor directives
- and Operators
- Line Numbers
- Predefined Symbolic Constants
- Assertions
38Functions
- Functions allow the programmer to modularize a
program. All variables defined in function
definitions are local variables they are known
only in the function in which they are defined. - return -- return from a function and go back to
caller - void -- no values associated with parameter or
arguments in a function - A list of parameters as local variables
- Examples
- int main()
- printf(Welcome to C \n)
- scanf(Enter Input d , intetger1)
39Summary
- Review
- C programming Environments
- Write C programs under Windows Environment
- Characteristics of C
- Basic Elements of the C Programming
- Data Types
- Reserved Keywords
- Write a Simple Addition Program in C
- Next
- Constants and Variables
- Operators of the C Language
40Question?