Keywords and Variables in C

05 min read

Keywords in C

Keywords are the words whose meaning has already been explained to the C compiler. They have a specific meaning and they implement specific C language features. Keywords can be used only for their intended purpose. They cannot be used as names for variables or other user-defined program elements. The following is the list of 32 reserved words (keywords) in C. A few of them are explained below.keywords and variables in C

Const:

const can be used to declare constant variables. Constant variables are those variables which once initialized, can’t change their value which means the value assigned to them remains unmodified.

Static:

The static keyword is used to declare static variables. These variables have a property of preserving their value even after they are out of their scope. Hence, static variables preserve the value of their last use in their scope. So they are initialized only once and exist till the termination of the program. This means that no new memory get allocated as they are never re-declared. Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.

Extern:

If extern is used to declare a variable, then it means that the variable is defined elsewhere and not within the same block where it is used. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block of the program.

Void:

Void is a special data type only. Void means an empty data type. It means it has nothing or it holds no value. When it is used as the return data type for a function, it simply represents that the function returns no value. 

Typedef

It is used to create a new data type which is equivalent to the existing data type. From then, the new data type can be used to declare variables.

 

Variables in C

A variable is an identifier used to represent a single data item whose quantity (or) value may vary during program execution. Variable names are the names given to memory locations where the values are stored. In C language, the data type associated with a variable cannot change. The data item corresponding to the variable can be accessed by simply mentioning the name of the variable.

But before using  variables in the program, the compiler should be informed well in advance about two things

  1. such a variable will be used in the program (eg., 'a' and `13')
  2. the type of data which the variable can hold.

A Type Declaration statement takes care of informing these to the compiler. The general syntax for a Type declaration statement:

datatype variablename;
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error