Variable in C

********** Variable Declaration*******
:::::::VARIABLES:::::::
1-You can store values in variables.
2-Each variable is identified by a variable name.
3-Each variable has a variable type.
4-Variable names start with a letter or underscore (_), followed by any number of letters, digits, or underscores.
5-Uppercase is different from lowercase, so the names sam, Sam, and SAM specify three different variables.
6-The variables are defined at the begining of the block.
The following is an example of some variable names:
average            /* average of all grades */
pi                 /* pi to 6 decimal places */   
number_of_students /* number students in this class */
The following are not variable names:
3rd_        /* Begins with a number */
all$        /* Contains a "$" */  
the end     /* Contains a space */
int         /* Reserved word */
Meaningful variable name:
entry_total /* total number of items in current entry */  
all_total   /* total of all entries */
 
::::::DECLARE VARIABLE:::::::
A variable declaration serves three purposes:
1-It defines the name of the variable.
2-It defines the type of the variable (integer, real, character, etc.).
3-It gives the programmer a description of the variable.
int answer;     /* the result of our expression */
1-The keyword int tells C that this variable contains an integer value.
2-The variable name is answer.
3-The semicolon (;) marks the end of the statement.
4-The comment is used to define this variable for the programmer.
::::::USING A VARIABE TO STORE THE VALUE:::::::::::
#include <stdio.h>
int main(void)
{
  int salary;           
  salary = 10000;       
  printf("My salary is %d.", salary);
  return 0;
}
OUTPUT:
My salary is 10000.
 
::::::::Initialize int value in declaration:::::::
#include <stdio.h>
int main(void)
{
  int number0 = 10, number1 = 40, number2 = 50, number3 = 80, number4 = 10;
  int number5 = 20, number6 = 30, number7 = 60, number8 = 70, number9 = 110;
  int sum = number0 + number1+ number2 + number3 + number4+
        number5 + number6 + number7 + number8 + number9;
  float average = (float)sum/10.0f;
  printf("\nAverage of the ten numbers entered is: %f\n", average);
  return 0;
}
OUTPUT:
Average of the ten numbers entered is: 48.000000
:::::Meaningful variable name::::::
int p,q,r;
Now consider another declaration:
int account_number;   
int balance_owed; 
int account_number;      /* Index for account table */
int balance_owed;
 
::::::Define three variables and use assignment operator to assign value:::::
int main()
    { 
        int term;       /* term used in two expressions */
        int term_2;     /* twice term */  
        int term_3;     /* three times term */
        term = 3 * 5; 
        term_2 = 2 * term;
        term_3 = 3 * term;
        return (0);
    }
:::::: Use printf to output variable::::::
#include <stdio.h>
int main() 
 { 
        int term;       /* term used in two expressions */ 
        term = 3 * 5; 
        printf("Twice %d is %d\n", term, 2*term); 
        printf("Three times %d is %d\n", term, 3*term);
        return (0);
    }
OUTPUT:
Twice 15 is 30
Three times 15 is 45
**********Variable Size and Limitation*********
:::::Finding the limits::::
#include <stdio.h>     
#include <limits.h>  
#include <float.h>
int main(void)
{
  printf("Variables of type char store values from %d to %d", CHAR_MIN, CHAR_MAX);
  printf("\nVariables of type unsigned char store values from 0 to %u", UCHAR_MAX);
  printf("\nVariables of type short store values from %d to %d", SHRT_MIN, SHRT_MAX);
  printf("\nVariables of type unsigned short store values from 0 to %u",USHRT_MAX);
  printf("\nVariables of type int store values from %d to %d", INT_MIN, INT_MAX);
  printf("\nVariables of type unsigned int store values from 0 to %u", UINT_MAX);
  printf("\nVariables of type long store values from %ld to %ld", LONG_MIN, LONG_MAX);
  printf("\nVariables of type unsigned long store values from 0 to %lu", ULONG_MAX);
  printf("\nVariables of type long long store values from %lld to %lld", LLONG_MIN, LLONG_MAX);
  printf("\nVariables of type unsigned long long store values from 0 to %llu", ULLONG_MAX);
  printf("\n\nThe size of the smallest non-zero value of type float is %.3e", FLT_MIN);
  printf("\nThe size of the largest value of type float is %.3e", FLT_MAX);
  printf("\nThe size of the smallest non-zero value of type double is %.3e", DBL_MIN);
  printf("\nThe size of the largest value of type double is %.3e", DBL_MAX);
  printf("\nThe size of the smallest non-zero value of type long double is %.3Le", LDBL_MIN);
  printf("\nThe size of the largest value of type long double is %.3Le\n", LDBL_MAX);
  printf("\nVariables of type float provide %u decimal digits precision.",  FLT_DIG);
  printf("\nVariables of type double provide %u decimal digits precision.",  DBL_DIG);
  printf("\nVariables of type long double provide %u decimal digits precision.", LDBL_DIG);
  return 0;
}
 
