Keyword, Identifier, Constants - TGN

Latest

Welcome to Technicalgurunow! Your No. 1 source for all networking , earning and programming tutorials. We're dedicated to providing you the best of knowledgeable information with a focus on dependability. We hope you enjoy our blog.

Tuesday, December 3, 2019

Keyword, Identifier, Constants

               Keyword, Identifier, Constants

  • Keyword :
    •  In designing a compiler (eg. C) in a programming language, some words are reserved for specific tasks. These words are called keywords or reserve words. Each keyword has a definite meaning and this meaning cannot be changed. Along with this, another important thing to note is that all keywords should be written in lowercase. If this is not done then error will come in the program.
    • In simple terms, the words which mean "C" compiler have already been explained, are called keywords.
    • The following keywords or reserve words are used in the C language.
    • The given reserve words are declared as standard words to perform some predetermined function in the C compiler. For example, "break" is a keyword and cannot be used as a variable.

In designing a compiler (eg. C) in a programming language, some words are reserved for specific tasks. These words are called keywords or reserve words.

  • Identifier : Identifiers are names that you give to variables, constants and functions etc. There are some rules for giving these names that you follow. If you do not follow these rules, then there is an error in the program.
    1. @,%, - characters cannot be used in identifiers.
    2. C is case sensitive language. Therefore, age and age will be called two different identifiers.
    3. You cannot use operators in identifiers.(eg. ++,--)
    4. You can start the identifier with either a character or an underscore. Identifiers cannot be initialized from Digits.
    5. You can start identifiers from both A to Z with both small and capital letters or underscore. After this you can use digits and other characters or underscore. 
    • Example : _name,emp_name(right) || 2name, emp-name(wrong)

  • Constant : Constants are variables whose value does not change in any way during program execution. Whenever you make a constant declare, its value remains fixed during the execution of the program. If an attempt is made to change its value, an error occurs in the program.
    • Constants in C language are of two types.
    • 1.Constant Literals.
    • 2.Constant Variables.

  • 1. Constant Literals : Constant literals are values that you use directly in the program. For example, see the code given below.
    • y= x + 10;
    • 10 in the above statement is a constant literal. It has been used directly in the program. It cannot be changed during execution of the program.
    • Consider Constant literals as direct values that cannot be changed. It is generally not recommended to use literal constants.
    • Suppose you have used a literal constant in many places in the program, now if you need to change this constant, then you have to find it manually and change it everywhere in the program.Therefore, you should use literal constants to a minimum.

  • 2.Constant Variables: You declare constant variables as variables yourself. The advantage of using Constant variables is that if you have to change the constant later, then you do not need to change it in many places in the program, you just change the value of the constant variable and it changes everywhere in the program. You change.
    • You can declare Constant variables in two ways. They are being told below.
    • 1.Using #define directive.
    • 2.Using const keyword.

  • 1.Using #define directive :#define is a pre processor directive, by using it you declare constant variables. Constant variables are declared before the main function at the beginning of the program by this directive. You can use constant variables defined by this directive anywhere in the program. example :
    • #include<stdio.h>
    • #define result 10                 /* Defining constant variable*/
    • int main()
    • {
    •    int a=15, b=5;
    •   result = a + b; /* error because value of constant result variable can not changed.*/
    •   printf("%d",result);
    •   return 0 ;
    • }
    • The above program generates the output given below.
    • error : lvalue required as left operand of assignment result = a + b ; 

  • 2. Using const keyword : You can also declare constant variables by const keyword in C language. If you want to use constant variable only in a function, then you can declare constant variable by this keyword.
    • The use of const keyword in C language is explained by the following example.
    • #include<stdio.h>
    • int main()
    • {
    •     const int i = 4;
    •     cont int j = 4;
    •     c = i + j ; /* Adding two constants */
    •     printf("c = %d",c);
    •     return 0 ;

    • }
    • output : c = 11

  • Types of C constants :
    • Integer constant
    • floating-point constant
    • character constant
    • string constant 
    • Enumeration constant




No comments:

Post a Comment

If you have any query, please let me know

Followers