Lecture 8 C Programming: Visibility & Conventions
41 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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.

False (B)

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.

<p>extern</p> Signup and view all the answers

Match the following:

<p>Global Variable = Visible to any other part of the program Static Variable = Visible inside the module where it is defined Local Variable = Visible within the function</p> Signup and view all the answers

What is the potential issue with allocating local variables inside a function?

<p>Local variables are allocated on the program stack, which can have limited size. (C)</p> Signup and view all the answers

Static variables inside a function are re-initialized each time the function is called

<p>False (B)</p> Signup and view all the answers

What value are static variables initialized to by default?

<p>0</p> Signup and view all the answers

In C structures can store several values of several ______ data types.

<p>different</p> Signup and view all the answers

Match struct with its usages:

<p>struct = store information of a single object</p> Signup and view all the answers

What is the main purpose of using the void * type in C?

<p>To create a generic pointer that can point to any data type. (B)</p> Signup and view all the answers

Header files should contain function definitions to ensure code reusability across multiple source files.

<p>False (B)</p> Signup and view all the answers

What is a typical naming convention for a header file, given a source file named "myModule.c"?

<p>myModule.h</p> Signup and view all the answers

A C module may use void * type to track ______ data.

<p>instance</p> Signup and view all the answers

Match the module component to its purpose:

<p>Code File = Contains global functions representing the public interface of the module Header File = Contains function declarations of the public interface</p> Signup and view all the answers

What issue do header guards primarily aim to prevent?

<p>Multiple definitions of the same structure or type. (D)</p> Signup and view all the answers

Header guards protect against circular dependencies between header files.

<p>True (A)</p> Signup and view all the answers

What preprocessor directives are commonly used to create header guards?

<p>#ifndef, #define, #endif</p> Signup and view all the answers

Makefiles are read by the ______ utility.

<p>make</p> Signup and view all the answers

Match the Makefile element with its purpose:

<p>Target = A file that is generated from the dependencies Dependencies = Zero or more files that, if modified, necessitate recompilation of the target Build Commands = Shell commands to be executed to build the target</p> Signup and view all the answers

Which of the following is NOT a typical benefit of using Makefiles?

<p>Automatic generation of source code. (B)</p> Signup and view all the answers

In a Makefile, a tab character must precede the build commands.

<p>True (A)</p> Signup and view all the answers

Other than a tab character, what character starts a comment in a Makefile?

<h1></h1> Signup and view all the answers

In a Makefile, macros are used to represent variables that store ______.

<p>text</p> Signup and view all the answers

Match the level of visibility with their description:

<p>Global = Definition visible to any other part of the program. Static = Definition only visible inside the module where it is defined. Local = Definition only visible within the function.</p> Signup and view all the answers

What is the purpose of function declarations in header files?

<p>To provide the compiler with the function signature for type checking. (D)</p> Signup and view all the answers

Using global variables is generally recommended in C programming to improve code maintainability.

<p>False (B)</p> Signup and view all the answers

What is the purpose of guards in header files?

<p>prevent multiple inclusion</p> Signup and view all the answers

The ______ operator can be used to dereference a pointer to a structure member.

<p>-&gt;</p> Signup and view all the answers

Connect file extensions to their purpose:

<p>.c = C source file .h = C header file .o = Object file</p> Signup and view all the answers

What is the consequence of not using header guards in a project with multiple source files?

<p>Linker errors due to multiple definitions. (C)</p> Signup and view all the answers

In C, nested function definitions are permitted.

<p>False (B)</p> Signup and view all the answers

What keyword in C declares a variable that can be accessed from multiple files?

<p>extern</p> Signup and view all the answers

In a Makefile, dependencies separated by spaces on the right side of the colon, represent files that, when changed, trigger ______.

<p>recompilation</p> Signup and view all the answers

Match the following terms related to C programming with their descriptions:

<p>Encapsulation = Bundling data and methods that operate on that data within a class Abstraction = Hiding complex implementation details and exposing only essential information Module = Collection of functions and data structures that perform a specific task</p> Signup and view all the answers

In C, what is the primary reason for using modules to organize code?

<p>To manage complexity and improve code maintainability. (A)</p> Signup and view all the answers

Using the static modifier in C guarantees thread safety for global variables.

<p>False (B)</p> Signup and view all the answers

How does using void * contribute to abstraction in C?

<p>hides implementation details</p> Signup and view all the answers

In a Makefile, a ______ is a file that is generated from dependencies.

<p>target</p> Signup and view all the answers

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?

<p>A cascade of 'redefinition' errors reported by the compiler, making it nearly impossible to pinpoint the root cause without specialized tools. (D)</p> Signup and view all the answers

Insanely Difficult: Describe a scenario where a C program must use global variables, despite the well-known risks, and briefly justify the exception.

<p>Handling signals in a real-time embedded system.</p> Signup and view all the answers

Flashcards

What are Global variables?

Variables that can be accessed by any function in the program.

Static modifier

A modifier. All functions are global unless they have a static modifier.

What is an external variable declaration?

Declared with the extern keyword to distinguish from a variable definition.

Local variables

Variables defined within a function body; exist during the function call.

Signup and view all the flashcards

What does the static keyword mean in this context?

Keyword that makes function or variable defined and visible only in file.

Signup and view all the flashcards

Structure in C

A construct that stores several values, possibly of different types.

Signup and view all the flashcards

What is a module?

A code file that provides specific functionality while hiding the implementation.

Signup and view all the flashcards

What does a C module contain?

Uses one or more global functions for the interface, with static elements being private.

Signup and view all the flashcards

Do modules have header files?

Header files (of the same name) that contain function declarations of the public interface and type definitions.

Signup and view all the flashcards

What are include guards?

Technique to ensure header files are only processed once by the preprocessor.

Signup and view all the flashcards

What is a Makefile?

A text file containing rules for how to build a program from its sources.

Signup and view all the flashcards

Target in a Makefile

A file that is generated from dependencies by a build command.

Signup and view all the flashcards

Dependencies (Makefile)

Files that, if modified, necessitate recompilation of the target.

Signup and view all the flashcards

Build Commands

Shell commands executed to build the target in a Makefile.

Signup and view all the flashcards

What does the make utility do?

Checks dependencies and their timestamps to determine what to rebuild.

Signup and view all the flashcards

What is the use of void *?

Pointer type that allows hiding implementation details and defining opaque types.

Signup and view all the flashcards

The C Commandments: Declaration Rule

The C rules: functions and variables used outside a code file should be declared in the corresponding header file.

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.

Quiz Team

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.

More Like This

Use Quizgecko on...
Browser
Browser