<<

QUIZ on Ch.11

We know that arrays are pointers (to the first element …) Are structures also pointers? QUIZ on Ch.11

We know that arrays are pointers (to the first element …) Are structures also pointers?

No! Structures are just plain variables. (Making them pointers would not “buy” any pointer arithmetic, since structures do not have index.) QUIZ on Ch.11 Is anything wrong with this code?

Source: http://www.codingunit.com/c-tutorial-structures-unions-typedef QUIZ on Ch.11 Rewrite it using typedef to create your own type.

Source: http://www.codingunit.com/c-tutorial-structures-unions-typedef QUIZ on Ch.11 Rewrite it using typedef to create your own type.

Source: http://www.codingunit.com/c-tutorial-structures-unions-typedef QUIZ on Ch.11 Create a pointer to index. Print using the pointer instead of the structure variable.

Source: http://www.codingunit.com/c-tutorial-structures-unions-typedef QUIZ on Ch.11 Create a pointer to index. Print using the pointer instead of the structure variable.

Source: http://www.codingunit.com/c-tutorial-structures-unions-typedef QUIZ on Ch.11 Declare a union type that can store either an integer or a double.

The union should not have a tag, instead use typedef and call the new type MYUNION. QUIZ on Ch.11 In the main program, declare an object of type MYUNION. Initialize its double with the value 3.14.

Source: http://www.codingunit.com/c-tutorial-structures-unions-typedef QUIZ on Ch.11

What’s wrong with the way we use the union here?

Source: http://www.codingunit.com/c-tutorial-structures-unions-typedef Chapter 12: and storage classes The difference between global and local scope

OK Compilation error! extern is used to declare global variables explicitly extern is used to declare global variables explicitly

If the global variable is declared in the same source file, extern is optional. However, in order to use a global declared in a different source file, extern is mandatory! We have not yet used multiple source files in our C projects. QUIZ The same as int ctr = 0;

Global variables are initialized with 0 by default. (Local variables aren’t.) Local variables can be declared at the beginning of any block! What does this program print? QUIZ QUIZ Individual work for next time:

End-of-chapter: 3, 4, 8, 10

(Not a homework Do not turn in for grading!) QUIZ

How are global variables initialized in C?

How are local variables initialized in C? QUIZ

The keyword extern is used to declare ______variables ______. When is extern really needed in a C program? static and auto variables

Lifetime of the block vs. lifetime of the entire program QUIZ QUIZ

The keyword auto is used to declare variables whose lifetime is ______. When is auto really needed in a C program? QUIZ

The keyword static is used to declare variables whose lifetime is ______. When is static really needed in a C program? Can we declare static variables in main()? What does this program print? What does this program print? External (global) and static! register class

void fun(void) { register int x; . . . . . }

Suggests to the compiler that the variable be stored in a CPU register rather than in the main memory of the computer. • Can only be used with automatic local variables! • Only used today in embedded programming. Storage class review Not in text Storage class review

volatile Warns compiler that variable can change outside of program (e.g. due to hardware interrupts), so it shouldn’t be cached. Not standard – used only in embedded programming! Homework for chapter 12 due Monday, Nov. 21 • Write a recursive factorial function (see ch.5) which has a static counter that keeps track how many recursive calls have been made. • The functions prints counter every time it’s called. • Test by calling factorial(7) in the main program (and make sure the counter goes up to 7!)

EOL 2