C Programming Part 2: Variables

Topics

  • Declaring, Initializing, and Assigning Variables
  • int, char float and double types
  • <stdint.h> types
  • Literals
  • Magic numbers: const and #define
  • static and extern modifiers
  • enum type

Class Slides

Resources

Study Questions

  1. What are variables?
  2. What is a variable’s type and size?
  3. What does it mean that C is typed?
  4. Does an assignment statement return a value?
  5. What characters can you use in your variable names?
  6. How do you use hex literals in C?
  7. What is the char datatype used for?
  8. Can you use the char datatype to store integers?
  9. (T/F) Floating point types can represent any fractional value.
  10. What is the value of sizeof(char)? sizeof(int)? sizeof(int32_t)?
  11. (T/F) A long int is always larger than an int?
  12. What does the const qualifier do?
  13. What does #define do?
  14. Should you end #define directives with a semicolon?
  15. What happens if you put static in front of:
    • A function?
    • A global variable?
    • A local variable?
  16. What is extern used for? Does it allocate memory for variables?
  17. What is the enum type used for? What value does it start at? Can you override it?

  18. What does the following code print?
     #include <stdio.h>
    
     #define PLUS1 x+1
     int main() {
       int x = 10;
       int y = 5 * PLUS1;
       printf("%d\n", y);
     }
    
  19. What is the name of the mystery function shown below (it is a standard library function):
     int mystery(char s[]) {
         int i = 0;
         while (s[i] != '\0')
             ++i;
         return i;
     }
    
  20. What does the code below print out?
     #include <stdio.h>
    
     int main() {
         enum {hello=3, goodbye} myEnum = goodbye;
         printf("%d\n", myEnum);
     }
    
  21. (T or F) In the program above, myEnum is an automatic (local) variable.

  22. What does the following program print out?
     #include <stdio.h>
    
     int main() {
         printf("%d\n", '1');
     }
    
  23. What does the following program print out?
     #include <stdio.h>
    
     int main() {
         printf("%d\n", "13");
     }