Featured - Constants in C

Hello everyone!, welcome back to the Think & Free. This is Another episode of Our Express Learning Programming series with GNU C Programming language. Today I’m going to talk about another basic of programming concept we must know. It is about Constants in C language & what are they?, why we need them and also how to use it in C language. Let’s begin today’s lesson. First, we’ll see what the constants are?.

What are the Constants?

Constants are something store data value and that value didn’t allow to change in the execution process. They are another type of variable. But they won’t allow doing changes in the execution process, that is the basic idea of what are the constants.

In the GNU C Programming language, we can use two different ways to use these Constants. Let’s see how it does.

How to use Constants in C

In the GNU C Programing language, we can use two ways to create these constant variables. Let’s see the first way how to create constants in the GNU C Programming language.

The first way is using the #define preprocessor directive. it’s used to define macros & it allows us to define Constants. Here is the syntax of using #define.

#define IDENTIFIER value

after putting the #define keyword, Now we need to give an identifier to our constant. And then, now we can put data value. Remember using #define to make constants, this way no need to config datatype. let’s see some examples.

#include <stdio.h>

#define MYNUM 16
#define MYSTRING "Hello, World"
#define MYCHAR 'A'
#define MYFLOAT 12.57

int main()
{
    //let's check value in MYNUM
    printf("MYNUM: %d\n", MYNUM);

    //let's check value in MYSTRING
    printf("MYSTRING: %s\n", MYSTRING);

    //let's check value in MYCHAR
    printf("MYCHAR: %c\n", MYCHAR);

    //let's check value in MYFLOAT
    printf("MYFLOAT: %f\n", MYFLOAT);

    return 0;
}

in this example program, I’m using the #define preprocessor directive and all basic data types. Now compile and run it. You can get an idea about how to make constants and how to use constants by using #define.

Ok, now let’s try the second way is, creating constants by using const keyword. let’s first see the syntax.

const datatype IDENTIFIER = Value;

First use const keyword, after doing that. Now we need a give specific datatype for it. and then we must give an IDENTIFIER or Variable_Name. Now put “=” symbol and then give a data value after putting “=”, for it. Don’t forget to give a semicolon finally. let’s check out some examples for, Get an idea.

#include <stdio.h>

int main()
{
    const int MYNUM = 16;
    const char MYSTRING[] = "Hello, World";
    const char MYCHAR = 'A';
    const float MYFLOAT = 12.57;

    //let's check value in MYNUM
    printf("MYNUM: %d\n", MYNUM);

    //let's check value in MYSTRING
    printf("MYSTRING: %s\n", MYSTRING);

    //let's check value in MYCHAR
    printf("MYCHAR: %c\n", MYCHAR);

    //let's check value in MYFLOAT
    printf("MYFLOAT: %f\n", MYFLOAT);

    return 0;
}

If you have a question or something else leave a comment. See you back soon with another Express Learning Programming With GNU C Programming Language lesson.

Leave a Reply

Your email address will not be published. Required fields are marked *