variable in C

C series for 1st sem students: variable, datatypes and macros

Posted on Posted in Blogs, Technology






Today we are going to discuss variable, datatypes and macros in C programming. In the last blog of this series we’ve discussed about preprocessor and the include statement. If you’ve missed that blog, check here.

Variable is a container to hold data. Datatype of a variable defines the type of data to be stored and the space required to store it. Variables in programming are analogous to variables in algebra. Just as algebraic expressions can have constant terms like ax + by = c , C programs can have constants, defined similar to variables but with the const keyword.

#Example of variables and datatypes:

#include <stdio.h>
 
int globalNum = 5;
 
int main(){
int a = 20;
char letter = 'A';
float price = 24.99;
const float PI = 3.14;
display();
return 0;
}
 
void display(){
int localNum = 4;
printf("Global Variable = %d and Local Variable = %d", globalNum, localNum);
}

Values of regular variables can be changed within the program, but values of constants cannot be changed within the program. Another important aspect of variables (and constants) is their scope, i.e. in which part of the program their values can be accessed/modified. Scope also determines the lifetime of a variable. In the example above the variable globalNum has a global scope. This means it can be accessed from anywhere within the program. But variables like localNum and a are accessible only within their respective function blocks. Here PI is defined as a constant, and its value cannot be changed even within the main function.

The lifetime of the local variables are limited to the lifetime of their parent functions. So the variable localNum stays in memory only till the display function gets executed. But the global variable globalNum stays in memory till the program execution stops.

Data types in C:

  1. Fundamental Data Types
    • Integer types
    • Floating type
    • Character type
  2. Derived Data Types
    • Arrays
    • Pointers
    • Structures
    • Enumeration

Integral types  int : Integers are whole numbers that can have both positive and negative values but no decimal values. The size of int is either 2 bytes(In older PC’s) or 4 bytes.

Floating types -floatdouble : Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare a floating point variable in C by using either float or double keyword. The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes.

Character types – char : Keyword char is used for declaring character type variables. The size of character variable is 1 byte.

We will discuss the derived data types in a later blog. In addition to the datatypes, there are qualifiers which change the meaning of base datatypes to yield a new datatype.

  • Size qualifiers: long and short. As their names suggest, they can increase or decrease the size of a datatype.
  • Sign qualifiers: signed and unsigned. They represent whether negative values are allowed in a datatype. Removing negative values effectively increase the range of positive values that can be stored in a datatype.
  • Constant and Volatile: const is used to define a constant value, as we’ve seen above. volatile variables are those that can be changed by some external sources outside the program.

While naming a variable we must follow these rules:

  1. A variable name can have letters(upper and lower case), numbers and underscore only.
  2. The names can only start with letters and underscores. Starting variable names with underscores is not recommended because system variables starts with underscores and this may cause conflicts.
  3. A variable name cannot be one of the keywords of the C language.
Keywords in C Programming
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

 

Macros in C:

macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. Macros are defined to the preprocessor using # identifier. Macros can be variable like, as well as function like. Lets see this example:
 

#Example of macros:

#include <stdio.h>
 
#define PI 3.14;
#define Area(r) PI*r*r;
 
int main(){
int radius = 20;
float area = Area(radius);
printf("Area of circle = %.2f", area);
return 0;
}

Using macros can simplify your programs by avoiding functions for small calculations and let you avoid using global constants.






If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.

Comments

comments