Sunday, 17 February 2019

Variables in C

What is variable
  • A variable in a program is a specific piece of memory that consists of one or more contiguous bytes, typically 1, 2, 4, 8 or 16 bytes.
  • Every variable has a type that specifies the kind of data the variable can store.
  • The type of a variable determines how many bytes are allocated for it.
  • Every variable in a program has a name, which will correspond to the memory address for the variable.
  • You use the variable name to store a data value in memory or retrieve the data that the memory contains.
  • The value of a variable isn’t fixed, and you can change it whenever you need to throughout a program.
Rules for naming variables
  • A variable name is a sequence of one or more uppercase or lowercase letters, digits, and underscore characters (_) that begin with a letter (incidentally, the underscore character counts as a letter).
  • A variable name must not begin with a digit.
  • A variable name must not include characters other than letters, underscores, and digits.
  • Variables starting with one or two underscore characters are often used in the header files, so don’t use the underscore as the first character in your variable names.
  • Variable names is that they are case sensitive. 
Notes:
  • The maximum number of characters that you can have in a variable name will depend on your compiler.
  • A minimum of 31 characters must be supported by a compiler that conforms to the C standard.
Types of variables
The type of a variable is determined by the its data type while declaring the variable. Variables can be of several type from simple type to complex type.

(a).Integer type variables
·         Used to store integer values.
·         Type is int.
·         Example : int marks=98;

(b).Real type variables
·         Are used to store real numbers.
·         Type is float, double etc.
·         Example : float rate=12.5;

(c).Char type variables
·         Used to character values.
·         Type is char.
·         Example : char grade=’A’;

(d).Complex type variables
·         Like struct,union type;

Variable declaration
Variable declaration specifies the variable name and type of data that variable will store.
Syntax for declaring a variable:
dataType variableName;
example : int salary;  // variable declaration

This statement is called a variable declaration because it declares the name of the variable.
Note that a variable declaration ends with a semicolon. If you omit the semicolon, your program will generate an error when you try to compile it.

Assigning value to a variable
The assignment operator(=) is used to assign value to a variable;

Example
int marks=98;
char grade=’A’;
float rate=12.5;


//Program to ask user their marks and then display the marks.
#include<stdio.h>
int main(){
  int marks;
  printf(Enter marks:“”);
  scanf(“%d”,marks);
  printf(“Your marks are %d.”,marks);
 return 0;
}

Please comment if you find anything incorrect, or you want to improve the topic discussed above..

No comments:

Post a Comment