ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-112Arrays Character Array…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-112

    Programming Fundamentals

    Progress0 / 39 done
    Topics39
    1Introduction to Problem Solving, Algorithms, Programming, and C Language2Problem Solving, a brief review of Von-Neumann Architecture3The C Programming Language, Pseudo-code, Concept of Variable4Data types in Pseudo-code, The C Standard Library and Open Source5Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6Concept of Integer division, Flowchart and its notations7Typical C Program Development Environment, Role of Compiler and Linker8Test Driving C Application
    9A Simple C Program: Printing Text, Adding Two Integer10Memory Concepts, Arithmetic in C, Operators
    11Equality and Relational Operators
    12The if, if...else, while Nested Control Statements
    13for, switch, do...while, break, continue, Logical Operators
    14Modularizing Program in C, Math Library Functions15Function Definitions and Prototypes, Function-Call Stack and Stack Frames16Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration
    18Defining Arrays, Character Arrays, Static and Automatic Local Arrays19Passing Arrays to Function, Sorting and Searching Arrays20Multidimensional and Variable Length Arrays
    21Pointer Definitions and Initialization, Pointer Operators22Passing Arguments to Function by Reference, Using the const and sizeof Operator23Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24Function Pointers
    25Strings and Characters, Character Handling Library26String Functions, Library Functions
    27Streams, Formatted Output with printf, Formatted Input with scanf
    28Defining Structures, Accessing Structure Member, Structures and Functions29typedef, Unions
    30Bitwise Operators, Bit Fields, Enumeration Constants
    31Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File
    32#include, #define, Conditional Compilation, #error and #pragma33# and ## Operators, Predefined Symbolic Constants, Assertions
    34Variable Length Argument List, Using Command Line Arguments35Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36Suffixes for Integer and Floating-Point Literals, Signal Handling
    37calloc and realloc, goto
    38Self-Referential Structures, Linked Lists39Efficiency of Algorithms, Selection and Insertion Sort
    7 min read
    Topic 18 of 39
    Topic Study Note Illustration
    18

    Arrays: Defining Arrays, Character Arrays, Static and Automatic Local Arrays

    In C, arrays are a fundamental data structure used to store multiple values of the same type in a contiguous memory block. They allow for easy access...

    Arrays in C: Defining Arrays, Character Arrays, Static and Automatic Local Arrays

    In C, arrays are a fundamental data structure used to store multiple values of the same type in a contiguous memory block. They allow for easy access and manipulation of a collection of related data. Understanding how arrays work and the different types of arrays, such as character arrays and static vs automatic local arrays, is essential for efficient programming in C.

    Let’s break down the key concepts of arrays in C.


    1. Defining Arrays in C

    An array in C is defined by specifying the type of elements it will store, followed by the name of the array, and the number of elements it can hold (its size).

    Syntax:

    type arrayName[arraySize];
    
    • type: The data type of the elements (e.g., int, float, char).
    • arrayName: The name of the array.
    • arraySize: The number of elements the array can hold (the size of the array).

    Example: Defining an Integer Array

    #include <stdio.h>
    
    int main() {
        // Define an array of 5 integers
        int numbers[5] = {1, 2, 3, 4, 5};
    
        // Access and print array elements
        for (int i = 0; i < 5; i++) {
            printf("numbers[%d] = %d\n", i, numbers[i]);
        }
    
        return 0;
    }
    

    Explanation:

    • An array numbers of size 5 is defined.
    • The elements of the array are initialized with values {1, 2, 3, 4, 5}.
    • We then loop through the array to print each element.

    Output:

    numbers[0] = 1
    numbers[1] = 2
    numbers[2] = 3
    numbers[3] = 4
    numbers[4] = 5
    

    2. Character Arrays

    In C, a character array is a special kind of array used to store strings. Since strings in C are represented as arrays of characters, a character array is used to hold a sequence of characters, with the last character always being a null terminator ('\0').

    Defining a Character Array:

    A string can be defined by initializing a character array with a string literal, which is automatically terminated by a null character.

    Syntax:

    char stringName[arraySize];
    

    Alternatively, you can define the array as:

    char stringName[] = "Hello";
    
    • In this case, the compiler will automatically determine the array size based on the length of the string, including the null terminator.

    Example: Defining and Printing a String Using a Character Array

    #include <stdio.h>
    
    int main() {
        // Define a character array (string)
        char name[] = "John";
    
        // Print the string
        printf("Hello, %s!\n", name);
    
        return 0;
    }
    

    Explanation:

    • The string "John" is stored in a character array name. In memory, it will look like this: {'J', 'o', 'h', 'n', '\0'}.
    • The %s format specifier is used in printf to print the string (i.e., the characters up to the null terminator).

    Output:

    Hello, John!
    

    Important Note:

    • If you define a character array large enough to hold a string, make sure it is always null-terminated, as C relies on the null terminator to identify the end of a string. For example, a string "Hello" requires an array of at least 6 characters (5 characters for "Hello" and 1 for '\0').

    3. Static and Automatic Local Arrays

    In C, arrays can be declared as static or automatic local arrays, which determines their lifetime, memory allocation, and scope.

    Automatic Local Arrays:

    • Automatic arrays are the default type of local arrays in C. These arrays are created when the function they are declared in is called and are destroyed when the function exits.
    • They are stored on the stack and have a limited scope (accessible only within the function or block where they are declared).
    Key Characteristics:
    • Memory is automatically allocated and deallocated when the function is called and returns.
    • The values stored in the array are not retained between function calls (they are not persistent).
    Example: Automatic Local Array
    #include <stdio.h>
    
    void printNumbers() {
        int numbers[3] = {1, 2, 3};  // Automatic array
    
        for (int i = 0; i < 3; i++) {
            printf("numbers[%d] = %d\n", i, numbers[i]);
        }
    }
    
    int main() {
        printNumbers();  // Call function with automatic array
    
        return 0;
    }
    

    Static Local Arrays:

    • A static array in C retains its value even after the function returns. It is initialized only once and persists across function calls. Static arrays are stored in the data segment of memory, not on the stack.
    • Static arrays are declared using the static keyword.
    Key Characteristics:
    • Memory is allocated once when the program starts and is released when the program ends.
    • The values stored in a static array persist between function calls, unlike automatic arrays.
    Example: Static Local Array
    #include <stdio.h>
    
    void printNumbers() {
        static int numbers[3] = {1, 2, 3};  // Static array
    
        // Modify the array value
        numbers[0] += 10;
    
        for (int i = 0; i < 3; i++) {
            printf("numbers[%d] = %d\n", i, numbers[i]);
        }
    }
    
    int main() {
        printNumbers();  // First call
        printNumbers();  // Second call
    
        return 0;
    }
    

    Explanation:

    • The first time printNumbers() is called, the array is initialized with {1, 2, 3}.
    • The second time printNumbers() is called, the array retains its previous state (numbers[0] = 11), because it is a static array.
    • The array's values are retained between function calls.

    Output:

    numbers[0] = 11
    numbers[1] = 2
    numbers[2] = 3
    numbers[0] = 11
    numbers[1] = 2
    numbers[2] = 3
    

    Comparison Between Static and Automatic Local Arrays

    Feature Automatic Local Arrays Static Local Arrays
    Memory Allocation Allocated and deallocated when the function is called and returns. Allocated once when the program starts and persists throughout the program.
    Lifetime Only exists while the function is executing. Exists for the lifetime of the program.
    Initialization Initialized every time the function is called. Initialized only once, the first time the function is called.
    Persistence Between Calls Values are lost after the function exits. Values persist between function calls.

    4. Summary of Key Concepts

    1. Defining Arrays:

      • Arrays are defined by specifying the data type, name, and size.
      • Arrays store multiple values of the same data type in contiguous memory locations.
    2. Character Arrays:

      • Character arrays are used to store strings.
      • Strings are arrays of characters that are null-terminated ('\0').
    3. Automatic Local Arrays:

      • These arrays are created when a function is called and destroyed when it returns.
      • The values are not retained between function calls.
    4. Static Local Arrays:

      • Static arrays retain their values between function calls and persist throughout the program's lifetime.
      • They are initialized only once and can be accessed across multiple calls to the function.

    Understanding these array types helps manage memory effectively and allows for better organization of data in your programs.

    ← Previous topic 17
    Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration
    Next topic 19 →
    Passing Arrays to Function, Sorting and Searching Arrays

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals