"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Monday, 1 July 2019

Variable and Constant

Variable

        A quantity whose value may change during execution of the program is called variable. A variable represents a location in the computer's memory.it is used to store program's input data and its result during program execution. The name of the memory location .i.e. the variable name remains fixed during program execution. however data stored in variable may change during program execution.

Declaration of Variables

        Specifying the variable names and their data types in the program is called declaration of variables. C is a typed language. It means that all variables must be declared before they are used in the program. The C-compiler will give an error if an undeclared variable is used in a program.

Variable Initialization 

        In C program, when a variable is declared, the compiler allocates some memory space for it. A meaningless data may be assigned to it. This meaningless data is called garbage data. A value can also be assigned to a variable at the time of its declaration. Assigning a value to a variable at the time of its declaration is called variable initialization.

Write a program to declare and initialize two variables of 'int' type. Calculate the sum and average of these values and display the result on screen.


C

C++


    # include < stdio.h >
     # include < conio.h >
     void main ( ) 
     {
          int  x , y ;
          float  sum , avg ;
          x = 25 , y = 5 ;
          sum = 25 + 5 ;
          avg = sum / 2.0 ;
          printf ( " Sum is = %f  \n " , sum ) ;
          printf ( " Average is = %f " , avg ) ;
     }


     # include < iostream >
     int main ( ) 
     {
          int  x , y ;
          float  sum , avg ;
          x = 25 , y = 5 ;
          sum = 25 + 5 ;
          avg = sum / 2.0 ;
          cout << “ Sum is :  “ << sum ;
          court << “ Average is : “ << avg ;   
return 0;
}





Constants

       The quantity whose the value cannot be changed during the execution of a program is called  constantsIn C language, an identifier. A value is assigned to it at the time of its declaration. Similarly, the 'define' directive can also be used to define  constants identifier. Once a value is assigned to a constant identifier, it cannot be changed during program execution.

Types of Constants

       C-language provides following types of constants:
  1.  Numeric constants
  2.  Character constants
  3.  String constants

1.   Numeric Constants

        Numeric constants consist of numbers (or numeric value). These may be positive or negative values. The numeric constants are further divided into two types:
  1.  Integer constants
  2.  Floating-point constants

  •  Integer constants

        The numeric values have no decimal part(i.e. the whole values) are known as integer constants. The numeric vales 10,-16,420,87 are examples of integer constants.

  •  Floating constants

        The numeric values that have decimal part are known as floating constantsThe numeric 1.609,4.0 and -6.2 are the examples of floating constants.

 2.   Character constants

        A single character enclosed in single quotation marks is called character constants A single character may be an alphabet, digit or a special character. The maximum length of a character constant is 1 character. For example 'x','y','+','a' and '9' represent character constants.

  3.   String character

        A sequence of character enclosed in double quotation marks is known as string constants or string literalThe C compiler considers as string constant as single token. For example "My first program","042-4709705","ICS-999" represent string constants.

No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.