C/Language Basics/Variable

Материал из C\C++ эксперт
Перейти к: навигация, поиск

demonstrate the use of variables

<source lang="cpp"> int main() {

   int variable;
   int variable_2;
   int variable_3;
   variable = 3 * 5;
   variable_2 = 2 * variable;
   variable_3 = 3 * variable;
   return (0);

}


      </source>


Finding the size of a type

<source lang="cpp"> /* Finding the size of a type */

  1. include <stdio.h>

void main() {

 printf("\n  char occupy %d bytes", sizeof(char)); 
 
 printf("\n short occupy %d bytes", sizeof(short)); 
 
 printf("\n int occupy %d bytes", sizeof(int)); 
 
 printf("\n long occupy %d bytes", sizeof(long)); 
 
 printf("\n float occupy %d bytes", sizeof(float)); 
 
 printf("\n double occupy %d bytes", sizeof(double)); 
 
 printf("\n long double occupy %d bytes", sizeof(long double)); 
}


      </source>


For and scanf

<source lang="cpp">

  1. include <stdio.h>

void main() {

 int number = 0;      
 int count = 10;        /* Number of values to be read */
 long sum = 0L;         
 float average = 0.0f;  
 int i = 0; 
 /* Enter in the ten numbers to be averaged */
 for(i = 1; i <= count; i ++)
 {
   printf("Enter grade: ");
   scanf("%d", &number);                        /* Read a number               */
   sum += number;                               /* Add it to sum               */
 }
 average = (float)sum/count;                     /* Calculate the average      */
 printf("Average of the ten numbers entered is: %f", average);

}


      </source>


the variable limits

<source lang="cpp"> /*Finding the variable limits */

  1. include <stdio.h>
  2. include < limits.h > /* For limits on integer types */
  3. include <float.h> /* For limits on floating-point types */

void main() {

 printf("char store values from %d to %d", CHAR_MIN, CHAR_MAX); 
 
 printf("\n unsigned char store values from 0 to %u", UCHAR_MAX);
 
 printf("\n short store values from %d to %d", SHRT_MIN, SHRT_MAX);
 
 printf("\n unsigned short store values from 0 to %u", USHRT_MAX);
 
 printf("\n int store values from %d to %d", INT_MIN, INT_MAX);
 
 printf("\n unsigned int store values from 0 to %u", UINT_MAX);
 
 printf("\n long store values from %d to %d", LONG_MIN, LONG_MAX);
 
 printf("\n unsigned long store values from 0 to %u", ULONG_MAX);
 printf("\n\n 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 %.3e",
                                                      DBL_MIN);
 printf("\nThe size of the largest value of type long double is %.3e\n",
                                                       DBL_MAX);

}


      </source>