C Programming: Arrays

HilariousChalcedony8823 avatar
HilariousChalcedony8823
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is the default storage class of a variable if it is not explicitly specified?

auto

What is the scope of an 'auto' variable?

Limited to the block in which it is declared

What is the difference between 'auto' and 'extern' variables in terms of accessibility?

'auto' variables are local, while 'extern' variables are global

What is the purpose of the 'extern' keyword in C?

To declare a global variable or function in another file

What is the purpose of the 'static' keyword in C?

To retain the value of a variable between function calls or block executions

What is the default initial value of a 'static' variable?

Zero

What is the benefit of using the 'register' storage class?

Faster access to variables

Where are 'register' variables stored?

CPU registers

What is the purpose of using the 'auto' storage class?

To create local variables

What happens to an 'auto' variable when the control goes out of the block?

Access to it is destroyed

Study Notes

Array Initialization

  • An array must be initialized when declared, or the compiler will show an error "size of array is unknown or zero".
  • There are four ways to initialize one-dimensional arrays in C programming:
    • Initialize at the time of declaration, e.g., int a[] = {10, 20, 30, 40, 50};.
    • Initialize all elements with 0 (zero).
    • Initialize elements individually, e.g., int a; a = 10; a = 20; ....
    • Define the size of an array by initializing its elements.

Return Statement

  • The return statement terminates the execution of a function and returns control to the calling function.
  • It can also return a value to the calling function.
  • The point of functions is to take in inputs and return something.

Array Definition

  • An array is a collection of elements of the same type placed in contiguous memory locations.
  • Array elements can be individually referenced by using an index to a unique identifier.

Storage Classes in C

Auto Storage Class

  • The auto storage class is used for local variables.
  • Variables defined using auto are called local variables.
  • The scope of an auto variable is limited to the particular block only.
  • Auto variables contain a garbage value by default.

Extern Storage Class

  • The extern storage class is used for global variables and functions shared between two or more files.
  • Extern variables are accessible throughout the program.
  • Extern variables cannot be initialized, as they have already been defined in the original file.

Static Storage Class

  • Static variables are used within functions/files as local static variables or as global variables.
  • Static local variables retain their value between function calls or block and remain visible only to the function or block in which they are defined.
  • Static global variables are global variables visible only to the file in which they are declared.
  • Static variables have a default initial value of zero and are initialized only once in their lifetime.

Register Storage Class

  • The register storage class is used to store local variables within functions or blocks in CPU registers instead of RAM for quick access.

This quiz covers the basics of arrays in C programming, including declaration, initialization, and individual element assignment.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

C++ Arrays
12 questions

C++ Arrays

WillingSeaborgium avatar
WillingSeaborgium
Java Arrays Declaration and Initialization
6 questions
C++ Array Type
8 questions

C++ Array Type

DesirablePrudence1033 avatar
DesirablePrudence1033
Use Quizgecko on...
Browser
Browser