34 cards generated

Zapisz talię, zanim zniknie

Te fiszki nie są jeszcze zapisane — znikną, gdy wyjdziesz. Załóż darmowe konto, aby je zachować i odblokować wszystko poniżej.

Zapisz i ucz się
  • Save this deck to your account
  • Study with spaced repetition
  • Export to Anki (.apkg) or PDF
Lepsze, bardziej rozbudowane generacje
  • Process documents up to 100 pages
  • Images extracted from your PDFs
  • Sharper text extraction & a more advanced AI model
Sign up free → Free forever · No credit card

Flashcards in this deck (34)

Szukam...
  • What defines the scope of a variable in C?


    Scope refers to the visibility of a variable, determining where in the program it can be accessed.

    c programming scope
  • How long does a variable exist in memory defined as lifetime?


    Lifetime refers to how long a variable exists, either during function execution or for the entire duration of the program.

    c programming lifetime
  • What is the default storage class for local variables in C?


    The default storage class for local variables is auto.

    c programming storage_classes
  • What type of scope can a variable have?


    • Local scope: visible only within a function
    • Global scope: visible throughout the entire program
    c programming scope
  • What keyword is used for auto storage class in C?


    The keyword for auto storage class is auto.

    c programming auto
  • What are the four different storage classes in C?


    • auto
    • register
    • static
    • extern
    c programming storage_classes
  • What does the register storage class suggest?


    The register storage class suggests that the variable should be stored in CPU registers for faster access.

    c programming storage_classes
  • What does the static storage class do?


    The static storage class retains the variable's value between function calls.

    c programming storage_classes
  • What does visibility refer to in C programming?


    Visibility refers to which parts of a program can see and use a variable, which may exist in memory but not be accessible.

    c programming visibility
  • The default storage class for local variables in C is auto.

    c programming auto
  • In C, the four storage classes are: auto, register, static, extern.

    c programming storage_classes
  • What is the scope of an auto variable?


    Local to the function where it is declared.

    c variables storage_classes
  • What is the lifetime of an auto variable?


    Exists only while the function is executing.

    c variables storage_classes
  • What is the default value of an auto variable?


    Garbage value (not initialized automatically).

    c variables storage_classes
  • Where is the storage location of an auto variable?


    RAM (stack memory).

    c variables storage_classes
  • What is the purpose of the register storage class?


    To define local variables stored in a CPU register for faster access.

    c variables storage_classes
  • What is the maximum size of a register variable?


    Equal to the register size, usually one word.

    c variables storage_classes
  • Can you use the & operator on a register variable?


    No, because it does not have a memory address in RAM.

    c variables storage_classes
  • What happens if no register is available for a register variable?


    It will be stored in RAM like a normal variable.

    c variables storage_classes
  • What is the default value of a register variable?


    Garbage value (not initialized automatically).

    c variables storage_classes
  • What is the purpose of the register storage class in C?


    It is used for variables that are accessed very frequently, like loop counters in nested loops.

    c storage_classes
  • What does a static local variable do?


    It retains its value between function calls throughout the program's lifetime.

    c static local_variables
  • How does a global static variable differ from a regular global variable?


    Its scope is restricted to the file where it is declared, making it inaccessible from other files.

    c static global_variables
  • The static storage class allows local variables to retain their value between function calls. Local static variables retain their value even after the function ends.

    c static storage_classes
  • The use of static with global variables restricts their scope to the file in which they are declared.

    c static global_scope
  • The register storage class in C is recommended for variables that need to be accessed very frequently, such as loop counters.

    c register performance
  • What is the initialization value of the global static variable count?


    5

    c variables static
  • What happens to the local static variable i on subsequent calls to func()?


    It retains its previous value and gets incremented.

    c variables static
  • What would occur if i were a normal auto variable instead of static?


    i would be initialized to 5 each time, resulting in the same output for all iterations.

    c variables
  • What is the lifetime of a local static variable?


    It lasts for the entire program execution.

    c lifetime static
  • Where does a static variable's value get stored?


    In the data segment of memory, not the stack.

    c memory storage
  • What is a special property of static variables?


    They retain their value between function calls.

    c variables static
  • In the given program, how many times will func() be called?


    5 times, as long as count is greater than 0.

    c loops functions
  • The characteristics of static variables include: - Scope: Local to the function (for local static) or local to the file (for global static) - Lifetime: entire program execution - Default value: Zero - Storage location: Data segment of memory - Special property: Retains value between function calls

    c variables static
Notatki do nauki

1. Introduction to Storage Classes

A storage class defines the scope and lifetime of variables/functions in C programs. There are four storage classes: - auto - register - static - extern

Understanding these is crucial as they control the variable's memory and accessibility.

2. Important Terms

Before diving into storage classes, we need to understand: - Scope: Visibility of a variable in the program (local vs. global). - Lifetime: Duration a variable exists in memory (function execution vs. entire program). - Visibility: Which parts of the program can access the variable.

3. The auto Storage Class

The auto storage class applies to local variables by default. Variables declared without a storage class are considered auto.

Syntax Examples:

int mount; // auto by default
// or explicitly:
auto int month;

Characteristics of auto

  • Scope: Limited to the function where declared.
  • Lifetime: Exists during function execution.
  • Default Value: Not initialized (garbage value).
  • Storage Location: RAM (stack memory).

4. The register Storage Class

The register storage class suggests storing local variables in a CPU register, which enhances access speed.

Syntax Example:

register int miles;

Important Points: - Used for frequently accessed variables, like loop counters. - Cannot use the & operator due to lack of memory address.

Characteristics of register

  • Scope: Limited to the function where declared.
  • Lifetime: Exists during function execution.
  • Default Value: Not initialized (garbage value).
  • Storage Location: CPU register or RAM if no register available.

5. The static Storage Class

The static storage class preserves a variable's value between function calls. It can be applied to both local and global variables.

  • Local Static Variables: Retain value after function execution.
  • Global Static Variables: Scope restricted to the file of declaration.

Complete Example of static Storage

#include <stdio.h>
void func(void);
static int count = 5; // global static
int main() {
while(count--) {
func();
}
return 0;
}
void func(void) {
static int i = 5; // local static
i++;
printf("i is %d and count is %d\n", i, count);
}

Output:

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

Explanation: Both count and i are static. count decrements each loop iteration, while i retains its value across function calls.

Characteristics of static

  • Scope: Restricted (local static) or local to the file (global static).
  • Lifetime: Exists throughout program execution.
  • Default Value: Zero (automatically initialized).
  • Storage Location: Data segment of memory.