using constants in C

in #constants7 years ago (edited)

In certain occasions, in C, or any other programming language, you may need to use variables which must mantain its value trough all the flow of your code. Lets take a look in a few ways you can do it in C.

Enums

Enums or enumerations, create a set of unsigned int constants with symbolic names representing each.


enum fruits {
    orange = 0,
    banana = 1,
    apple = 2,  

};

If you happen to not assign a value to any of these, it will be the value of the previous element +1:


enum fruits {
    orange = 0,
    banana = 10,
    apple, 
    grape = 27

};

img5.jpg

Since the banana element of the fruits enum is 10, apple then will be 11 if not assigned by you.

#

Instructions which begin with the # pragma, are executed at the pre-processing stage. The preprocessor manipulates text and code even before the source code of your program is compiled and sent to execution in memory(when it is actually running). You may remember of this directive from our first topic, when we included a C I/O library, which brought a few functions which we could use to perform input/output, like printf() function:


#include<stdio.h>

However, you have more options with the # directive, not limited to #include, like #define, #undef, #error or #line. Lets take a brief look at #define which can define a constant, so you may know better what you are working with, and then in a later tutorial, get in details about all of them:

#define

The #define directive will create a symbolic name, and assign values or even code to this name, before compilation. The values you assign to this symbolic name, cannot be changed during program execution:


#define twice 2;

We can now use this constant in our code, instead of a loose value:


int number = 26;
printf("My number is %d \n", number);
number = number * twice;
printf("My number now is %d \n", number);
return 0;

img1.jpg

Like previously told, you may create defined constats even to replace your own code. Look how neat:


#define msg printf("I dont have to write all this piece of code now\n");
int main() {
    msg;
    return 0;
}

img2.jpg

The #define directive may also generate macros(defined macros), replacing arguments for text:


#define multiply(x, y) x * y

img3.jpg

If you are coming from another language background, you may also use #define to make C commands more like your favorite language:


#define print printf 

img4.jpg

There, we got a new print command, with global scope, like python and perl, two of my favorite programming languages :)

Typedefs

As you could use #define to rename native C datatypes, you have the option of using the typedef mechanism. Its use is simple:


typedef char c;

Done! Now you can declare your new character type variables as:


c x = 37;

The typedef mechanism also has global scope, and its not limited to the scope of the code you are in(inside your brackets).
To define new pointers type names, you might be better with typedef instead of #define since #define mechanism can cause confusion in the declaration. For example:


#define c char *
c x, y;

Will declare only x to be a pointer, y will remain a normal char type. you may easily correct this, and not causing too much headches to the person reading your code, instead of hack a solution for it with #define:


typedef char * c;

img6.jpg

Constants

We have seen how to create constants with #define, but you may also create constant variables with const modifier:


const int x = 48;

Modifier const. Must be declared in the same line, and cannot be declared in one line and assigned in other.


const double mn = 1.618;
double const mn2 = 1.618;

You may declare any way you like. But assign a value to a const number in another line wont work:


const double mn;
mn = 1,618;

The const will respect the scope of the brackets within it was declared but #define will have a global scope. A const variable in certain situations will be more flexible to you in certain situations, as it can be used like a real variable, passing arguments to functions, casting to another type(cast is an operation which will change its type but not alter its value, altough it is limited to the value the memory will have acess in binary to do this type modification), using in pointers and such. However, deppeding of your project, #define may be the best solution, since you can use it anywhere a literal constat is allowed, like the example below:


#define msg printf("I dont have to write all this piece of code now\n");
#define size 5
int array[size]

Sort:  

I love that photo

Which one, andyx? :)

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 57495.98
ETH 2320.95
USDT 1.00
SBD 2.35