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:
int globalNum = 5;
int a = 20;
char letter = 'A';
float price = 24.99;
const float PI = 3.14;
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:
- Fundamental Data Types
- Integer types
- Floating type
- Character type
- Derived Data Types
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 -float, double : 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:
- A variable name can have letters(upper and lower case), numbers and underscore only.
- 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.
- A variable name cannot be one of the keywords of the C language.
|Keywords in C Programming|
Macros in C:
A 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:
#define PI 3.14;
#define Area(r) PI*r*r;
int radius = 20;
float area = Area(radius);
printf("Area of circle = %.2f", area);
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.