identifiers and variables in c | All In One Tuts

What are Identifiers in C language

Identifiers : Identifiers refers to the names of variables, functions and arrays. These are user defined names and consist of a sequence of letters and digits, with a letter as a first characters. Both uppercase and lowercase letters are permitted. The underscore character is also permitted in identifiers. It is usually used as link between two words in long identifiers.

Differentiate between Keywords and Identifiers 

Keywords : In C language, some words are reserved for specific task. They carry special meaning for C compiler. Thus, these words should not be used as identifiers. These reserved words are called ‘keywords’. Keywords are the words whose meaning had already been explained to the C compiler.

Following is a partial list of C keywords:

Auto, Continue, Float, Break, Do, If, Else, While, For; Struct etc.

All C keywords must be written in lowercase letters. Because, in C, both lowercase and uppercase ‘letters are significant.

Identifiers : Identifiers are basically the names that are used for identifying an entity. These entities can be anything like variables, functions etc.

For example, if roll number and age of the students have to be referred in the program, then two identifiers, such as Roll and Age could be used in the program. The rules that apply on identifiers are as follows :

  1. An identifier can consist of alphabets, digits, and / or underscore.
  2. It must not start with a digit, however it can start with underscore.
  3. C is case sensitive so ROLL and Roll will differ from each other.

What are variables? Enumerate the rules followed for naming variables in C language.

Variables : A variable is a data name that may be used to store a data value. Unlike constant that remain unchanged during the execution of a program, a variable may take different value at different times during execution.

Rules regarding Variable :
Variable name may consist of letter, digits and the underscore character, subject to the following conditions:

  1. They must begin with a letter. Some system permit underscore as the first character.
  2. ANSI standard recognizes a length of 31 characters. However, the length should not be normally more than eight character, since only first eight character are treated as significant by many compiler.
  3. Upper case and lower case are significant. That is the variable ‘Total’ is not same as ‘total’ or ‘TOTAL’.
  4. The variable name should not be a keyword.
  5. White space is not allowed.

Local versus Global variables in C

Local versus Global variables in C :
Local Variables : The formal arguments and variables declared in the body of a function are unknown outside the function. The factor that determines which function recognizes a variable and which do not is called the visibility of the variable.
A local variable used in this way in a function is known as automatic variable, because it is automatically created, when a function is called and destroyed, when the function returns. The length or duration of time a variable last is called its life time.

Example :   auto int a, b, c ;

Global Variables :
In contrast to a local variable, a global variable is visible to all functions, and is declared outside all function. The usual place of declaration of a global variable is in the beginning of the program file i.e. before all function definition. These are also known as external variables. The use of external variables provides a convenient mechanism for transferring information back and forth between functions.

Example :      extern float root 1 , root 2 , char star ;

What are Derived data types  

Ans. Derived data type are the data types that are derived from the basic data types. In C language, we have three different types of derived data types. These are:

  1. Arrays
  2. Functions
  3. Pointers

 

  1. Array : Array is a collection of similar data type. Syntax used for declaration of array is
    <data type> < variable name> [<size>];Example :  intmarks[10];
  2. Functions : Functions are block of statements that are grouped together to perform a specific task.
  3. Pointers : A pointer is a variable that contains an address which is location of another variable in memory.

    What do you mean by Enumerated Data Type? Explain with example. 

 

Enumerated Data Types : An enumeration is a data type, similar to a structure or a union. Its members are constants that are written as identifiers, though they have signed integer values. These constant represents values that can be assigned to corresponding enumeration variables.
In general terms, an enumeration may be defined as :
enumtag {member 1, member 2,…. member m }; Where enum is s required keyword; tag is a name that identifies enumerations having this composition and member 1, member 2,….. member m represent the individual identifiers that may be assigned to variables of this type. The member name must differ from one another, and they must be distinct from other identifiers whose scope is the same as that of the enumeration.

Example : enum colors {black, blue, cyan, magenta, red, white };
colors foreground, background;

The first line defines an enumeration named col­ors. The enumeration consists of sixth constants whose names are black, blue, cyan, magenta, red, white.

What are arrays? Give the general form of array declaration.   

Arrays : An array is a group of related data items that share a common name. It is defined as a finite set of homogeneous elements stored in continuous memory locations. Like any other variable, arrays must be declared before they are used. The gen­eral form of array declaration is :

type variable_name [size];

For example, int group [5|;

declares the group as an array to contain a maximum of 5 integer constant.

Its Use :

  1. Arrays give us the flexibility of maintaining more than one variable under the common name. It makes programmers task easy because handling one variable name is easier than handling 100 variable.
  2. Arrays also provide the expandability. If we want to have more variables, we can simply do it without of much extra efforts.
  3. It is used to sort a list and find the median within no time.

    Different types of arrays available in C

     Types of Array :

  1. Single Dimensional Array : In single dimensional array, only one subscript is required.
  2. Two Dimensional Arrays: It is also possible for arrays to have two or more dimensions. The two dimensional array is also called a ‘Matrix’.
  3. Multi-Dimensional Arrays : It can be thought of as array of arrays.

     Define Multidimensional arrays  

Multidimensional Arrays : Arrays can be multidimensional also. C also supports the multidimensional arrays. Like single dimensional array can be thought of as a list of vector. Similarly, 2-D array can be think of as a table consisting of row and columns.
Arrays can be N-dimensional but they rarely exceed 3-dimensions. Three dimensional array may be thought of as a collection of table like pages in a book and so on.
Regardless of the size of the multidimensional array, the type of all the elements of an array is same. In general, a multidimensional array can be declared as:
atype aname[exprl] [expr2] ….[exprn] ;
where, atype is the base-type of array. This tells the type of individual items of array, aname is. the name of the array i.e. name by which array can be called or identified.

An example showing entry of data into an array

Entering Data into an array :
Here is the section of code that places data into an array:

for (i = 0; i < =29; i++)
{

printf(“\n Enter marks”); scanf (“%d”, & marks [i]);

}

What are various data types used in C Language? Illustrate their declaration and usage.   

C has different data type for different type of data and can be classified as :

data types | All In One Tuts

Data type declaration and their size and range i

Variable keyword Byte Required Range
char 1 -128 to 127
unsigned char 1 0 to 255
;nt 2 -32768 to 32767
short int 2 -32768 to 32767
Long int 4 -2,147.483,648 to 2,147,483647
unsigned int 2 0 to 65535
unsigned long int 4 0 to 4,294,967,295
foat 4 3.4 E-38 to 3.4 E + 38
double 8 1.7 E-308 to 1.7 E +308
Long double 10 3.4 E-4932 to 1.1 E + 4932
Hello Viewers,
welcome to the Allinonetuts a Technical tutorial and Programming Blog Maintained by Vikas Kardam, a Web Developer and Blogger From Delhi, India.
Identifiers and Variables In C Programming was last modified: April 9th, 2017 by Vikas Kardam

Leave a Reply

Your email address will not be published. Required fields are marked *