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-112Simple C Program Examp…
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
    5 min read
    Topic 9 of 39
    Topic Study Note Illustration
    09

    Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer

    C programming is one of the most widely used and foundational programming languages. Developed in the 1970s by Dennis Ritchie at Bell Labs, C has influenced many...

    Introduction to C Programming

    C programming is one of the most widely used and foundational programming languages. Developed in the 1970s by Dennis Ritchie at Bell Labs, C has influenced many other modern programming languages, including C++, Java, and Python. It's a general-purpose language that provides a good balance between high-level functionality and low-level memory manipulation.

    C is commonly used for system-level programming (e.g., operating systems, embedded systems) due to its close proximity to hardware and its efficient performance.


    A Simple C Program

    A simple C program typically starts with the inclusion of necessary header files, followed by the definition of a main() function. The main() function is the entry point of any C program, and it’s where the program starts its execution.

    Below are two very simple tasks often used in C programming for beginners:

    1. Printing Text to the screen.
    2. Adding Two Integers and printing the result.

    1. Printing Text in C

    Printing text or outputting information to the console is one of the most fundamental actions in any programming language. In C, you can use the printf() function to display text or values.

    Here’s an example of a C program that prints a message:

    #include <stdio.h>  // Preprocessor directive to include standard I/O library
    
    int main() {
        // Print a message to the screen
        printf("Hello, World!\n");
        return 0;  // Return 0 to indicate successful execution
    }
    

    Explanation:

    • #include <stdio.h>: This line includes the standard input/output library which contains the printf() function, used for printing text to the screen.
    • main() function: Every C program starts execution from the main() function.
    • printf() function: This function is used to print the output. The text "Hello, World!" is displayed on the screen. The \n at the end of the string is a newline character, which moves the cursor to the next line after printing.
    • return 0;: This line returns 0 from the main() function, indicating the program finished successfully.

    Output:

    Hello, World!
    

    2. Adding Two Integers

    Now let’s write a simple program to add two integers and print the result. In this program, we will:

    • Declare two integer variables.
    • Accept input for those variables from the user.
    • Perform addition and print the result.
    #include <stdio.h>  // Include the standard input/output library
    
    int main() {
        int num1, num2, sum;  // Declare three integer variables: num1, num2, and sum
        
        // Prompt the user for input
        printf("Enter first integer: ");
        scanf("%d", &num1);  // Read the first integer from the user
    
        printf("Enter second integer: ");
        scanf("%d", &num2);  // Read the second integer from the user
        
        sum = num1 + num2;  // Add the two integers and store the result in 'sum'
        
        // Output the result
        printf("The sum of %d and %d is %d\n", num1, num2, sum);
        
        return 0;  // Indicate that the program has ended successfully
    }
    

    Explanation:

    • int num1, num2, sum;: Declares three integer variables. num1 and num2 will store the two input integers, and sum will store their sum.
    • scanf("%d", &num1);: This function reads the user's input and stores it in the variable num1. %d is the format specifier for integers.
    • sum = num1 + num2;: This performs the addition of the two integers and stores the result in the sum variable.
    • printf("The sum of %d and %d is %d\n", num1, num2, sum);: This prints the result of the addition using the printf() function. The format specifiers %d are replaced with the values of num1, num2, and sum.

    Example Output:

    Enter first integer: 5
    Enter second integer: 10
    The sum of 5 and 10 is 15
    

    How It Works Step-by-Step:

    1. The program starts and displays a prompt for the user to enter two integers.
    2. The program uses the scanf() function to accept input from the user and stores the values in num1 and num2.
    3. The program calculates the sum of the two integers and stores it in the sum variable.
    4. Finally, it prints the sum using the printf() function, displaying the result to the user.

    Summary of Key Concepts in the Program:

    1. Preprocessor Directives: #include <stdio.h> includes the standard input/output library necessary for using printf() and scanf().
    2. Variables: We declared integer variables (num1, num2, and sum) to hold the values.
    3. Input/Output: scanf() reads input from the user, and printf() displays output to the user.
    4. Arithmetic: We performed a simple arithmetic operation (+) to add the two integers.
    5. Return Statement: return 0; signals the successful completion of the program.

    This is the foundation of C programming — working with input, output, and arithmetic operations to create simple programs.

    ← Previous topic 8
    Test Driving C Application
    Next topic 10 →
    Memory Concepts, Arithmetic in C, 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%
    5mremaining
    804words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals