ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    CC-112
    Progress0 / 39 topics
    Topics
    1. Introduction to Problem Solving, Algorithms, Programming, and C Language2. Problem Solving, a brief review of Von-Neumann Architecture3. The C Programming Language, Pseudo-code, Concept of Variable4. Data types in Pseudo-code, The C Standard Library and Open Source5. Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6. Concept of Integer division, Flowchart and its notations7. Typical C Program Development Environment, Role of Compiler and Linker8. Test Driving C Application9. Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer10. Memory Concepts, Arithmetic in C, Operators11. Decision Making: Equality and Relational Operators12. Structured Program Development: The if, if...else, while Nested Control Statements13. Program Control: for, switch, do...while, break, continue, Logical Operators14. Functions: Modularizing Program in C, Math Library Functions15. Function Definitions and Prototypes, Function-Call Stack and Stack Frames16. Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17. Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration18. Arrays: Defining Arrays, Character Arrays, Static and Automatic Local Arrays19. Passing Arrays to Function, Sorting and Searching Arrays20. Multidimensional and Variable Length Arrays21. Pointers: Pointer Definitions and Initialization, Pointer Operators22. Passing Arguments to Function by Reference, Using the const and sizeof Operator23. Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24. Function Pointers25. Characters and Strings: Strings and Characters, Character Handling Library26. String Functions, Library Functions27. Formatted Input/Output: Streams, Formatted Output with printf, Formatted Input with scanf28. Structures: Defining Structures, Accessing Structure Member, Structures and Functions29. typedef, Unions30. Bit Manipulation and Enumeration: Bitwise Operators, Bit Fields, Enumeration Constants31. File Processing: Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File32. Preprocessor: #include, #define, Conditional Compilation, #error and #pragma33. # and ## Operators, Predefined Symbolic Constants, Assertions34. Other Topics: Variable Length Argument List, Using Command Line Arguments35. Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36. Suffixes for Integer and Floating-Point Literals, Signal Handling37. Dynamic Memory Allocation: calloc and realloc, goto38. Advance Topics: Self-Referential Structures, Linked Lists39. Efficiency of Algorithms, Selection and Insertion Sort
    CC-112›Memory Concepts, Arithmetic in C, Operators
    Programming FundamentalsTopic 10 of 39

    Memory Concepts, Arithmetic in C, Operators

    7 minread
    1,117words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,117
      Code examples0
      DifficultyIntermediate