Constants and Variables
Hi,
This is a long post, be patient in digesting it. Here few aspects of C which are useful from campus interview preparation as well as for MCQ's of competitive exams. The section contains Rules for naming constants and variables along with some Test Yourself questions for checking your conceptual understanding.
The read time for this post is about 10mins.
After understanding this post you will be able to answer:
- Inventors of C.
- Why C?
- Arguments for learning C from programmer’s point.
- Constants and Variables in C along with rules to define them.
Acceptance of C:
C was found in 1972, by Dennis Ritche. After initial hesitation, C was widely accepted and appreciated programming language due to simplicity and ease of use. There is no harm in calling C as the universal programming language as whatever can be done on any other language is quite possible to do in C.
It has been almost 5 decades of domination by C, where many languages come up to lime light and faded away slowly but C stood the test of time.
Building blocks of a language. [Just for interview]
- Storing Data: i.e. how data is stored by the language in the system
- Data Operation: what are the operations that can be performed on stored data
- Data I/O: How Input and output operations are accomplished.
- Program Execution: Execution of execution of instruction in program.
C in Today’s Scenario:
- Cart before the Horse philosophy:
Most of the modern languages i.e. C++, JAVA etc are object oriented languages with complex concepts like class, objects, inheritance, polymorphism etc, comprehending them without learning C is equivalent to putting the Cart before your Horse. Thus, learning C before learning any other language makes sense.
- Fast and Reliable:
Major part of operating systems i.e. Linux, Unix, Windows are still written in C. Even today when it comes to speed of execution and real time programs, the C program is more efficient with respect to others. For connecting a new hardware to operating system, the drivers are written exclusively in C.
- Games:
The gaming industry is growing very fast along with the complexity of games is increasing and reaction time is decreasing. In this scenario, there is no language better than C which meets the expectations of player that too in 3D graphics environment.
- Android:
Today’s age is age of smartphones, loaded with lot android apps. The core of android OS written in C and rest is in Java, its shows that when it comes to core processing and performance C has edge over Java and for that matter any other programming language.
Learning C:
Learning any language is a step wise process. For example to learn English, you first learn alphabets, than, how to combine those alphabets to make words, followed by combining words to create a meaningful sentence and combining those sentences to create paragraphs.
To learn a programming language, you have to go through the same process, starting from learning alphabets/digits/special symbols we learn constants/variables/keywords which are combined to form instructions, which further combine to make a program.
Characters:
Any alphabet, digit or symbol used to represent some information is part of C Character set. These alphabets, digits and/or symbols combine to form Constants, Variables and Keywords.
Constants
In mathematics, the value of “pi” is 22/7 which is a constant. Similarly, speed of light i.e. 3x10^8 m/sec is constant. Similarly, a program can also have some constants whose value cannot be changed during the execution of program.
Types of Constants:
Constants can be divided broadly into 2 categories:
Primary Constants: int, char and real constants.
Secondary Constants: array, pointer, structure, union and enum.
Note: In this post presently on primary constants are explained, rules for constructing secondary constant will be updates later on.
Thumb Rules for Primary Constants
Integer Constants:
- Constant must have atleast 1 digit.
- Should be a non decimal.
- It can be positive or negative
- By default, the sign is taken as positive, negative sign is to be mentioned specifically.
- Commas and Blankspaces are not allowed in constants
- Range- Compiler dependent. For a 16-bit compiler, its -32768 to 32767.
Real (Floating Point) Constants:
Real constants can be represented in 2 ways i.e. Fractional Form and Exponential Form.
Rules for Fractional Form:
- Constant must have atleast 1 digit.
- Should be a non decimal.
- It can be positive or negative
- By default, the sign is taken as positive, negative sign is to be mentioned specifically.
- Commas and Blank spaces are not allowed in constants
Rules for Exponential Form:
- Mantissa and exponential form of the value should be separated by letter “e”.
- Mantissa part may have positive or negative sign, by default sign is positive.
- Exponent must have atleast one digit, which can be positive/negative, by default it is positive.
- Range in exponential form: -3.4e^38 to 3.4e^38.
Character Constants:
- A character constant is a single alphabet, digit or a special symbol enclosed within single inverted comma.
Note: Both inverted comma should point to the left.
For eg: ’A’ is valid character constant whereas ‘A’ is not.
- Max length of character constant is 1 char.
Test Yourself: Constant:
Find out correct constants from the list for a 16 bit compiler.
- 23, 4987, -7777, -65444, +-3232, -32769, 32768
- 32e, -3.4e^39, 3.5e^38, -3.3e^38, 3.4e^37
- ‘AB’, ’Z’, ’9’, ‘34’, ‘6’, ’Y’
Variables
Variables are the containers containing intermediate values used in calculation or logic implementation during the execution of program. More specifically, variable is the name assigned to a memory location. The value stored in this memory location is known as value of variable.
These memory locations can contain integer, float, constant, char values depending on the type of variable created. A integer type variable can hold only integer type of value, similarly a char stores a char and float stores a float value.
Thumb rules for Constructing Variables:
- Name should be combination of 1 to 31 alphabets, digits or underscores. Depending upon compilers, some compilers allow variable names upto 247 chars.
- Variable name should start with a character or underscore only. i.e. Variables name cannot start with digits.
- Commas and blanks are not allowed in variable name.
- Only underscore (“_”) is allowed as a symbol in variable name.
- Nomenclature rules remain same for both primary and secondary variables.
- Compiler differentiates different types of variables by making it compulsory to declare type of variable while constructing variable i.e. int, char, float etc.
As a programmer it is advisable to construct short and meaningful variable names as it increases program readability.
Test Yourself: Variables
Find out correct variable names.
- Alpha, beta, gamma,
- _alpha, __beta, ___gamma,
- 9_alpha, 10_beta, 0_gamma,
- Alpha9_, beta9, gam9ma_,
- @alpha, 9@beta, _gam@ma,
Thanks
Prateek
Enjoy Learning!!!
Answers:
Test Yourself [Correct answers are in bold]
Constants
- 23, 4987, -7777, -65444, +-3232, -32769, 32768
- 32e, -3.4e^39, 3.5e^38, -3.3e^38, 3.4e^37
- ‘AB’, ’Z’, ’9’, ‘34’, ‘6’, ’Y’
Variables
- Alpha, beta, gamma,
- _alpha, __beta, ___gamma,
- 9_alpha, 10_beta, 0_gamma,
- Alpha9_, beta9, gam9ma_,
- @alpha, 9@beta, _gam@ma,
Comments
Post a Comment