what is a integer constant in c?

Viewed 671

I am a beginner and reading C primer plus. There's a place I can't understand. I know that constant can't be changed throughout the program, and when I read this:

Initializing a Variable:
...
int hogs = 21;
int cows = 32, goats =14;
int dogs, cats = 94;
...
The various integers(21, 32, 14, and 94) are integer constants. When you write a number without a decimal point and without an exponent, C recognizes it as an integer.

I was confused about what he's saying the integer constant and integer. I thought maybe he said that when you write this "int hogs = 21;", you are declaring the hogs as a constant. So I coded myself to check whether "int hogs = 21;" is or not to declare a constant:

int main()
{
   int words = 3020, lines = 350;

   printf("there were %d words and %d lines\n", words, lines);

   words = 2;
   lines = 1;
   printf("change %d and %d", words, lines);

    return 0;

}

It can be changed! And then I searched online, I knew that you should write this "const int hogs = 21;" to declare a constant. But what is the integer constant? what is "The various integers(21, 32, 14, and 94) are integer constants. When you write a number without a decimal point and without an exponent, C recognizes it as an integer.". It's already an integer because you have coded "int hogs =21;". I think maybe he said that if you don't declare the variable and use a number directly, like 10, the compiler will recognize the number as a integer variable. And 10 is what he said the 'integer constant'. So is that right?

3 Answers

By the statement int hogs = 21 is a constant, the author simply means that an integer variable 'hogs' has been declared and it has been initialized with a constant value 21.

Similarly with "The various integers(21, 32, 14, and 94) are integer constants" statement - author means that the other integer variables have been initialized with constant values 32, 14 and 94 respectively.

Your understanding of the const keyword is absolutely right, the const keyword is used to declare a constant integer variable - the variable which can be declared only once and whose value does not change.

  • 21 is an integer constant with value 21 and type int.

  • 20 + 1 is an expression consisting of integer constants 20 and 1 and operator +, and is an integer constant expression, with value 21 and type int. (21 by itself is also an integer constant expression.)

  • int bogs; declares (and possibly defines) bogs as an object of type int. These named objects are also called variables.

  • int hogs = 21; declares and defines hogs as an object (variable) of type int and initializes it with the value of the integer constant 21. I.e. the value 21 will be initially stored into it. The programmer can change it thereafter by assigning a new value to it.

  • const int cogs = 21 declares and defines cogs as an object (variable) of type const int and initializes it with the value of the integer constant 21. const as a type of an object signifies that the programmer intends not to change the value of the object (variable) after initialization.

    However in C, cogs is not an integer constant, nor it is an integer constant expression, it is just a variable(!) of type const int. The distinction is important, because there are many places that require an integer constant expression and in those cases you cannot use the value of any object at all, not even cogs: for example the labels of switch - case, dimensions of arrays with static storage duration, widths of bitfields etc...

The problem here is a bit of confusion in the author's speech, between what is considered an integer (the type, as a set of values taken from the mathematical set of the whole numbers), an integer constant (which I guess you interpreted as a kind of object that is not allowed to be changed, or a named number), and an integer literal (which is one of the values of the set of whole number, like 23)

The author mixes the terms and so, you are confused. I'll try to explain:

  • An integer literal is the value you use to initialize or to assign a concrete integer value. An example of this is 23.
  • An integer constant is something you cannot modify, but has a name in order to refer to it (a good example, being it an integer, should be the constant PI, but it is not a good example of an integer ;) Let's think of a fixed array of N elements, in which you want to decide in the last time, when you are going to give it a value, but the value doesn't change during the program. You have two ways of defining a constant value:
#define N 1024

In this case, you make the C preprocessor to always that it sees the token N, to change it by its value, so it is actually a constant, as if you try:

    N = 1023;

the c preprocessor will expand this into:

    1024 = 1023;

and you'll get a compilation error, due to an invalid statement (you cannot assign a different value to a number literal)

  • The other way is the recent way of doing this:
    const int N = 1024;

In this case, the compiler will create a variable (bad name for a constant ;) ) and you can refer to it in any program statement, but the compiler will not allow you to modify it. If you try something like

    N = 1023;

You will get an error, telling you that N is a constant object and so, you cannot modify it.

It's funny that every author treats string literals as some text between quotes, but they don't do the same with integer literals or floating point literals.

Related