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›Input/Output, Arithmetic expressions, Assignment statement, Operator precedence
    Programming FundamentalsTopic 5 of 39

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

    7 minread
    1,266words
    Intermediatelevel

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

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