C Basics - PowerPoint PPT Presentation

About This Presentation
Title:

C Basics

Description:

C is a programming language developed in the 1970s with the UNIX ... Memory Depiction. float y = 12.5; short Temperature = 32; char Letter = 'c'; short Number; ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 35
Provided by: andrew184
Category:

less

Transcript and Presenter's Notes

Title: C Basics


1
Programming
  • C Basics

2
Introduction to C
  • C is a programming language developed in the
    1970s with the UNIX operating system
  • C programs are efficient and portable across
    different hardware platforms
  • C is an extension of the C language
  • C programs should run in C
  • C supports object-oriented programming

3
General form of a C program
  • //Program description is first
  • include directives go next
  • using namespace std
  • int main()
  • constant declarations go here
  • variable declarations go here
  • assignment statements go here
  • return 0

4
General form of a C program
  • //simple program
  • include ltiostreamgt
  • using namespace std
  • int main()
  • // constant declaration
  • const double Pi 3.14159
  • // variable declarations
  • double radius
  • double area
  • // assignment statements
  • cout ltlt "Enter circle radius "
  • cin gtgt radius
  • area Pi radius radius
  • cout ltlt "Area " ltlt area ltlt endl
  • return 0

5
Syntax of the C Language
  • Reserved words (appear in blue in Visual C)
  • Reserved words have a special meaning in C.
  • The list of reserved words
  • asm, auto, bool, break, case, catch, char,
    class, const, continue, default, delete, do,
    double, else, enum, extern, float, for,
    friend, goto, if, include, inline, int, long,
    namespace, new, operator, private, protected,
    public, register, return, short, signed,
    sizeof, static, struct, switch, template,
    this, throw, try, typedef, union, unsigned,
    using, virtual, void, volatile, while

6
Syntax of the C Language
  • Identifiers (appear in black in Visual C)
  • An identifier is a name for variables, constants,
    functions, etc.
  • It consists of a letter followed by any sequence
    of letters, digits or underscores
  • Names are case-sensitive. The following are
    unique identifiers
  • Hello, hello, whoami, whoAMI, WhoAmI
  • Names cannot have special characters in them
  • e.g., XY, J-20, 007, etc. are invalid
    identifiers.
  • C reserved words cannot be used as identifiers.
  • Choose identifiers that are meaningful and easy
    to remember.

7
Syntax of the C Language
  • Comments (appear in green in Visual C)
  • Comments are explanatory notes
  • not compiled (i.e. no executable code is
    generated for comments)
  • Comments are done in two ways
  • // A double slash starts a single line comment
  • / A slash followed by an asterisk marks the
  • start of a multiple line comment.
  • It ends with an asterisk followed by
    a slash /

8
Syntax of the C Language
  • Compiler Directive include
  • It refers to a header file of library functions
    or variables (e.g. input and output).
  • When you compile your C program, the compiler
    reads in the contents of the file before
    compiling the rest of your program.
  • The included file is compiled together with the
    program.
  • There are two forms of include
  • include ltstdio.hgt // for pre-defined files
  • include "my_lib.h" // for user-defined
    files

9
Libraries
  • include loads the code from the standard
    libraries
  • include ltiostreamgt // new I/O library
  • include ltiostream.hgt // old I/O library
  • include ltstdio.hgt // standard functions
  • include ltmath.hgt // math functions
  • include ltstdlib.hgt // contains random funct
  • include lttime.hgt // time function
  • using namespace std indicates that the new C
    libraries should be used. If this line is left
    out, then the old iostream library is loaded
  • include ltiostream.hgt

10
Constant Declarations
  • Constants represent permanent values.
  • Their values can only be set in the declaration
  • const double pi 3.14159
  • They can make a program more readable and
    maintainable
  • Constant declaration syntax
  • const lttypegt ltidentifiergt ltconstant
    expressiongt
  • Examples
  • const double US2HK 7.8
  • const double HK2Yuan 1.07
  • const double US2Yuan US2HK HK2Yuan

11
Variable Declarations
  • A variable is best thought of as a container for
    a value with an address
  • Variable declaration syntax
  • lttypegt ltidentifiergt
  • Examples
  • int nickel
  • int penny
  • A variable must be declared before it can be
    used.
  • int main()
  • x 5 // illegal x was not declared

5342.23
1024
12
Variable Declarations
  • A variable can be initialized in a declaration
  • int x 3
  • Several variables of the same type can be
    declared in the same declaration
  • double total_USD, area
  • A variable must have only one type. For example,
    a variable of the type int can only hold integer
    values.

13
Types of Variable Declarations
BINARY DIGIT
  • Simple C variable types
  • int integer (32-bit integer on the PC)
    (example 1)
  • short 16-bit integer (allows 32,767)
  • long 32-bit integer (allows 2,147,483,647)
  • float floating point number (allows about 7
    digits of precision 0.1234567)
  • double double precision float (allows about 15
    digits of precision 0.12345678901234)
  • char a single character (example y)

14
Simple Number System
  • Decimal
  • What is the maximum value a 3 decimal digit
    number can hold?
  • 999
  • Binary
  • What is the maximum value a 16-binary digit
    number can hold?
  • one bit for sign
  • 0 positive (or zero)
  • 1 negative
  • 32767

digits
digits
Decimal base
Binary base
15
Memory Depiction
Memory location
  • float y 12.5
  • short Temperature 32
  • char Letter 'c'
  • short Number