OUTPUT:
Variables of type char store values from -128 to 127
     Variables of type unsigned char store values from 0 to 255
     Variables of type short store values from -32768 to 32767
     Variables of type unsigned short store values from 0 to 65535
     Variables of type int store values from -2147483648 to 2147483647
     Variables of type unsigned int store values from 0 to 4294967295
     Variables of type long store values from -2147483648 to 2147483647
     Variables of type unsigned long store values from 0 to 4294967295
     Variables of type long long store values from -9223372036854775808 to 9223372036854775807
     Variables of type unsigned long long store values from 0 to 18446744073709551615
    
     The size of the smallest non-zero value of type float is 1.175e-38
     The size of the largest value of type float is 3.403e+38
     The size of the smallest non-zero value of type double is 2.225e-308
     The size of the largest value of type double is 1.798e+308
     The size of the smallest non-zero value of type long double is 3.362e-4932
     The size of the largest value of type long double is 1.190e+4932
    
     Variables of type float provide 6 decimal digits precision.
     Variables of type double provide 15 decimal digits precision.
     Variables of type long double provide 18 decimal digits precision.
:::::::Finding the size of a type :::::::#include <stdio.h>
int main(void)
{
  printf("\nVariables of type char occupy %d bytes", sizeof(char));
  printf("\nVariables of type short occupy %d bytes", sizeof(short));
  printf("\nVariables of type int occupy %d bytes", sizeof(int));
  printf("\nVariables of type long occupy %d bytes", sizeof(long));
  printf("\nVariables of type float occupy %d bytes", sizeof(float));
  printf("\nVariables of type double occupy %d bytes", sizeof(double));
  printf("\nVariables of type long double occupy %d bytes", sizeof(long double));
  return 0;
}
OUTPUT:
Variables of type char occupy 1 bytes
     Variables of type short occupy 2 bytes
     Variables of type int occupy 4 bytes
     Variables of type long occupy 4 bytes
     Variables of type float occupy 4 bytes
     Variables of type double occupy 8 bytes
     Variables of type long double occupy 12 bytes

*********VARIABLE SCOP*********CODE:1-
Scope of variables

Variable can be defined in the block.
The blocks are marked using { and } braces.
The scope of the variable is in the block where it is declared.
Variable defined in the outer block can be used in the inner block.
The nearest definition has more precedence.
E.G
#include <stdio.h>
main()
{  
    int i = 10;   
    {       
        int i = 0;           
        for( i=0;i<2;i++) 
        {
               printf("value of i is %d\n",i);
        }    
    }
    printf("the value of i is %d\n",i);
}
::::Inner variable shadows outer variable::::::::
#include <stdio.h>
int main(void)
{
  int count = 0;                               
  do
  {
    int count = 0;               
    ++count;                     
    printf("\ncount = %d ", count);
  }
  while( ++count <= 8 );  /* This works with outer count */
  /* this is outer */
  printf("\ncount = %d\n", count);
  return 0;
}

************GLOBAL VARIABLE****************
::::::Declare global variables:::::

1-A global variable is available to all functions in your program.
2-A local variable is available only to the function in which it's created.
3-Global variables are declared outside of any function.
4-Global variables are typically declared right before the main() function.
5-You can also declare a group of global variables at one time:

int s,t;

And, you can preassign values to global variables, if you want:
 char prompt[]="What?";
:::::::::Define and use Global variables:::::::
#include <stdio.h>
int count = 0;                         /* Declare a global variable   */
void test1(void){
  printf("\ntest1   count = %d ", ++count);
}
void test2(void){
  static int count;                   /* This hides the global count */
  printf("\ntest2   count = %d ", ++count);
}
int main(void){
  int count = 0;                      /* This hides the global count */
  for( ; count < 5; count++) {
    test1();
    test2();
  }
  return 0;
}
OUTPUT:
     value of i in main 0
     value of i after call 50

Comments

Popular posts from this blog

Error : DependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: com.adobe.aem:uber-jar:jar:apis -> version 6.3.0 vs 6.4.0

ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote'

Error : Sorry, there was an error : unauthorized_client in Sitecore 9.1 login