A simple, interactive C program to calculate the area of common geometric shapes. This tool is perfect for beginners learning about functions and conditional logic in C.
-
Circle Area: Calculates area using
$\pi r^2$ . -
Square Area: Calculates area using
$side^2$ . -
Rectangle Area: Calculates area using
$length \times breadth$ . - Input Validation: Basic check for menu selection.
You need a C compiler installed on your system (such as gcc, clang, or msvc).
- Clone the repository:
git clone github.com
A simple C program that takes a temperature input in Celsius and converts it to Fahrenheit using a modular function.
- Modular Design: Uses a separate function
temp()for calculation. - Precision: Uses
floatdata types to handle decimal values. - User-Friendly: Simple command-line interface for input and output.
- Requirement: Ensure you have a C compiler like GCC installed.
- Compile:
gcc main.c -o converter
- Execute:
./converter
A simple and efficient C program to calculate the average percentage of a student based on marks obtained in three subjects: Science, Maths, and Sanskrit.
This project demonstrates the use of:
- Functions: A dedicated
percentage()function to handle logic. - Floating Point Precision: Formatting output to 2 decimal places for accuracy.
- User Input: interactive data collection using
scanf.
The program assumes each subject is out of 100 marks. The formula used is:
Percentage = ((Science + Maths + Sanskrit) / 300) * 100
๐ How It Works
The program uses a simple iterative approach with three integer variables (b, c, and d) to calculate the next number in the sequence by adding the two preceding ones.
This program demonstrates the relationship between variables, their memory addresses, and pointer variables. It showcases how to:
- Assign an address to a pointer using the
&operator. - Access the value at a memory address using the
*operator (dereferencing). - Print memory addresses using the
%pformat specifier.
A fundamental exploration of memory addresses and dereferencing in C.
This repository contains a concise C program that demonstrates the relationship between a variable, its physical memory address, and a pointer. It highlights two different ways to access a value residing in memory.
- The Pointer (
*ptr): We store the memory address ofageinsideptr. By calling*ptr, we "dereference" it to see the value at that location. - The Address-Of Operator (
&): This retrieves the specific hex code representing where the variable lives on the stack. - The Result: Both print statements yield
22, proving that different paths can lead to the same memory data.
- Clone the repo:
git clone github.com - Compile:
gcc main.c -o pointer_demo - Execute:
./pointer_demo
This program demonstrates the concept of a pointer to a pointer (double pointer). It illustrates how memory can be accessed indirectly through multiple levels of referencing, a fundamental concept for managing dynamic arrays and complex data structures.
#include <stdio.h>int main() { int i = 5; // The base value int *ptr = &i; // Single pointer: Stores address of i int **pptr = &ptr; // Double pointer: Stores address of ptr
printf("value of i from pointer to pointer: %d\n", **pptr); return 0;
}
int i = 5;: A standard integer variable stored at a specific memory location.int *ptr = &i;: A single pointer that "points" to the address ofi.int **pptr = &ptr;: A double pointer (pointer to a pointer) that "points" to the address of the pointerptr.- Double Dereference (
**pptr): The first*retrieves the address stored inptr, and the second*retrieves the actual integer value5.
- Clone this repository.
- Compile using a standard C compiler:
gcc main.c -o double_pointer - Run the executable:
./double_pointer
This project demonstrates how to swap two integer values using a custom function. Unlike a standard "Call by Value" approach, this program uses pointers to directly manipulate memory addresses, ensuring the changes persist after the function call.
- Pass by Reference: We pass the addresses (
&aand&b) to theswapfunction. - Dereferencing: Inside the function, we use the
*operator to access and modify the actual values stored at those addresses. - Temporary Variable: A
tempvariable is used to hold one value so it isn't overwritten during the exchange.
- Ensure you have a C compiler installed (like GCC).
- Save the code as
swap.c. - Open your terminal and run:
gcc swap.c -o swap_demo && ./swap_demo - Modular Design: Calculations for sum and product are offloaded to dedicated functions for better readability.
- Pass-by-Reference: By passing addresses (e.g.,
&a), the functions use*ato access the original data without creating redundant copies in memory. - Precision: The average is calculated using
3.0to ensure floating-point precision, preventing integer truncation. - Clone this repository or copy the code into a file named
arithmetic.c. - Compile using a standard C compiler:
gcc arithmetic.c -o arithmetic - Run the program:
./arithmetic - Call by Value: A copy of the variable is passed. The original variable remains untouched outside the function.
- Call by Reference: The actual memory address is passed. Any changes made inside the function directly affect the original variable.
This program calculates the Sum, Product, and Average of three user-provided integers. It emphasizes the use of pointers (int*) to pass variables to modular functions, allowing for direct memory access and cleaner logic separation.
This program demonstrates how C handles function arguments. It provides a side-by-side comparison of two fundamental behaviors:
| Method | Terminal Output | Effect on Original Variable |
|---|---|---|
| Call by Value | square: 25 |
No Change (Variable remains 5) |
| Call by Reference | _square: 25 |
Permanent Change (Variable becomes 25) |
To run this demonstration in your local environment:
# Compile the program gcc -o comparison main.c
./comparison