16
Assignment Statements
  • Assignment syntax
  • ltidentifiergt ltexpressiongt
  • Examples
  • int n, m, k // declaration
  • n 5
  • m 6 (4 6)
  • k (n 2) m
  • k k / 2

17
Assignment Statements
  • A variable must be assigned a value before it can
    be used. Variables are not automatically
    initialized in VC.
  • int x, y // x declared, but not initialized
  • // x and y have random values
  • y x // the random value in x assigned to y
  • Once a value has been placed in a variable, it
    stays there until the program changes it.

18
Assignment Statements
  • int NewStudents 6
  • int OldStudents 21
  • int TotalStudents
  • TotalStudents NewStudents OldStudents

19
Assignment Statements
  • TotalStudents NewStudents OldStudents
  • OldStudents TotalStudents

20
  • Value1 10
  • Value2 20
  • int Hold Value1
  • Value1 Value2
  • Value2 Hold

21
Arithmetic Operators
  • Common
  • Addition
  • Subtraction -
  • Multiplication
  • Division /
  • Mod
  • Note
  • No exponentiation operator

22
Integer Division
  • Integer division produces an integer result
  • It rounds down the result
  • Examples
  • 3 / 2 evaluates to 1
  • 4 / 6 evaluates to 0
  • 10 / 3 evaluates to 3

23
Mod
  • Produces the remainder of the division
  • Examples
  • 5 2 evaluates to 1
  • 12 4 evaluates to 0
  • 4 5 evaluates to 4

24
Converting inches to feet and inches
  • 63 inch 5 foot 3 inch
  • 5 63 / 12
  • 3 63 12

25
Operators and Precedence
  • Which of the following is it equivalent to mx b
    ?
  • (m x) b
  • m (x b)
  • Operator precedence tells how to evaluate
    expressions
  • Standard precedence order
  • ( ) Evaluated first, if nested innermost done
    first
  • / Evaluated second. If there are
    several, then evaluate from left-to-right
  • - Evaluate third. If there are several, then
    evaluate from left-to-right

26
Operator Precedence
  • Examples
  • 1 2 3 / 4 - 5
  • 1 1 - 5 -3
  • 2 4 / 5 3 5 4
  • 6/5 154 13 4
  • 3.0 3 / 4
  • 9.0 / 4 2.25
  • (1 3) ((2 4 6) 3) / 2 2
  • 4 (263)/2 2 158

27
Standard Input/Output
  • cin - the standard input stream
  • Input operator gtgt
  • extracts data from input stream (the keyboard
    by default)
  • skips over white spaces
  • extracts only characters of the right form and
    performs automatic conversion to the type
    specified

28
Standard Input/Output
  • cout - the standard output stream
  • Output operator ltlt
  • inserts data into the output stream (the screen
    by default)
  • Example
  • int id
  • float score
  • cout ltlt "Enter student ID and score "
  • cin gtgt id gtgt score
  • cout ltlt "Student ID " ltlt id ltlt " score "
  • ltlt score ltlt endl

29
  • // Convert inches to feet and inches
  • // Input inches
  • // Output feet and inches
  • include ltiostreamgt
  • using namespace std
  • int main()
  • // inches to feet conversion factor
  • const int in2feet 12
  • int inches // number of inches
  • int feet // number of feet
  • coutltlt "Enter number in inches "
  • cin gtgt inches
  • // Convert inches to feet and inches
  • feet inches / in2feet
  • inches inches in2feet
  • cout ltlt feet ltlt " feet " ltlt inches ltlt " inches "
    ltlt endl

30
Assignment Conversions
  • A floating-point expression assigned to an
    integer object is always rounded down (truncated)
  • An integer expression assigned to a
    floating-point object is converted to a
    floating-point value
  • Example 1
  • float y 2.7
  • int i 15
  • int j 10
  • i y // i is now 2
  • cout ltlt i ltlt endl
  • y j // y is now 10.0
  • cout ltlt y ltlt endl

31
Assignment Conversions
  • Example 2
  • int m, n
  • double x, y
  • m 3
  • n 2.5 // 2.5 converted to 2 and assigned to
    n
  • x m/n // 3/21 converted to 1.0 and assigned
    to x
  • n xm/2
  • // m/21 integer division
  • // xm/2 double addition because x is double
  • // convert result of m/2 to double (i.e. 1.0)
  • // xm/22.0
  • // convert result of xm/2 to int (i.e. 2)
  • // because n is int

32
White Spaces
  • Extra blanks or tabs (white spaces) are ignored
    x3 x 3
  • Blank lines and comments are treated as white
    spaces
  • Code can be indented in any way
  • More than one statement can be on one line
  • int x, y x3 y 10 int z xy
  • A single statement can be continued over several
    lines
  • cout ltlt feet ltlt " feet and "
  • ltlt inches ltlt " inches" ltlt endl

33
Good Programming Style
  • Place each statement on a line by itself
    (except long cout statements)
  • x m/n
  • Use blank lines to separate sections of code
  • Use the same indentation for all statements in
    the same block of code . (Format selection,
    ALT-F8, will automatically fix indentation)
  • int main()
  • int x
  • x 5
  • return 0

34
Good Programming Style
  • Use meaningful identifier names
  • double area, sum, radius
  • Document each variable when it is declared
  • double area // area of the circle
  • double distance // distance from top to
    bottom
  • Document each segment of code
  • // Convert inches to feet and inches
  • feet inches / in2feet
  • inches inches in2feet
  • Document the beginning of the program with a
    header that tells the purpose of the program
  • // Convert inches to feet and inches
Write a Comment
User Comments (0)
About PowerShow.com