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-112Io Arithmetic Expressi…
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 5 of 39
    Topic Study Note Illustration
    05

    Input/Output, Arithmetic expressions, Assignment statement, Operator precedence

    In C programming, input and output (I/O) are fundamental for interacting with the user and displaying results. The standard library provides several functions for input and output...

    Input/Output in C

    In C programming, input and output (I/O) are fundamental for interacting with the user and displaying results. The standard library provides several functions for input and output operations. The most common ones are printf() for output and scanf() for input.

    1. Output using printf()

    The printf() function is used to print data to the screen (standard output). It can print variables, literals, and formatted text.

    Syntax:

    printf("format string", arguments);
    
    • format string: A string that specifies how the output should be formatted.
    • arguments: The values to be printed.

    Example:

    #include <stdio.h>
    
    int main() {
        int age = 25;
        float height = 5.9;
        printf("Age: %d, Height: %.1f\n", age, height);
        return 0;
    }
    

    Here:

    • %d is used to print integers.
    • %.1f is used to print floating-point numbers with one decimal place.

    2. Input using scanf()

    The scanf() function is used to take input from the user. It reads formatted data from the standard input (keyboard).

    Syntax:

    scanf("format string", &variable);
    
    • format string: A string that specifies the type of data expected.
    • &variable: The memory address of the variable where the input will be stored (the & symbol is used for this purpose).

    Example:

    #include <stdio.h>
    
    int main() {
        int num;
        printf("Enter an integer: ");
        scanf("%d", &num); // Reading an integer input
        printf("You entered: %d\n", num);
        return 0;
    }
    

    Here:

    • %d is used to read an integer.

    Note: Always ensure that the correct format specifier is used when reading inputs. For instance, %f is used for floating-point numbers, %c for characters, and %s for strings.


    Arithmetic Expressions in C

    An arithmetic expression in C is a combination of constants, variables, operators, and parentheses that computes a value. The arithmetic operators in C include addition, subtraction, multiplication, division, and modulus.

    Basic Arithmetic Operators

    1. Addition (+): Adds two operands.
      int sum = a + b;
      
    2. Subtraction (-): Subtracts the second operand from the first.
      int difference = a - b;
      
    3. Multiplication (*): Multiplies two operands.
      int product = a * b;
      
    4. Division (/)
      • Divides the first operand by the second operand.
      • If both operands are integers, the result will be an integer (fractional part is discarded).
      int quotient = a / b;
      
    5. Modulus (%): Returns the remainder when the first operand is divided by the second operand.
      int remainder = a % b;
      

    Example of Arithmetic Expression

    #include <stdio.h>
    
    int main() {
        int a = 20, b = 10;
        int result;
        result = (a + b) * (a - b); // Arithmetic expression
        printf("Result: %d\n", result);
        return 0;
    }
    

    In this example, the arithmetic expression (a + b) * (a - b) involves addition, subtraction, and multiplication.


    Assignment Statement in C

    An assignment statement in C is used to assign a value to a variable. The assignment operator is =.

    Syntax of Assignment Statement:

    variable = expression;
    
    • variable: The variable to which a value is assigned.
    • expression: The value or calculation that will be assigned to the variable.

    Example of Assignment Statement

    #include <stdio.h>
    
    int main() {
        int x = 10;    // Assigning value 10 to variable x
        int y;
        y = x + 5;     // Assigning the result of x + 5 to y
        printf("x = %d, y = %d\n", x, y);
        return 0;
    }
    

    In this example:

    • x = 10 assigns 10 to x.
    • y = x + 5 calculates x + 5 and assigns the result (15) to y.

    Compound Assignment Operators

    C also provides compound assignment operators, which are shorthand for operations that involve an assignment. For example:

    • += adds a value to the variable.
    • -= subtracts a value from the variable.
    • *= multiplies the variable by a value.
    • /= divides the variable by a value.
    • %= takes the modulus and assigns the result.

    Example:

    #include <stdio.h>
    
    int main() {
        int a = 5;
        a += 3; // Equivalent to a = a + 3
        printf("a = %d\n", a);
        return 0;
    }
    

    Here, a += 3 increases a by 3, so the result will be a = 8.


    Operator Precedence in C

    Operator precedence defines the order in which operators are evaluated in an expression. In C, some operators have higher precedence than others, meaning they are evaluated first. Parentheses () are used to explicitly specify the order of evaluation.

    Operator Precedence (Highest to Lowest)

    1. Parentheses (): Used to group expressions and alter the natural precedence.

      int result = (a + b) * c;
      
    2. Unary Operators: Operators that work on a single operand, such as increment (++), decrement (--), and logical NOT (!).

      int x = 5;
      x++;   // Increment x
      
    3. Multiplication, Division, and Modulus (*, /, %): These operators have higher precedence than addition and subtraction.

      int result = a * b + c;  // Multiplies first, then adds
      
    4. Addition and Subtraction (+, -): These have lower precedence than multiplication, division, and modulus.

      int result = a + b - c;  // Adds a and b first, then subtracts c
      
    5. Relational Operators (<, >, <=, >=, ==, !=): These are used for comparisons and have lower precedence than arithmetic operators.

      if (a > b) { // Compares a and b
          printf("a is greater than b");
      }
      
    6. Logical Operators (&&, ||): These operators have the lowest precedence among the common operators.

      if (a > b && b > c) { // Logical AND operator
          printf("Condition is true");
      }
      

    Example of Operator Precedence

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10, c = 2;
        int result = a + b * c;  // Multiplication happens before addition
        printf("Result: %d\n", result);  // Output will be 25 (10 * 2 + 5)
        return 0;
    }
    

    In this example, b * c is evaluated first due to its higher precedence, and then a is added to the result.

    Parentheses to Control Precedence

    To alter the natural order, parentheses can be used to explicitly specify the desired order of operations:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10, c = 2;
        int result = (a + b) * c;  // Addition happens before multiplication
        printf("Result: %d\n", result);  // Output will be 30 ((5 + 10) * 2)
        return 0;
    }
    

    Summary

    • Input/Output: In C, printf() is used to display output, and scanf() is used to read input.
    • Arithmetic Expressions: These are combinations of numbers, variables, and operators used to calculate a value. Common operators include +, -, *, /, and %.
    • Assignment Statement: The = operator is used to assign values to variables, and compound assignment operators like +=, -=, etc., can be used for shorthand.
    • Operator Precedence: Operators have a defined precedence that dictates the order of evaluation. Arithmetic operators like multiplication and division have higher precedence than addition and subtraction. Parentheses can be used to explicitly define the order of operations.

    Understanding these concepts is crucial for writing clear and efficient programs in C.

    ← Previous topic 4
    Data types in Pseudo-code, The C Standard Library and Open Source
    Next topic 6 →
    Concept of Integer division, Flowchart and its notations

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

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals