1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language Professor Paul I. Lin, [email protected]http://www.etcs.ipfw.edu/~lin P I. Lin - Lect 2. Intro to C Language 2 Lecture 2: Introduction to C Programming Language Review • C programming Environments: MS Visual Studio.NET and Visual C++ • 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
14
Embed
C Programming Language with Applications - · PDF file1 P I. Lin - Lect 2. Intro to C Language 1 C Programming Language with Applications Lecture Note 2 Intro to C Programming Language
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• char (signed or unsigned, small integer)• int (signed or unsigned, short 16-bit, long)• float and double• Arrays: char arrays (strings), int arrays, float
pointers• struct (record)• bit-field struct and union
5
P I. Lin - Lect 2. Intro to C Language 9
Commonly used Terms in Programming Languages
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)
P I. Lin - Lect 2. Intro to C Language 10
C Identifiers (Variable 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
6
P I. Lin - Lect 2. Intro to C Language 11
Escape 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
P I. Lin - Lect 2. Intro to C Language 12
lvalues & rvalues
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
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
7
P I. Lin - Lect 2. Intro to C Language 13
Basic Elements of C
Basic Elements include: • Character set
• Constants
• Variables
• Data types
• Reserved Words
• Expressions
• Preprocessor
• Functions
• Operators
P I. Lin - Lect 2. Intro to C Language 14
Basic 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
8
P I. Lin - Lect 2. Intro to C Language 15
Basic 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
Data types
• char, int, long, float, constant, etc.
P I. Lin - Lect 2. Intro to C Language 16
Data Types
Integer types – include char, int, short, and longType Descriptionchar byte (8-bit) or 32-bit integer (-128 ..
Type Descriptionfloat single precision floating point number double double precision floating point numberlong double
Constants types – includes literal constants, Symbolic constants
Type Descriptionliteral actual number or charactersSymbolic named associated with literal – case
sensitive
P I. Lin - Lect 2. Intro to C Language 18
Example 2-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
• Run the program as a command under • MS DOS Command window (Run ->
command)• Window 32 Command window (Run -> cmd)
10
P I. Lin - Lect 2. Intro to C Language 19
Example 2-2: Running Compiled Adding Two Integers Program as DOS Command
P I. Lin - Lect 2. Intro to C Language 20
Example 2-2: Running Compiled Adding Two Integers Program as DOS Command
11
P I. Lin - Lect 2. Intro to C Language 21
Reserved Keywords
C’s 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
P I. Lin - Lect 2. Intro to C Language 22
Reserved 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 memoryVolatile -- 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
12
P I. Lin - Lect 2. Intro to C Language 23
Expressions
Expression is a statement or a part of a statement.Simple expression
x + y Simple decision making
If … else Multiple decision making
switchdefaultcasebreak
P I. Lin - Lect 2. Intro to C Language 24
Iteration 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
13
P I. Lin - Lect 2. Intro to C Language 25
PreprocessorsPreprocessing 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.
Conditional compilation #error and #program preprocessor directives # and ## Operators Line Numbers Predefined Symbolic Constants Assertions
P I. Lin - Lect 2. Intro to C Language 26
Functions
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);
14
P I. Lin - Lect 2. Intro to C Language 27
Summary
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