Podcast
Questions and Answers
What does the static
keyword do in C?
What does the static
keyword do in C?
- It makes a variable visible within the function.
- It makes a variable visible to all parts of the program.
- It indicates that the variable's value cannot be changed.
- It makes a variable visible only inside the module where it is defined. (correct)
In C, a global variable must be declared using the static
modifier.
In C, a global variable must be declared using the static
modifier.
False (B)
What is the primary purpose of header files in C modules?
What is the primary purpose of header files in C modules?
specify interfaces for modules
The ______
modifier is used to distinguish a global variable declaration from its definition.
The ______
modifier is used to distinguish a global variable declaration from its definition.
Match the following:
Match the following:
What is the potential issue with allocating local variables inside a function?
What is the potential issue with allocating local variables inside a function?
Static variables inside a function are re-initialized each time the function is called
Static variables inside a function are re-initialized each time the function is called
What value are static variables initialized to by default?
What value are static variables initialized to by default?
In C structures can store several values of several ______ data types.
In C structures can store several values of several ______ data types.
Match struct with its usages:
Match struct with its usages:
What is the main purpose of using the void *
type in C?
What is the main purpose of using the void *
type in C?
Header files should contain function definitions to ensure code reusability across multiple source files.
Header files should contain function definitions to ensure code reusability across multiple source files.
What is a typical naming convention for a header file, given a source file named "myModule.c"?
What is a typical naming convention for a header file, given a source file named "myModule.c"?
A C module may use void *
type to track ______
data.
A C module may use void *
type to track ______
data.
Match the module component to its purpose:
Match the module component to its purpose:
What issue do header guards primarily aim to prevent?
What issue do header guards primarily aim to prevent?
Header guards protect against circular dependencies between header files.
Header guards protect against circular dependencies between header files.
What preprocessor directives are commonly used to create header guards?
What preprocessor directives are commonly used to create header guards?
Makefiles are read by the ______
utility.
Makefiles are read by the ______
utility.
Match the Makefile element with its purpose:
Match the Makefile element with its purpose:
Which of the following is NOT a typical benefit of using Makefiles?
Which of the following is NOT a typical benefit of using Makefiles?
In a Makefile, a tab character must precede the build commands.
In a Makefile, a tab character must precede the build commands.
Other than a tab character, what character starts a comment in a Makefile?
Other than a tab character, what character starts a comment in a Makefile?
In a Makefile, macros are used to represent variables that store ______
.
In a Makefile, macros are used to represent variables that store ______
.
Match the level of visibility with their description:
Match the level of visibility with their description:
What is the purpose of function declarations in header files?
What is the purpose of function declarations in header files?
Using global variables is generally recommended in C programming to improve code maintainability.
Using global variables is generally recommended in C programming to improve code maintainability.
What is the purpose of guards in header files?
What is the purpose of guards in header files?
The ______
operator can be used to dereference a pointer to a structure member.
The ______
operator can be used to dereference a pointer to a structure member.
Connect file extensions to their purpose:
Connect file extensions to their purpose:
What is the consequence of not using header guards in a project with multiple source files?
What is the consequence of not using header guards in a project with multiple source files?
In C, nested function definitions are permitted.
In C, nested function definitions are permitted.
What keyword in C declares a variable that can be accessed from multiple files?
What keyword in C declares a variable that can be accessed from multiple files?
In a Makefile, dependencies separated by spaces on the right side of the colon, represent files that, when changed, trigger ______
.
In a Makefile, dependencies separated by spaces on the right side of the colon, represent files that, when changed, trigger ______
.
Match the following terms related to C programming with their descriptions:
Match the following terms related to C programming with their descriptions:
In C, what is the primary reason for using modules to organize code?
In C, what is the primary reason for using modules to organize code?
Using the static modifier in C guarantees thread safety for global variables.
Using the static modifier in C guarantees thread safety for global variables.
How does using void *
contribute to abstraction in C?
How does using void *
contribute to abstraction in C?
In a Makefile, a ______
is a file that is generated from dependencies.
In a Makefile, a ______
is a file that is generated from dependencies.
Insanely Difficult: Consider a large C project where multiple developers are contributing. If a developer omits header guards in a commonly included header file and inadvertently introduces a circular dependency involving dozens of header files, what is the most likely immediate consequence?
Insanely Difficult: Consider a large C project where multiple developers are contributing. If a developer omits header guards in a commonly included header file and inadvertently introduces a circular dependency involving dozens of header files, what is the most likely immediate consequence?
Insanely Difficult: Describe a scenario where a C program must use global variables, despite the well-known risks, and briefly justify the exception.
Insanely Difficult: Describe a scenario where a C program must use global variables, despite the well-known risks, and briefly justify the exception.
Flashcards
What are Global variables?
What are Global variables?
Variables that can be accessed by any function in the program.
Static modifier
Static modifier
A modifier. All functions are global unless they have a static modifier.
What is an external variable declaration?
What is an external variable declaration?
Declared with the extern
keyword to distinguish from a variable definition.
Local variables
Local variables
Signup and view all the flashcards
What does the static
keyword mean in this context?
What does the static
keyword mean in this context?
Signup and view all the flashcards
Structure in C
Structure in C
Signup and view all the flashcards
What is a module?
What is a module?
Signup and view all the flashcards
What does a C module contain?
What does a C module contain?
Signup and view all the flashcards
Do modules have header files?
Do modules have header files?
Signup and view all the flashcards
What are include guards?
What are include guards?
Signup and view all the flashcards
What is a Makefile?
What is a Makefile?
Signup and view all the flashcards
Target in a Makefile
Target in a Makefile
Signup and view all the flashcards
Dependencies (Makefile)
Dependencies (Makefile)
Signup and view all the flashcards
Build Commands
Build Commands
Signup and view all the flashcards
What does the make
utility do?
What does the make
utility do?
Signup and view all the flashcards
What is the use of void *
?
What is the use of void *
?
Signup and view all the flashcards
The C Commandments: Declaration Rule
The C Commandments: Declaration Rule
Signup and view all the flashcards
Study Notes
- Assignment 1 is due on February 07
- Assignment 2 will be out on Friday
- Lab 4 is scheduled for this week
Key Concepts
- Programs can consist of multiple files and millions of code lines, necessitating proper structure and organization
- Encapsulation and abstraction in C rely on programmer conventions and idioms
Programmer Conventions and Practices
- Avoid global variables
- Utilize the static modifier to create simple modules
- Specify interfaces for modules with header files
- Use the void * type to hide implementation details
- These conventions guide modern C development practices
Three Levels of Visibility in C
- Global definitions are visible to any part of the program
- Static definitions are visible only inside the module where defined
- Local definitions are visible only within the function
- Types are compile-time artifacts included where needed
- C does not permit nested functions.
- Visibility for each definition type is:
- Types are Static and Local
- Variables are Global, Static, and Local
- Functions are Global and Static
Global Variables and Functions
- Global entities are accessible by any function
- All functions are global unless designated with a static modifier
- Variables outside a function are global unless specified with static
- Global variables/functions must have unique names to prevent linker errors
- The "extern" modifier must be used
Local Variables and Types
- Local types/variables are defined within a function body
- Local variables are allocated on the program stack
- Large variables, like arrays, should be allocated on the stack
- Local variables exist only during the function call
Static Variables, Functions, and Types
- Static entities are defined and visible only in their file
- Types are static by default without modifiers
- Variables/functions are static if they have the static modifier
- Static variables are initialized to 0 by default
Structures in C
- Structures store multiple values of different types
- When data needs to be grouped together
- Examples are database tables, or contact information
- struct struct_name {} is the syntax
Struct Example
- char name[20];
- char addr[100];
- long long phone;
- } Is how to define an AddrBook structure
Using Pointers With Struct
- structAddrBook t * Con; is the way to point to a structure using memory
- the arrow operator -> is one way to reference the structure memory
Arrays with Struct Type
- Structs support arrays
- To be used with databases
- Consider the AddrBook_t can be used to show multiple addresses
- Similar to a database table holding several addresses
Modules
- A Module is a code file that provides specific functionality while hiding how that functionality is implemented
- It is analogous to a Java class, which has a public interface but private variables and methods
Modules consist of
- A code file that contains; One or more global functions representing the public interface of the module, and static functions and variables that are not part of the public interface
- A header file (of the same name) that contains function declarations of the public interface, and type definitions used by the public interface
- Additionally, a module may use void * type to track instance data
Guards in Header Files
Multiple Inclusion Problem
- Occurs if Node.h defines a struct “node”, Stack.h includes “node.h”, queue.h includes “node.h”
- User.c includes both stack.h and queue.h with struck node defined twice
- C does not allow struct node to be defined twice
- Programmers may not know “node.h” is included by both stack.h and queue.h
Using Guards to Avoid Multiple Inclusion
- Use a guard to ensure header files are only processed once
- The C preprocessor can do conditional preprocessing
- Use the following template: #ifndef HEADER_H #define HEADER_H… #endif
- Using #iffed to only first-time include if HEADER_H
Brief Introduction to Makefiles
- Reduce compile times
- Code files that have not been compiled before
- Code files that have been changed singe the last compile
- Code files that include header have been changed
- Want to automate build with gcc
- Want to use automating using a special file
Makefiles and Make
- A Makefile
- I a text file
- Contains rules for how to build a program from its source
- Created by the developer
- Read by the make utility
- The make utility
- Reads in the Makefile
- Processes the rules
- Executes the build command in the rules
Makefile Compositions
- Consists of the following 3 sections:
- Suffixes
- Marcos -Dependencies / build rules
- Macros are simply variables that store text so do not have duplicate info
- The following from is to dependency: Target: Dependencies, Build Commends
Makefile Tips
- Targte is a file generate from the denpendecies
- Dependencies are 0 or more files that if modified, necessitate recompilation of the target
- Build commands are shell compiles
What does make do?
- Reads in makefile
- Processes macros and dependencies
- The first dependency is the final the final target
- For each dependency:
- For each of the dependency files compares to the right of the colon
- Performs the dependencies that specifies how the file is built
- For each pf the dependency files:
- Checks the dependencies of the file
- Comparison the target file timestamp
- If the target has to be rebuilt, the build commands are executed
Abstraction and Encapsulation through void *
- Used with multiple stacks
- The functions Take struct node *n as they 1st argument
- Code implementation is hidden
Static Code Analysis - The C Commandments
- Do not put function or variable definitions in header files as the the linker complains about multiple def
- Every code file, except the one containing main() should have a corresponding header file
- All functions and variables that are used outside code file should be declared (not defined) in the corresponding header file
- Initialize all variables especially pointers
- do not use global variables unless an API requires it
Stack Implementation
- Void pop function; will pop a pointer to an item off the stack and return it
- Void push the pointer to an item to item in the stack
- Int empty that returns 1 if empty and 0 if not
Key Points
- C has three levels of visibility: global, static, and local
- Global visibility should be limited to functions
- Organize programs into modules by using the static modifier to reduce visibility of private variables and types
- Use guards in header files to prevent multiple or circular inclusion of header files
- Hide type info by using void to define opaque types
- Makefiles and make are used to automate the build process
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore C programming conventions for managing code structure, including avoiding global variables and using the static modifier for module creation. Understand the levels of visibility in C: global, static, and local, and how they affect program organization. C relies on programmer conventions and idioms for encapsulation and abstraction.