Express Learning Programming – Constants in C

Express Learning Programming – 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 programming concept we must know. It is about Constants in C language & what are they?, why we need them and also how to use them in C language. Let’s begin today’s lesson. First, we’ll see what the constants are.

What are the Constants?

Constants are something that store data value and that value doesn’t allow for change in the execution process. They are another type of variable. But they won’t allow 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.

Latest: Top 5 Linux Distros in 2023 – Let’s Learn about Linux

How to use Constants in C

In the GNU C programming language, we can use two ways to create these constant variables. Let’s see the first way 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 for using #define.

#define IDENTIFIER value

after putting the #define keyword, we need to give an identifier to our constant. And then, now we can put data value. Remember to use #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.

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

const datatype IDENTIFIER = Value;

First use the const keyword, after doing so. Now we need to a give specific datatype for it. and then we must give an IDENTIFIER or Variable_Name. Now put the “=” 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.

CATEGORIES
TAGS
Share This

COMMENTS

Wordpress (2)
  • comment-avatar
  • comment-avatar

    […] Previous lesson: Express Learning Programming – Constants in C […]

  • Disqus ( )