Variables and Types Flashcards

(38 cards)

1
Q

C Fundamental Types

A
  • This figure shows all the basic types we
    have, a few more than we have in Java
    long double
    double
    float
    void *
    • You have types for
      real (floating point)
      values.
      – Including long
      double, which we
      don’t have in Java
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Integer Types

A
  • Most of these are for storing integers.
  • There are many integer type specializations for:
  • Different sizes
    • Including long long, which we don’t have in Java
  • Different signed-ness
    • Mostly, signed is default
    • Except for char(signed-ness of char is platform dependent)

signed long long unsigned long long
signed long unsigned long
signed int unsigned int
signed short unsigned short
signed char unsigned char

                    _Bool
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s in a Name(Repetitive Names)

A
  • There are different ways to name the same integer type.
  • Signed is usually the default, so
    – int is the same as signed int
    – long is the same as signed long
    – short is the same as signed short
  • The word int is optional for short, long and long
    long, so:
    – long int is the same as long
    – long long int is the same as long long
    – short int is the same as short
    – …
  • So, there are multiple names for the same type:
    – signed long int is the same as long
    – unsigned short int is the same as unsigned short
    – …
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

All Fundamental Types(1 Rule of Order)

A
  • The standard makes few guarantees about the sizes of these types.
    • For example, long may or may not have more capacity than int
      –…,but it must not
      have less capacity.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Integer Capacity

A
  • The exact range of a type is platform dependent
  • In general, we can figure this out based on the memory used for each variable type.
  • Or, we can just look in the limits.h header
    – It’s a file: /usr/include/limits.h (on the common
    platform)
  • With constant names:
    – SHRT_MIN, SHRT_MAX, USHRT_MAX
    – INT_MIN, INT_MAX, UINT_MAX
    – LONG_MIN, LONG_MAX(signed range), ULONG_MAX(unsigned range)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Integer Capacity On the Common Platform

A

signed/unsigned char - 8 bits
signed/unsigned short - 16 bits
signed/unsigned int - 32 bits
signed/unsigned long - 64 bits
signed/unsigned long long - 64 bits(same capacity as long(but no smaller than long)
(to see more of table, see doc: https://docs.google.com/document/d/1oSf5X8tu8wevaKgDcBTqtf9Z1SeA8B9sghvy_fIcHF8/edit?usp=sharing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Real Number Capacity On the Common Platform

A

float - 32 bits
double - 64 bits
long double - 128 bits
(to see more of table, see doc: https://docs.google.com/document/d/1oSf5X8tu8wevaKgDcBTqtf9Z1SeA8B9sghvy_fIcHF8/edit?usp=sharing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Making Variables(4 elements)

A
  • A variable has:
    – A name : that’s a legal identifier name
    – A type : that’s how the compiler interprets the
    contents of memory representing the variable
    – A scope : that’s the section of code that can
    access the variable
    – A storage class : that’s how it’s stored and
    initialized
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Static Typing

A
  • In C (like Java), all variables are statically typed
    – The type of a variable type can’t change as the program runs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dynamic Typing(difference from Static typing)

A

– Lots of languages have dynamic typing (e.g.,
JavaScript, PHP, Python)
- * It’s a tradeoff in:
– flexibility
– performance
– ability to detect errors at compile time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Auto(Local) Variable Scope

A
  • Declared inside a function
    – … including function
    parameters.
  • In scope from the declaration to the end of
    the surrounding block
  • These are called auto variables
    – Their storage class is called auto
    – In fact, auto is a keyword in C
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Global Variable Scope

A
  • A variable defined outside any function has
    global scope
  • Its lifetime is the whole execution of the program
    – Their storage class is called static
  • Any code can access this variable.
  • There’s a potential for name collision
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Shadowing

A
  • Two variables can have the same name … as long as they are declared in different scopes.
    – They are independent variables, just with the same name.
  • How does the compiler decide which one you
    want?
    – You get the one in the one declared in the narrowest surrounding scope.
  • We say the one in the narrower scope shadows the one in the wider scope
  • Scoping rules let you shadow variables in lots
    of languages
    – e.g., in Java, a local variable can shadow a field.
  • This is a clever trick … that you should probably avoid
    using too cleverly.
  • There’s at least one place where it’s nice to
    have.
    – In macro definitions … later.
    Examples in doc:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Literal Values

A
  • For variables, the declaration determines the
    type.
    int x = 45;
    double y = 17;
  • For numbers, the compiler will decide on a
    type
    f( 315, -1.45 )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Integer Literal Values

A
  • For a sequence of digits, the compiler assumes you want the int type
  • Unless the value is too large to fit in an int
    – … then, it assumes you want the smallest integer type that’s big enough:
    – Signed int if the value is not too big (e.g., 75)
    – Signed long if the value is too big for int (e.g.,
    5271486214)
    – Signed long long if it’s too big to be a long
  • You can say what type you want for literal
    values
    – For unsigned, put a U at the end (e.g., 351U)
    – For long, put an L at the end (e.g., 351L)
    – For long long, put LL at the end (e.g., 351LL)
    – You can combine these, in any order (e.g., 351ULL)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Floating Point Literals

A
  • With a decimal or scientific notation, the
    compiler assumes double
    – Like this: 3.14 (or 3.0 or 0.14 or .14)
    – Or in scientific notation: 6.022E23
  • You can add a character to specify the type
    – The smaller, float type: 3.14F
    – The larger, long double type: 3.14L
    Does the type on a literal matter?
  • Literals (like 42, 42U, 1.5, ‘a’, “hi”) have specific types that affect conversions, promotions, signed/unsigned behavior, overloads (well — C has no overloads, but it matters for which function prototype/match or varargs you pick), integer promotions, arithmetic results, and whether operations are well-defined or overflow/underflow/UB.
  • signedness and width affect arithmetic, comparisons, and shifts
17
Q

C99 Boolean Type

A
  • Originally, C had no specific boolean type
  • C would let you use any numeric type:
    – With 0 being interpreted as false
    – And anything else being true.
  • This still works just fine.
    int i = 100;
    while ( i ) {
    i -= 1;
    }
    double f = 10;
    while ( f ) {
    f -= 0.1;
    }
    It even works with floating point types … but this is probably a bad idea.
  • In C99, they added a type for boolean values.
  • They had to give it a goofy name, _Bool, a
    name nobody would have been using for
    anything.
  • Still, internally, boolean values are 0 for false,
    Anything else for true, With 1 as the typical
    value for true.
    _Bool b;
    b = 0; I’m false.
    b = 1; I’m true.
    b = 100; I’ll still just be 1.
18
Q

A Time before Boolean

A
  • Earlier, we pretended functions like isalpha()
    returned bool.
    – This was a lie.
    bool isalpha( int ch ); X
  • These functions pre-date the bool type.
    – They really return int.
    int isalpha( int ch );
    int isspace( int ch );
    int isdigit( int ch );
19
Q

Meet stdbool.h

A
  • _Bool works, but it looks like a strange type
    name.
  • You can include stdbool.h
    – You get the name the name bool as an alias for _Bool
    – But wait, there’s more. You also get
    – false : a preprocessor constant for 0
    – true : a preprocessor constant for 1
  • This header lets you code as if bool is a built-in type
20
Q

Conditional Expressions

A
  • The logical operators (!, && and ||) were defined before we had a
    _Bool type.
  • They expect int parameters
    – Zero -> false
    – Anything else -> true
  • Evaluate to an int result
    – Zero -> false
    – One -> true
    int i = 276, j = 0;
    float f = 2.718;
    x = i || j; This evaluates to an
    int (with value 1)
    y = f && j; This also evaluates to
    an int (with value 0)
20
Q

Short Circuiting

A
  • The && and || operators are guaranteed to
    be short circuiting (just like Java)
    – This can improve performance; we stop evaluating early if the outcome is determined.
    – More importantly, it lets us guard some tests with other tests that make sure they are safe.
    if ( people != 0 && slices / people >= 2 ) {
    printf( “Everyone can have two slices!\n” );
    }
    slices / people >= 2 gets evaluated if people != 0 is true
20
Q

C Expressions

A
  • C and Java have many of the same operators
  • Here are most of them, highest-precendence
    to lowest.
    Operator Description
    ++ – Postincrement, postdecrement
    ++ – Preincrement, predecrement
    + - Unary positive, negative
    ! Logical not
    (type) Type cast
    sizeof Well, it’s the sizeof operator
  • / % Multiply, divide, mod
    + - Add, subtract
    < <= > >= Relational operators
    == != Equals, not equals
    && Logical and
    || Logical or
    ? : Ternary operator
    = += -= *= … Assignment, and its friends
    , Comma operator
21
Q

Meet Sizeof

A
  • sizeof is an operator that tells you how much
    memory it takes to store something.
  • It works for types:
    sizeof( int )
  • Or variables:
    sizeof a (The parentheses aren’t required, but remember, sizeof has fairly high precedence.)
  • Or other values:
    sizeof( x * y )
  • We’re going to use this operator a lot.
22
Q

How Much Memory( function for reading how much a type)

A
  • C has a type for talking about how much memory
    you need
  • It’s called size_t
  • In practice, this type is aliased to one of the types you’ve already seen.
    – Like unsigned long on the common platform.
  • There’s a conversion specification for this type:
    printf( “%zd\n”, sizeof( x ) ); -> For printing
    size_t s; -> For reading
    scanf( “%zd”, &s ); -> For reading
23
Meet The Comma Operator
* You can chain together multiple expressions with a comma – Sub-expressions get evaluated in order, left-to-right – The whole expression evaluates to the value of the last sub-expression. int a = 5, b = 10, c = 20; int d; d = ++a, b--, ++c; -> Here, d gets 6 (because = has higher precedence than comma) d = ( ++a, b--, ++c ); -> Here, d gets 21 * Mostly useful for packing multiple expressions in a space made for one expression. for ( int i = 0; i < n; i++, x-- ) { ...
24
Meet the Ternary Operator
* Java has this, but sometimes students don’t get to see it until this class. * It’s an expression that works like an if/else statement. test ? expr1 : expr2 Standard if/else: // Choose the larger of a and b. c = a; if ( b > a ) c = b; // Choose the right word to print. if ( x == 1 ) printf(“Price: %d dollar\n”, x); else printf(“Price: %d dollars\n”, x); Ternary: // Choose the larger of a and b. c = a > b ? a : b; // Choose the right word to print. printf(“Price: %d %s\n”, x, x == 1 ? “dollar” : “dollars”);
25
Type Conversion
* C can convert between its various basic types (int, float, unsigned short, long, bool, etc. ) – This can be specified explicitly, via a type cast – Or implicitly, by combining types in an expression, or providing one type where the compiler expects another float f; char c; short s; double d; … c = (char) ( d * (double) ( f + (float) s ) ); -> explicit c = d * ( f + s ); -> implicit
26
The Type Cast
* A type cast is a kind of operator – It lets the programmer specify a type conversion * Syntax: (destination_type) expression * Example: c = (unsigned char) d; * Remember, the type cast has very high precedence * A special case, casting to void – We can tell the compiler we don’t need the result of an expression – Example: (void) f( x ); – Why? Mostly to document behavior or suppress compiler warnings.
27
Implicit Conversions
* Internally, the hardware typically can’t perform arithmetic on mixed types * C will automatically insert numeric conversions in order to evaluate an expression * Lots more than Java would perform automatically. c = d * ( f + s ); char double float short * The standard defines a set of Usual Arithmetic Conversions
28
Usual Arithmetic Conversions
* The capacity of each type can depend on the compiler and the platform. * C describes rules general rules that can be applied to any platform. – We will review these in general. – And, for the common platform. As we move up this chart, we tend to get greater capacity. – We can think of some types as wider or narrower than others. * Five rules to consider, depending on the two operand types. – For example: int * float unsigned char + char short - unsigned long bool / long long short % long
29
Usual Arithmetic Conversions(Rule 1: Floating Type)
* If an operand is a floating point type, convert to the wider floating-point type. – If one operand is a floatingpoint type, convert to that. – If both are floating-point types, convert to the wider one. int * float -> float * float double + unsigned char -> double + double float - long double -> long double - long double
30
Usual Arithmetic Conversions(Rule 2: Lower than int)
* OK. What if neither type is real-valued? * C uses the rank to describe the relative size of its integer types. * Otherwise, if either operand rank is lower than int, promote to (at least) the rank of int. * Lower ranks are promoted to signed int if that is large enough to hold all values from lower-ranked types. – This is how the common platform works. - An int can hold any value stored as a short, unsigned short, signed char, unsigned char, or _Bool. char * unsigned short -> int * int bool + short -> int + int char – unsigned char -> int - int
31
Usual Arithmetic Conversions(Rule 3: Same signed-ness)
* If both types have the same signed-ness, promote to the higher-ranked type.
32
Usual Arithmetic Conversions(Rule 3: Same signed-ness)
* If both types have the same signed-ness, promote to the higher-ranked type.
33
Usual Arithmetic Conversions(Rule 4: Different signed-ness different type)
if the signed type can store every value the unsigned type can, convert to the signed type.
33
Usual Arithmetic Conversions(Rule 5: Different signed-ness same type)
* Otherwise, if the signed type isn’t large enough, use the unsigned version of the higher-ranked type. * So, given two equal-ranked types of different signedness, C will use the unsigned type long * unsigned int -> long * long int + unsigned long -> unsigned long + unsigned long long - unsigned long -> unsigned long long - unsigned long long
34
Assignment and Implicit Conversion
* Implicit conversions can also happen in assignment * Here, it’s easy – the source value just gets converted to the destination type. Resulting int converted to double for assignment. char c; int i; double d; d = i * c; Resulting float converted to bool for assignment. long l; float f bool b; b = l - f;
35
Function Parameters and Implicit Conversion
* In C, function parameters are also easy. – Since there’s no function overloading. * Parameters values get converted to the parameter types expected by the function. * Return value gets converted to the return type.