Understanding Constants in C

By CodesEasy
Updated January 09, 2026

When you're writing C programs, you'll often have values that shouldn't change while the program runs. Think of things like pi (3.14159...), the maximum size of an array, or a tax rate. These are constants—fixed values that stay the same from start to finish.

C gives you a couple different ways to define constants, and each has its place. Let me walk you through both.

Using the const Keyword

The most straightforward approach is declaring a variable with the const keyword. This tells the compiler "hey, this value is set in stone—don't let anything modify it."

Here's what that looks like:

const float pi = 3.14;
const int max_attempts = 5;

Once you've declared these, trying to change them later in your code will throw a compiler error. That's actually a good thing—it catches mistakes before they become bugs.

The syntax is simple: put const before the data type, give it a name, and assign the value right there. You have to assign it immediately because, well, you won't get another chance.

Symbolic Constants with #define

The other option is using #define, which works a bit differently. Instead of creating a variable, you're essentially telling the preprocessor to do a find-and-replace before your code even compiles.

#define PI 3.14
#define MAX_SIZE 100

Notice there's no equals sign and no semicolon. That trips people up at first.

What happens here is the preprocessor scans through your code before compilation and swaps out every instance of PI with 3.14. By the time the compiler sees your code, it's as if you typed 3.14 everywhere yourself.

You'll often see these written in all caps. That's just a convention—it helps other programmers (and future you) recognize that something is a constant at a glance.

Here's a slightly odd but useful example:

#define print printf

Now you can write print("Hello") instead of printf("Hello"). The preprocessor just substitutes one for the other. I wouldn't necessarily recommend this particular usage, but it shows how flexible #define can be.

Which Should You Use?

Both approaches work, but const variables are generally preferred in modern C code. They're type-safe (the compiler knows pi is a float), and they play nicer with debugging tools since they're actual variables with names.

#define still has its uses, especially for conditional compilation and macros, but for simple constants, const is usually the cleaner choice.

Quick Recap

Constants prevent accidental changes to values that should stay fixed. You can create them with const (which makes a read-only variable) or #define (which does text substitution before compilation). Either way, they make your code safer and more readable—anyone looking at your program knows immediately that MAX_USERS isn't going to change halfway through.

That's really all there is to it. Constants are a small concept, but using them well is one of those habits that separates clean code from messy code.

Comments (10)

Leave a Comment

Bryon Flippo

6 years ago

Have you already setup a fan page on Facebook ?

Vishnu

6 years ago

Yes, Check out https://www.facebook.com/codeseasy

Felecia Warsaw

6 years ago

Hi there, just became aware of your blog through Google, and found that it is really informative. I am gonna watch out for Brussels. I appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

Vergie Pokorney

6 years ago

I am interested in looking for more of such topics and would like to have further information. Hope to see the next blog soon.

Halina Braucks

6 years ago

Great post. I was checking continuously this blog and I’m impressed! Extremely helpful info particularly the last part I care for such information a lot. I was seeking this particular info for a very long time. Thank you and best of luck.

GuillerminaTodahl

7 years ago

Hi this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

furtdsolinopv

7 years ago

I was very pleased to seek out this net-site.I wished to thanks in your time for this excellent learn!! I definitely having fun with every little bit of it and I’ve you bookmarked to check out new stuff you weblog post.

Belveal

7 years ago

I added a new list. As you’ll see it’s bigger than most of them. I hope you all have had a great week!

Jeffery Feng

7 years ago

I just got done eating a platter of spaghetti before visiting your site. It sure makes the full feeling all that much better.

Donovan Hebner

7 years ago

Hey there! Someone in my Myspace group shared this website with us so I came to look it over. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Great blog and great design.

Vikas

8 years ago

usefull information