Monday, April 28, 2025

C Tutorial - 4 - Constants and Comments

#include <stdio.h>

#define STATICNUMBER 500
#define PI 3.14

int main()
{  

    printf("Static Num: %d \n", STATICNUMBER);
    printf("PI: %f \n", PI);

    return 0;
}

This C code defines two constants using the #define preprocessor directive, then prints their values to the console using the printf() function.

The first line of the code includes the standard input/output library stdio.h.

The second line defines a constant named STATICNUMBER using the #define directive. It assigns the value 500 to STATICNUMBER, so wherever STATICNUMBER appears in the code, it will be replaced with the value 500 during compilation.

The third line defines a constant named PI using the #define directive. It assigns the value 3.14 to PI.

The main() function then prints the values of the two constants to the console using printf(). The first printf() statement prints the value of STATICNUMBER using the format specifier %d, which is used to format integer values. The second printf() statement prints the value of PI using the format specifier %f, which is used to format floating-point values.

When the program is executed, it will output the following text to the console:

Static Num: 500
PI: 3.140000

Note that the output for PI includes extra decimal places because the printf() function defaults to printing floating-point values with six decimal places.

 

What is "#define" preprocessor directive

#define is a preprocessor directive in the C programming language. It is used to define a macro, which is a piece of code that the compiler will substitute with the defined value before the program is compiled.

The syntax for #define is:

#define identifier value

Here, identifier is the name of the macro being defined, and value is the value that the macro will be replaced with.

For example, the following #define statement defines a macro called MAX_NUM with a value of 100:

#define MAX_NUM 100

When the compiler encounters MAX_NUM in the code, it will replace it with the value 100 before compiling the program.

#define can also be used to define functions-like macros, which can take arguments. Here's an example:

#define SQUARE(x) ((x) * (x))

This macro defines a function-like macro called SQUARE that takes a single argument x. When the compiler encounters a call to SQUARE(5), it will replace it with ((5) * (5)), which will evaluate to 25.

#define is a powerful tool for defining constants and macros in C programming. However, it is important to use it carefully, since macros can sometimes lead to unexpected behavior or errors if they are not used correctly.

 

Can i define constants inside main() function or outside

In C programming, constants can be defined both inside and outside the main() function using the const keyword or the #define preprocessor directive.

If you use the const keyword to define a constant inside the main() function, it will be a local constant and will only be accessible within the scope of the main() function. For example:

int main() {
    const int MAX_NUM = 100;
    printf("Max number is %d", MAX_NUM);
    return 0;
}

On the other hand, if you define a constant outside the main() function using either the const keyword or the #define preprocessor directive, it will be a global constant and can be accessed from anywhere in the program. For example:

#define MAX_NUM 100

int main() {
    printf("Max number is %d", MAX_NUM);
    return 0;
}

It is generally a good practice to define constants outside of the main() function, since it makes them more visible and accessible to the entire program.

 

Constants with "const" example

#include <stdio.h>

int main()
{
    const int staticNumber = 500;
    const float PI = 3.14;

    printf("Static Num: %d \n", staticNumber);
    printf("PI: %f \n", PI);

    return 0;
}

Result:

Static Num: 500
PI: 3.140000

Process returned 0 (0x0)   execution time : 0.020 s
Press any key to continue.

The above C code defines and initializes two constants, staticNumber and PI, using the const keyword.

The const keyword is used to define variables that cannot be modified or changed once they have been initialized. In other words, staticNumber and PI are read-only variables and any attempt to modify them later in the program will result in a compiler error.

The value of staticNumber is set to 500 and the value of PI is set to 3.14.

Then, the values of these constants are printed using the printf() function. The %d format specifier is used to print an integer value, and the %f format specifier is used to print a floating-point value.

Finally, the main() function returns 0, which indicates successful termination of the program.

The code defines and uses constants to store and print fixed values that are not supposed to change during program execution.

 

What are Comments in C programming and how to create them

Comments are explanatory statements in a program that are ignored by the compiler. They are used to make the code more readable and understandable to humans.

In C programming, there are two types of comments:

  1. Single-line comments: These comments start with two forward slashes (//) and end at the end of the line. They are used to comment on a single line of code.

    For example:

    // This is a single-line comment

     

  2. Multi-line comments: These comments start with a forward slash followed by an asterisk (/) and end with an asterisk followed by a forward slash (/). They can span multiple lines and are used to comment on multiple lines of code.

    For example:

    /* This is a 
       multi-line comment */
    

    Comments can be added to the code at any point, and they do not affect the execution of the program. They are simply ignored by the compiler.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...