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-112Memory Concepts Arithm…
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 10 of 39
    Topic Study Note Illustration
    10

    Memory Concepts, Arithmetic in C, Operators

    In C programming, understanding memory management is crucial for writing efficient programs. Memory is used for storing data, program instructions, and other important information during the execution...

    Memory Concepts in C

    In C programming, understanding memory management is crucial for writing efficient programs. Memory is used for storing data, program instructions, and other important information during the execution of a program.

    Memory Layout in C

    Memory in C is divided into different sections, each serving a specific purpose. Here’s a general layout of how memory is organized in a C program:

    1. Stack:

      • The stack is used for storing local variables and function call information (such as function arguments and return addresses).
      • It works on a Last-In-First-Out (LIFO) principle, meaning the last function called is the first to return.
      • The stack grows and shrinks as functions are called and return.
    2. Heap:

      • The heap is used for dynamic memory allocation.
      • Variables stored in the heap remain there until explicitly freed using free() (for example, when using malloc() or calloc() to allocate memory).
      • This allows for memory that is flexible in size and duration.
    3. Data Segment:

      • This is where global and static variables are stored.
      • It is divided into two parts: initialized (where variables with initial values are stored) and uninitialized (for variables that are not initialized).
    4. Text Segment:

      • The text segment contains the program's executable code (machine code instructions).
      • It is typically read-only to prevent accidental modification of instructions during execution.

    Memory Management in C

    In C, the programmer has more control over memory compared to higher-level languages. This includes manual memory allocation and deallocation:

    • malloc(): Allocates a block of memory of a given size.
    • calloc(): Allocates memory for an array of a specified size and initializes all elements to zero.
    • free(): Deallocates memory that was previously allocated dynamically.

    Proper memory management is important to prevent memory leaks (not freeing memory) or segmentation faults (accessing unallocated memory).


    Arithmetic in C

    In C, arithmetic operations are performed using various operators. These include basic operations like addition, subtraction, multiplication, division, and modulus.

    Arithmetic Operators in C

    Here’s a list of the common arithmetic operators in C:

    Operator Description Example
    + Addition a + b
    - Subtraction a - b
    * Multiplication a * b
    / Division a / b
    % Modulus (remainder after division) a % b

    Examples of Arithmetic Operations:

    1. Addition: Adding two numbers:

      int result = 5 + 3;  // result will be 8
      
    2. Subtraction: Subtracting one number from another:

      int result = 10 - 4;  // result will be 6
      
    3. Multiplication: Multiplying two numbers:

      int result = 7 * 6;  // result will be 42
      
    4. Division: Dividing one number by another:

      int result = 8 / 4;  // result will be 2
      
    5. Modulus: Finding the remainder of a division:

      int result = 10 % 3;  // result will be 1 (remainder of 10 divided by 3)
      

    Division and Integer Division

    C performs integer division when both operands are integers. The result is an integer, and any decimal or fractional part is discarded.

    Example:

    int result = 7 / 3;  // result will be 2 (fractional part discarded)
    

    To perform floating-point division, at least one operand should be a float or double. For example:

    float result = 7.0 / 3;  // result will be 2.3333
    

    Operators in C

    C provides a wide range of operators that can be categorized into different types:

    1. Arithmetic Operators

    As discussed earlier, these operators perform basic mathematical operations on numeric values.

    Example:

    int a = 10, b = 20;
    int sum = a + b;        // sum = 30
    int difference = a - b; // difference = -10
    

    2. Relational Operators

    Relational operators are used to compare two values. They return either true (1) or false (0).

    Operator Description Example
    == Equal to a == b
    != Not equal to a != b
    > Greater than a > b
    < Less than a < b
    >= Greater than or equal to a >= b
    <= Less than or equal to a <= b

    Example:

    int a = 10, b = 5;
    if (a > b) {
        printf("a is greater than b\n");  // This will be printed
    }
    

    3. Logical Operators

    Logical operators are used to combine conditional statements.

    Operator Description Example
    && Logical AND a && b
    ` `
    ! Logical NOT !a

    Example:

    int a = 5, b = 10;
    if (a < b && b > 0) {
        printf("Both conditions are true\n");  // This will be printed
    }
    

    4. Assignment Operators

    Assignment operators are used to assign values to variables.

    Operator Description Example
    = Simple assignment a = b
    += Addition assignment a += b
    -= Subtraction assignment a -= b
    *= Multiplication assignment a *= b
    /= Division assignment a /= b
    %= Modulus assignment a %= b

    Example:

    int a = 10;
    a += 5;  // a = a + 5, so a becomes 15
    

    5. Increment and Decrement Operators

    The increment (++) and decrement (--) operators are used to increase or decrease a variable by 1.

    Operator Description Example
    ++ Increment by 1 a++ or ++a
    -- Decrement by 1 a-- or --a

    Example:

    int a = 5;
    a++;  // a becomes 6 (increment by 1)
    

    6. Bitwise Operators

    Bitwise operators operate on binary representations of integers. These operators allow for operations at the bit level.

    Operator Description Example
    & Bitwise AND a & b
    ` ` Bitwise OR
    ^ Bitwise XOR a ^ b
    ~ Bitwise NOT ~a
    << Left shift a << 2
    >> Right shift a >> 2

    Example:

    int a = 5, b = 3;
    int result = a & b;  // result will be 1 (binary AND of 5 and 3)
    

    7. Conditional (Ternary) Operator

    The conditional operator (?:) is a shorthand for an if-else statement.

    Syntax Description
    condition ? expr1 : expr2 If condition is true, expr1 is evaluated, otherwise expr2 is evaluated.

    Example:

    int a = 10;
    int result = (a > 5) ? 1 : 0;  // result will be 1 (since a > 5 is true)
    

    Summary

    • Memory Concepts: C allows you to manage memory manually through stack and heap, and provides mechanisms like malloc(), calloc(), and free() for dynamic memory management.
    • Arithmetic in C: Basic operations such as addition, subtraction, multiplication, division, and modulus are fundamental in C. Integer division discards the fractional part.
    • Operators: C provides several types of operators, including arithmetic, relational, logical, bitwise, and assignment operators to perform operations on variables.

    Understanding these memory concepts, arithmetic operations, and operators is essential for writing effective and efficient C programs.

    ← Previous topic 9
    Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer
    Next topic 11 →
    Decision Making: Equality and Relational Operators

    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.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals