Title: The C Language
1Chapter 2
2Hello World
- / A first program to display Hello world /
- include ltstdio.hgt / Compiler directive /
- void main(void) / Beginning of function
definition / -
- printf("Hello World!\n") / Displays on
screen / -
- Output
- Hello World!
3Example
- / Another simple C program /
- include ltstdio.hgt / Compiler directive /
- define MAX_GRADE 20
- void main(void) / Beginning of function
definition / -
- int quiz / Declaration of a
variable / -
- quiz MAX_GRADE / Assignment
statement / - / Displays on screen /
- printf("A perfect quiz is i points.\n",
quiz) - printf("Will I get perfect scores?\n")
4Example Output
- Output
- A perfect quiz is 20 points.
- Will I get perfect scores?
5General Format
6Parts of a C Program
- Whitespace
- Comments
- Directives
- Functions
- Word Symbols
- Special Symbols
- Variables/Constants
- Statements
7Whitespace
- spaces, tabs, line endings, and blank lines
- irrelevant to the compiler
- should be used in specific places to make your
program more readable
8Outline Form
- Your program should resemble an outline with main
topics to the left and subordinate topics to the
right
9Comments
- Comments in C are shown by / and /
- Everything between / and / is ignored by the
compiler - Comments are very important to the
understandability of a program - Make sure you end all of your comments
10Directives
- Directives tell the preprocessor to do things
- Directives always begin with
- Directives end at the end of the source line
- The include in the example program tells the
preprocessor to insert the code found in the
stdio.h file at that location - The define in the example program names a
constant
11Word Symbols
- Also called reserved words or keywords
- Part of the C language
- Appendix A (p. 786) in textbook
12Special Symbols
- Mathematical symbols
- - /
- Grammar symbols
- . ,
- Symbols made from 2 characters
- lt ! gt
13Identifiers
- An identifier is the name used for a data object
(variable/constant) or for a function. - The identifiers are case sensitive.
- Using meaningful identifiers is a good practice.
14Statements
- A statement is an instruction that performs a
specific operation - quiz 20
- Every single statement ends with a semicolon
- Where the line ends is usually not important
- However, line breaks are not allowed inside of
quotations - printf(A perfect quiz is i points.\n,
quiz)
15Functions
- A function is a set of instructions that performs
an operation - A function will always have a name that can be
used to reference (or call) the function - The function name will be followed by the
argument list in parentheses (sometimes there
will be no arguments and so the parentheses will
be empty)
16Examination of printf()
17Class Assignment 1
- Write a short program that will print your full
name to the screen.
18Values in C
- Numeric values
- Real numbers
- Integral numbers (or whole numbers)
- Character values
- ASCII code is an integer value for characters
- The character A is the same as 65 in ASCII
19Special ASCII Characters
- Special characters have a backslash (\) followed
by a printable character
\0 Null \a Audible alarm \b Backspace \n New
line \r Carriage Return
\t Horizontal tab \v Vertical tab \
Apostrophe \ Quote \? Question
mark \\ Backslash
20Variables
- Represent a location or locations in memory
- Values are placed there to be used in a program
- Values can change, thus are variable
- Variable names are used to reference
21Variable Declarations
- All variables must be declared before being used
- datatype variable initialization
- int frequency
- int startValue 14
- A declaration does a number of things
- Tells the compiler how to store the value
- Defines it, memory is allocated for it
- May initialize it, giving it an initial value
22Data Types
- Data Types
- Basic (Built-In) Data Types
- Derived Data Types
- User Defined Data Types
- Basic (Built-In) Data Types
- Integer types
- Floating point types
- Character type
23Integer Data Types
- Three types based on the size, the size can be
system specific - int 32 bits (Microsoft C)
- short int 16 bits (Microsoft C)
- long int 32 bits (Microsoft C)
- Each can be
- signed (default)
- unsigned
24Integer Data Types (Cont ..)
- signed allows the value to be negative
- unsigned allows only positive (and zero)
- The range is approximately double on the positive
side
25Summary of Integers in Microsoft C
26Floating Point Data Types
- Stored in scientific notation
- Example
- 0.0235 2.35 10?2
- Expressed in standard or scientific notation
- Can use 7146.0 or 7.146e3
Mantissa
Base
Exponent
27Floating Point Types (Cont ..)
- Three flavors
- float 32 bits
- double 64 bits
- long double 80 bits
- Some valid declarations
- float wageRate 12.75
- double area, volume
- long double humongous
28Floating Point Types (Cont ..)
- A value with a decimal point stored as double
- Type can also be assigned explicitly
- Use F or f for a float
- 3.806e-3f or 0.003806f
- 3.806e-3F or 0.003806F
- Use L or l for a long double
- 7.16e-4l or 0.000716l
- 7.16e-4L or 0.000716L
29Comparison offloat and double
30Character Data Type
- Eight bits long
- Declared with char
- Used to store characters, but really numbers
(integers) - Can be
- signed char -128 to 127
- unsigned char 0 to 255
31Character Data Type (Cont ..)
- Some valid declarations
- char letter
- unsigned char index
- char topGrade A,
- bottomGrade F
- char topGrade 65,
- bottomGrade 70
32Variable Declaration Examples
16 Bits
- Examples
- short num
- short sum 120
- int product
product
num
16 bits
32 bits
120
sum
33Variable Names
- Length depends on compiler (at least 31)
- Characters allowed
- Alphabets A to Z, a to z
- Numeric 0 to 9
- The underscore character _
- Must begin with alpha or underscore
- C is case-sensitive
- Total is different from total and toTal
34Variable Names (Cont ..)
- Reserved words can not be used
- A list of reserved words is in Appendix (page
786) - Avoid naming variables with the same names as
functions
35Variable Names (Cont ..)
float _x1y
starts with digit
short int 1sum
integer not a reserved word
int integer
"for" is a reserved word
double for
float SUM
int s123
36Variable Names (Cont ..)
- Use meaningful and descriptive variable names
- int s
- int sum
Avoid the first one and try to use the second one
if your variable is supposed to store the value
of some sum
37Variable Names (Cont ..)
- Begin a variable name with a lowercase letter
- Words after the first word in a variable name are
capitalized
Good Names int test1, test2, test3 float
averageTestScore 0 int sumOfTests 0
Bad Names (but legal) int NumStudents float c,
d, e int average_grade
38Class Assignment 2
- Write a program that contains one double, one
int, and one char. - Store your height (to nearest half inch) in the
double, the last digit of your phone number in
the int, and your first initial in the char - Print these values to the screen
39Constants
- Values can not be changed
- Can be defined/declared in two ways
- Using the define directive
- define PI 3.14159
- Declaring it with the reserved word const. In
that case, it has to be initialized during
declaration - const int MAX_GRADE 100
- const double PI 3.14
40Constants (Cont ..)
- As with variables, use meaningful and descriptive
names for constants - Use all uppercase letters for the name of each
constant - Separate multiple words with underscore
Bad Names (but legal) const int
numOfStudents const float c define pi 3.14
Good Names const int NUM_OF_STUDENTS define
MAX_GRADE 100