C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Running the Program
  • Conclusion

Was this helpful?

  1. Basic Concepts

Program: Calculating the Area of a Triangle

To calculate the area of a triangle, you can use the formula:

[ \text{Area} = \frac{1}{2} \times \text{base} \times \text{height} ]

Let's create a simple C program that takes the base and height of a triangle as command line arguments and calculates the area.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    // Check if both base and height are provided
    if (argc == 3) {
        // Convert command line arguments to floating-point numbers
        float base = atof(argv[1]);
        float height = atof(argv[2]);

        // Check if the provided values are valid
        if (base > 0 && height > 0) {
            // Calculate the area of the triangle
            float area = 0.5 * base * height;

            // Display the result
            printf("Area of the triangle with base %.2f and height %.2f is: %.2f\n", base, height, area);
        } else {
            printf("Both base and height must be positive numbers.\n");
        }
    } else {
        printf("Usage: ./triangle_area <base> <height>\n");
    }

    return 0;
}

Running the Program

To run the program, compile it and provide the base and height as command line arguments.

./triangle_area 5.0 8.0

In the above command, triangle_area is the name of the compiled executable, and 5.0 and 8.0 are the base and height values, respectively.

Conclusion

This C program demonstrates how to calculate the area of a triangle using command line arguments. It checks if the required inputs are provided, validates the values, performs the calculation, and displays the result. Feel free to incorporate or modify this code according to your documentation needs.

In the upcoming sections, we'll continue exploring various topics in C programming. If you have specific questions or areas you'd like to delve into further, feel free to ask. Happy coding!

PreviousCommand Line ArgumentsNextOperators

Was this helpful?