The C Language - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

The C Language

Description:

void main(void) /* Beginning of function definition ... Special characters have a backslash () followed by a printable character Null ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 41
Provided by: scie263
Category:

less

Transcript and Presenter's Notes

Title: The C Language


1
Chapter 2
  • The C Language

2
Hello 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!

3
Example
  • / 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")

4
Example Output
  • Output
  • A perfect quiz is 20 points.
  • Will I get perfect scores?

5
General Format
6
Parts of a C Program
  • Whitespace
  • Comments
  • Directives
  • Functions
  • Word Symbols
  • Special Symbols
  • Variables/Constants
  • Statements

7
Whitespace
  • spaces, tabs, line endings, and blank lines
  • irrelevant to the compiler
  • should be used in specific places to make your
    program more readable

8
Outline Form
  • Your program should resemble an outline with main
    topics to the left and subordinate topics to the
    right

9
Comments
  • 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

10
Directives
  • 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

11
Word Symbols
  • Also called reserved words or keywords
  • Part of the C language
  • Appendix A (p. 786) in textbook

12
Special Symbols
  • Mathematical symbols
  • - /
  • Grammar symbols
  • . ,
  • Symbols made from 2 characters
  • lt ! gt

13
Identifiers
  • 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.

14
Statements
  • 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)

15
Functions
  • 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)

16
Examination of printf()
17
Class Assignment 1
  • Write a short program that will print your full
    name to the screen.

18
Values 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

19
Special 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
20
Variables
  • 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

21
Variable 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

22
Data 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

23
Integer 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

24
Integer 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

25
Summary of Integers in Microsoft C
26
Floating 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
27
Floating 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

28
Floating 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

29
Comparison offloat and double
30
Character 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

31
Character Data Type (Cont ..)
  • Some valid declarations
  • char letter
  • unsigned char index
  • char topGrade A,
  • bottomGrade F
  • char topGrade 65,
  • bottomGrade 70

32
Variable Declaration Examples
16 Bits
  • Examples
  • short num
  • short sum 120
  • int product

product
num
16 bits
32 bits
120
sum
33
Variable 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

34
Variable 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

35
Variable Names (Cont ..)
  • int xyz

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
36
Variable 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
37
Variable 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
38
Class 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

39
Constants
  • 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

40
Constants (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
Write a Comment
User Comments (0)
About PowerShow.com