Variables and Identifiers

Variables

A variable is a named data storage location in user computer′s memory.

By using a variable′s name in the program, the users are, in effect, referring to the data stored

there.

For example:

  • Age = 10; implies that age hold the value 10.
  • Age = 20; now implies that age hold the value 20.
  • So from the above example age changed its value from 10 to 20.
  • Hence age can be referred to as a variable.

Identifier

In C the names that are used to reference variables, functions, labels and various other user defined objects are called identifiers.

The length of an identifier in C can vary from one to several characters.

In most cases the first character must be a letter or an underscore, and subsequent characters can be a letter, number or an underscore.

Identifiers are used extensively in virtually all information processing systems.

Naming entities makes it possible to refer to them, which is essential for any kind of symbolic processing.


Variable Definition

  • A variable is a way of referring to a memory location used in a computer program.
  • This memory location holds value-sperhaps numbers or text or more complicated types of data like a payroll record.

Variable Declarations

  • Before the users can use a variable in a C program, it must be declared.
  • A variable declaration tells the compiler the name and type of a variable and optionally initializes the variable to a specific value.
  • If the program attempts to use a variable that hasn′t been declared, the compiler generates an error message.
  • A variable declaration has the following form:
    • typename varname;
      • typename specifies the variable type and must be one of the keywords.
  • From the declarations, varname is the variable name, which must follow the rules mentioned earlier.
  • User can declare multiple variables of the same type on one line by separating the variable
    • names with commas:
    • int count, number, start; /* three integer variables */
    • float percent, total; /* two float variables */
    • char firstname; /* character type variable */

    Character Variables

    • A character is a single letter, numeral, punctuation mark, or other such symbol.
    • A string is any sequence of characters.
    • Strings are used to hold text data, which is comprised of letters, numerals, punctuation marks, and other symbols.
    • Using Character Variables, like other variable the users must declare chars before using them, and he can initialize them at the time of declaration.
    • Here are some examples of character variables:
      • char a, b, c; /* Declare three uninitialized char variables */
      • char code = ‘x’; /* Declare the char variable named code and store the character x there */
      • code = ‘!’; /* Store ! in the variable named code */

      Integer, Float and Double declarations

      • To declare a variable as integer, follow the below syntax:
        • int variable_name;
            • int is the type of the variable named variable_name.
            • ‘int’ denotes integer type.
      • A float is a single-precision
      • floating point value.
      • To declare a variable as float, follow the below syntax:
        • float variable_name;
      • A double is a double-precision
      • floating point value.
      • To declare a variable as double, follow the below syntax:
        • double variable_name;
      • Examples:
        • int i, j, k;
        • float f, salary;
        • double d;

      Programs

      Write a program to input number of days from user and display it, week and days format.


      For Example

      input is : 20 days

      then output is:

      2 week and 6 days.



      View Solution


      Write a program to input number of days from user and display it, Month, Week and Days format. (consider 30days = 1 month)


      For Example:

      input is : 50 days


      output is: 1 month, 2 week and 6 days.



      View Solution


      Write a C program which accept a character from user and ASCII Value of that character


      View Solution


      Write a program which accept one integer number from user and print it.


      View Solution


      Write a C Program to input any Character and Find its ASCII Value


      View Solution


      Write a C Program to input dividend, divisor and Compute Quotient and Remainder


      View Solution


      Write a C Program to Find the Size of int, float, double and char


      View Solution


      Write a C Program to Swap Two Numbers


      View Solution


      Write a C program to Swap Numbers Without Using Temporary(3rd) Variables


      View Solution


      Write a C Program to input any  number and Check  number is Odd or Even Using the Ternary Operator


      View Solution