Skip to content

C_Constants.c

ani9977 edited this page Sep 11, 2024 · 1 revision

Here's the updated code with comments and explanations:

#include <stdio.h>  // Includes the standard input-output library

#define PI 3.14159  // Defines a constant PI with the value 3.14159

int main() {  // Main function where the program starts executing
    printf("PI value: %f\n", PI);  // Prints the value of PI using the %f format specifier for floating-point numbers
    return 0;  // Returns 0, indicating that the program has run successfully
}

Explanation:

  • #include <stdio.h>: This line includes the standard input-output library which allows the use of functions like printf.
  • #define PI 3.14159: This is a preprocessor directive that defines a constant PI with a value of 3.14159. The #define keyword allows you to create symbolic constants in C.
  • int main(): The main function is the starting point of the program.
  • printf("PI value: %f\n", PI);: The printf function prints the value of PI to the console. The %f format specifier is used to display floating-point numbers.
  • return 0;: This indicates that the program executed successfully.

Using #define to define constants like PI allows for more readable and maintainable code, as it avoids magic numbers (hardcoded values) scattered throughout the program.

Clone this wiki locally