Introduction to C: Constants, Variables & keywords



We learned about the character set of C in our previous post, you should know about that moving forward.

When you learn the English language, you first are taught the English alphabet, then you formulate words from them, and once you start getting the idea and the meaning of words, you are taught sentences, their construction and orientation of words inside a sentence and the order in which they come, and with that, you start to learn the final product of your effort that is a paragraph.

The same is the case for learning any programming language eg. C.
From the character set, we shall learn about Constants, Variables & keywords then using these the instructions can be created after learning the "order" or in this case the syntax, and finally, you will get the final result in the form of a program!!!!!


Isn't this exciting!, Let's get started with Let's Constants, Variables & keywords

Constants: It is an entity or a type of entity that doesn't change

Variables: It is an entity that is "variable" in its value or it can change.

Keywords: In any programming language, keywords are words that hold a special meaning. 

To understand the difference between constants and variables while simultaneously better our understanding of them, let's take an example.

In C language when you store data inside memory, it stores it in memory cells.
These memory cells have names to make it easy to retrieve and change the data stored inside that memory cell and as the data might change, these "names" are variable names.

lets take x = 1

this x is the name of the memory location, as this name is assigned to that specific memory cell.
now by some computer algorithm or program or whatever, the value changes to x = 3;

the memory location named x can hold different values at different times and thus x is a variable and 1,3 do not change and thus are constants.
Constants:
There are two types and we shall focus on only the first which is primary constants, the second type which is secondary constants will be detailed and taught later.

Primary Constants:
There are three types and those are as follows

1. Integer Constant
2. Real Constant
3. Character Constant

Rules for integer constants:

To create an integer constant is to have at least one digit also it should not have a decimal point.
The number that is an integer constant can be positive or negative and without any sign specifier( + or -) it is automatically positive.
This constant isn't allowed commas and blanks.
range is from -2,147,483,648 to +2,147,483,647.

Rules for real constants:

To create a real constant is to have at least one digit and a decimal point.
A real constant can be positive or negative and without any sign specifier( + or -) it is automatically positive.
The real constant isn't allowed commas and blanks.
The exponential form is used to show the range and when the number is too large or too small, you get the idea right?

0.000453 can be written as 4.53e-4 or 4.53 * 10 ^ -4

range is  -3.4e38 to 3.4e38

Character constants are the ones that have a single alphabet or a single digit or a single special character that has single commas enclosing it.
 for example 'A', '1', '!'

Rules for variables:

A variable name is an alphanumeric word that is it is a combination of the 26 alphabets, integers, and underscores.
While it is good to have a long name in general when working on a big project as it provides the detail we should refrain from making it too long.

NOTE: The first character of a variable should be an alphabet or an underscore and no special symbol besides underscore(_) can be used in the variable naming convention.

eg. int num, int num_first, int _num.

Keywords in C:

C compiler has some set of rules and instructions to work upon, one of those said instructions is that of keywords.
Keywords are known as reserved words that have their meaning set inside of the C compiler and thus can't be used as variable names because it will be equal to assigning a new meaning to them.

There are 32 keywords in C and they are as follows.



Why can't we use keywords in naming a function or a variable?

These keywords cant be used in a general sense and the naming convention because it will confuse the compiler/interpreter about the sense in which we are using the said keyword.

Post a Comment

Previous Post Next Post