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›Test Driving C Application
    Programming FundamentalsTopic 8 of 39

    Test Driving C Application

    6 minread
    1,064words
    Intermediatelevel

    Test Driving a C Application

    "Test driving" in the context of software development typically refers to the process of writing and testing a program in an iterative manner, ensuring that the application works as expected at each stage of development. This process involves writing, compiling, running, debugging, and validating a C application in a step-by-step fashion.

    When it comes to test-driving a C application, the goal is to develop, run, and test the application iteratively and continuously. This allows the developer to catch issues early and ensure that the program works correctly in a real-world scenario.

    Let's walk through the process of test driving a simple C application.


    Steps Involved in Test Driving a C Application

    1. Writing the Code

    The first step in test-driving a C application is writing the code. You'll start by writing a C program, often starting with a simple function or a small piece of functionality, and then gradually add more functionality as the tests confirm that everything works as expected.

    Example C Program: Let’s assume we want to create a simple C program to calculate the sum of two numbers and test it through iterative development.

    #include <stdio.h>
    
    int sum(int a, int b) {
        return a + b;
    }
    
    int main() {
        int num1, num2;
        printf("Enter two integers: ");
        scanf("%d %d", &num1, &num2);
        
        int result = sum(num1, num2);
        printf("The sum of %d and %d is %d\n", num1, num2, result);
        
        return 0;
    }
    

    Here, we have a simple function sum() that adds two integers and returns the result. The main() function takes input from the user and displays the result.


    1. Compiling the Code

    Once the code is written, the next step is to compile it using a C compiler. The compiler will check for syntax errors and generate an object file (or an executable) that the operating system can run.

    • Command to compile using GCC (GNU Compiler Collection):
      gcc -o sum_program sum_program.c
      

    This command:

    • Compiles the source code in the file sum_program.c.
    • Produces an executable file named sum_program (or sum_program.exe on Windows).

    If there are any syntax errors, the compiler will display an error message and stop the compilation process. You'll need to fix the errors and compile again.


    1. Running the Program

    Once the program is compiled, you can run the executable to test its functionality.

    • Command to run the compiled program:
      ./sum_program
      

    When you run the program, the terminal will prompt you for input. For instance, if the program asks you to enter two integers, you might provide:

    Enter two integers: 5 10
    The sum of 5 and 10 is 15
    

    1. Test-Driven Development (TDD) Process

    In a more structured approach, test-driven development (TDD) involves writing tests before writing the actual code. However, in simple test-driving, you iteratively run the program and check the correctness of the output.

    Steps to Test-Drive the C Program

    1. Write a small piece of code (e.g., the sum() function).
    2. Compile and run the code to see if it works as expected.
    3. Test the functionality by providing sample inputs.
      • Example: Test with positive numbers (5 + 10), negative numbers (-3 + 4), and zeros (0 + 0).
    4. Debug and fix errors if the program doesn't behave as expected.
    5. Gradually add new features or functionality to the program, such as adding more functions for different operations, and repeat the above steps.
    6. Iterate: Continue testing and adding features until the application is complete.

    Example: Iterative Test-Driving

    First Iteration: Basic Addition

    #include <stdio.h>
    
    int sum(int a, int b) {
        return a + b;
    }
    
    int main() {
        int num1, num2;
        printf("Enter two integers: ");
        scanf("%d %d", &num1, &num2);
        
        int result = sum(num1, num2);
        printf("The sum of %d and %d is %d\n", num1, num2, result);
        
        return 0;
    }
    
    • Test: Input 5 and 10 → The program should output 15.
    • Fix if necessary: If there is an error in calculation, debug the sum() function.

    Second Iteration: Handling Negative Numbers

    Add functionality to handle negative numbers:

    int sum(int a, int b) {
        return a + b;
    }
    
    • Test: Input -3 and 4 → The program should output 1.
    • Fix if necessary: If the sum does not work with negative numbers, recheck the code.

    Third Iteration: Checking for Zero

    Add additional checks or test cases:

    • Test: Input 0 and 0 → The program should output 0.
    • Fix if necessary: If there is an issue with zero values, inspect the input-handling logic.

    Debugging During Test Driving

    If the program does not produce the expected output, debugging is necessary. Common debugging steps include:

    • Check input: Ensure that the correct input is being provided (e.g., using scanf() properly).
    • Print intermediate values: Use printf() to print variable values at different points in the program to understand where it goes wrong.

    Example:

    int sum(int a, int b) {
        printf("a: %d, b: %d\n", a, b);  // Debug print statement
        return a + b;
    }
    
    • Use a debugger: Tools like GDB (GNU Debugger) allow you to step through your code, examine variables, and set breakpoints to find logical errors.

    Finalizing the Application

    Once all test cases pass (i.e., the program produces the correct output for all input scenarios), you can finalize the C program by:

    • Cleaning up: Remove any unnecessary debug statements or test code.
    • Optimizing the code: Make sure the code is efficient and maintainable.
    • Adding comments: Include comments to explain the functionality of various parts of the program.
    • Compiling for release: If you're preparing the application for production, use optimization flags in the compiler to improve performance.

    For instance:

    gcc -O2 -o sum_program sum_program.c
    

    The -O2 flag optimizes the code for better performance.


    Summary of Test Driving a C Application

    1. Write the code in small, manageable increments.
    2. Compile and run the code to check for syntax errors and correctness.
    3. Iteratively test the program with different inputs, debugging as necessary.
    4. Gradually add new features and repeat the cycle of writing, testing, compiling, and debugging.
    5. Finalize by cleaning the code and optimizing it for production.

    The key to effective test-driving is incremental development and continuous testing, which helps identify errors early and ensures the program behaves as expected.

    Previous topic 7
    Typical C Program Development Environment, Role of Compiler and Linker
    Next topic 9
    Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer

    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 time6 min
      Word count1,064
      Code examples0
      DifficultyIntermediate