Data Types, Simple IO, Operators, Casts - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Data Types, Simple IO, Operators, Casts

Description:

break; Important things to remember when using decisions ... You must use break statements in your switches at the end of each case. ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 15
Provided by: Informatio55
Category:

less

Transcript and Presenter's Notes

Title: Data Types, Simple IO, Operators, Casts


1
Data Types, Simple I/O, Operators, Casts
Decisions
  • Chapters 6 - 9

2
Integer Data Types
  • Short 16 bits
  • -32,768 to 32,767
  • Unsigned Short 16 Bits
  • 0 to 65,535
  • Long 32 Bits
  • -2,147,483,648 to 2,147,483,647
  • Unsigned Long - 32 Bits
  • 0 to 4,294,967,295
  • Long Long 64 Bits
  • /-9.2233720 X 1018
  • Unsigned Long Long 64 Bits
  • 0 to 1.8446744 X 1019
  • Char 8 bits
  • -128 to 127
  • Unsigned Char
  • 0 to 255
  • Int
  • Depends on the compiler either short or long.
    Instead, you should use short or long explicitly.

3
Floating Point Data Types
  • Float 32 bits
  • -1.2 x 1038 to 3.4 x 1038
  • Double 64 bits
  • -2.2 x 10308 to 1.8 x 10308

4
Constants
  • Any sequence of only the digits 0-9 and not
    starting with a 0 is considered a decimal integer
  • Any sequence of digits beginning with a 0 is
    considered an octal int
  • Any sequence of digits or letters a-f starting
    with a 0x or 0X is considered a hexadecimal
    integer
  • Adding a trailing L to any of the preceding
    constants promotes the int to a long. ie 20L
  • A character constant is a single character
    surrounded by single quotes.
  • Any sequence of digits containing a decimal point
    is considered a double constant
  • Any sequence of digits containing a decimal point
    and ending with an f or F is considered a
    float constant.
  • A string constant is any sequence of characters
    or digits surrounded by double quotes.

5
Variables
  • Rules to live by
  • All variables must begin with a letter or the
    underscore character.
  • Subsequent characters may be letters, digits, or
    the underscore character.
  • No special characters other than the underscore
    are allowed.
  • Variables must be defined before they can be
    used. You can define and assign a value at the
    same time. ie short temp35

6
Operators
  • Mathematical
  • addition
  • - subtraction
  • multiplication
  • / division
  • modulus
  • Unary Operators
  • increment
  • -- decrement
  • address of
  • pointer dereference
  • (type) cast operator
  • (int)temp
  • Boolean
  • equals
  • ! not equal
  • greater than
  • greater or equal
  • ! not

7
Decisions
  • If-else
  • Done exactly the same way as in JAVA nuff said
  • Switch
  • switch (expression) case constant1 statement
    s break case constant 2 statements brea
    k etc default statements break

8
Switch Example
  • short temp
  • switch (temp) case 1 statements break c
    ase 2 statements break default printf(
    temp value wasnt either 1 or 2) break

9
Important things to remember when using decisions
  • C will allow you to use an assignment operator
    () inside an if or switch even if you meant to
    use an equality operator (). Be very careful
    to double check youre using the right operator
    in your decisions.
  • You must use break statements in your switches at
    the end of each case. If youre missing the
    breaks, execution will continue until it hits a
    break regardless of whether theyre inside
    another case statement.

10
Example of a missing break
  • short temp
  • switch (temp) case 1 statements break c
    ase 2 statements default printf(temp
    value wasnt either 1 or 2) break

If temp is 1, only thesestatements will execute
If temp is 2, these statements willbe executed,
but so will the printf statement because case 2
ismissing its break statement.
11
Output
  • printf
  • You already know printf will print a string
    constant to the screen, but how do you print out
    variables?
  • printf(formatting string, arg1, arg2, arg3,
    etc)
  • The formatting string is a string constant with a
    place-holder for each variable you wish to add to
    the output.
  • Place-holders all begin with a and specify the
    type of variable to be printed. See the next
    slide for a list of place-holders.
  • There is a one to one relationship between
    place-holders and arguments. The first
    place-holder will be replaced with the first
    argument, the second place-holder will be
    replaced by the second place-holder etc.

12
Output Placeholders
  • d decimal integer
  • u unsigned integer
  • o octal integer
  • x hexadecimal (small letters)
  • X hexadecimal (uppercase letters)
  • c ASCII character
  • s string
  • f decimal
  • e scientific notation
  • g automatically selects either decimal or
    scientific notation based on size of variable
    being printed
  • p print the address of a pointer in
    hexadecimal notation

13
Input From Keyboard
  • scanf
  • Scanf, is much like printf in that it takes a
    format string, and variable arguments.
  • Scanf(format string,arg1,arg2, etc)
  • The arguments to scanf MUST be memory addresses.
    Pointers and arrays (and therefore strings) are
    already memory addresses, so nothing needs to be
    done to them before they are passed to scanf.
    For other types of variables, you must prepend
    the address operator () to your variable name.
  • If you have two shorts declared as a and b, the
    scanf command to fill them would bescanf(d
    d,a,b)
  • A single space in the format string of scanf will
    accept any number of white space characters. You
    never want to end your format string with a
    space, otherwise your program will never exit the
    scanf call.

14
Input Place-holders
  • d decimal integer
  • o octal integer
  • x hexadecimal
  • X hexadecimal
  • c ASCII character
  • s string
  • f decimal
  • e scientific notation
  • g either decimal or scientific notation

You can put an l after the percent sign in any
integer or float placeholder to convertthe input
into a long integer or float respectively. Ie
ld or lf. You can put an hafter the percent
sign to force the use of a short integer. Ie
hd. There is noshort float, so using the h
with any float type has no effect.
Write a Comment
User Comments (0)
About PowerShow.com