C Basics

Variables

User Rating:  / 2
PoorBest 

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

 

Type

Description

char

Typically a single octet(one byte). This is an integer type.

int

The most natural size of integer for the machine.

float

A single-precision floating point value.

double

A double-precision floating point value.

void

Represents the absence of type.

 

 

 

There are various other types of data types that we will cover in subsequent chapters.

Rules for Constructing Variable Names

• It is any combination of 1 to 31 alphabets, digits or underscores.

• It must start with an alphabet or underscore

• No commas or blanks are allowed within a variable name

• No special symbols other than an underscore is allowed

E.g. average_salary first_name lastName

Variable Declaration

All variables must be declared before we use them in C program, although certain declarations can be made implicitly by content. A declaration specifies a type, and contains a list of one or more variables of that type as follows: type variable_list; type is a valid C data type including char, int, float, double, or any user defined data type etc., and variable_list may consist of one or more identifier names separated by commas.

E.g. int name,subjects,div; char ch; float average,percentile; double result;

Initialization at the time of declaration. int subjects = 4; Though you can declare a variable multiple times in C program but it can be decalred only once in a file, a function or a block of code.

Variable Initialization

Variables are initialized (assigned an value) with an equal sign followed by a constant expression. The general form of initialization is:

variable_name = value;

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:

type variable_name = value;

 

E.g.  int d = 3, f = 5; /* initializing d and f. */

       byte z = 22; /* initializes z. */

       double pi = 3.14159; /* declares an approximation of pi. */

       char x = 'x'; /* the variable x has the value 'x'. */

 

It is a good programming practice to initialize variables properly otherwise, sometime program would produce unexpected/random result.

E.g.

#include <stdio.h>
void main ()
{
  /* variable declaration: */
  char a;
  int b,c;
  int sum;
  float d;
 
  /* actual initialization */
  a = ‘a’;
  b = 10;
  c = 20;
  sum = a + b;
  printf("Value of sum : %d \n", sum);
  f = 70.0/3.0;
  printf("Value of f : %f \n", f);
 
  printf(“Value of character a is %c”,a);
  getch(); // use return 0 for int main()
}

Output: -

 

value of c : 30
value of f : 23.333334

                                                                                          About Us  ||  Contact Us  ||  Terms & Conditions  ||  Privacy Policy 

Referral Banners