We know each variable in C programming has a specific data type associated with it. Each data type requires different amounts of computer memory and has some specific operations which can be performed over it. Data types decides how we enter data into our programs, what type of data we enter, how much space it will occupy in storage and how the bit pattern stored is interpreted.
We can classify the C data type into 2 types some are classifying into 3 and 4 but the total data types are same in all cases. So we will broadly classify into 2 types.
Primary or Fundamental data types : int, float, char and void are known as primary or fundamental data types.
Derived data types : When primary data types are grouped or twisted a little they form derived data types. The examples are array, structure, union and pointer.
Primary Data Types
char, int, float, double
We have already discussed about these data types in our previous class and we know different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. To know the exact size of a data type or a variable on a particular platform, we can use the sizeof operator. The expressions sizeof(type) outputs the storage size of the object or type in bytes.
int a =1;
char b =‘G’;
double c =3.14;
//printing the variables mentioned above along with their sizes
printf(“Hello! I am a character. My value is %c and “
“my size is %lu byte.\n”, b,sizeof(char));
//can use sizeof(b) above as well
printf(“Hello! I am an integer. My value is %d and “
“my size is %lu bytes.\n”, a,sizeof(int));
//can use sizeof(a) above as well
printf(“Hello! I am a double floating point variable.”
” My size is %lu bytes and my value is %lf .\n”,sizeof(double),c);
//can use sizeof(c) above as well
printf(“Bye! See you soon. :)\n”);
Hello! I am a character.My value is G andmy size is1byte.
Hello! I am an integer.My value is1andmy size is4 bytes.
Hello! I am a double floating point variable.My value is3.140000andmy size i
s 8 bytes.
Bye!See you soon.🙂
What are derived data types in C programming ?
When primary data types are little twisted or grouped for a special purpose is called derived data type. for example pointers, arrays, structures and unions. we will discuss regarding them in later chapters.
Use of extern keyword in C variable declaration
A variable, which is declared using the extern keyword, declares outside the main() function. When we are using extern as a prepend the variable only declares, memory is not allocated to it. before moving forward please look at the below example to have a better understanding of declaring and defining.
Now lets look at below examples:
This program will compile correctly and give output 10. because the variable ‘var’ declared and defined implicitly.
Now look at below example:
This program will give compiler error because due to extern key word this variable ‘var’ is only declared not defined. So memory is not allocated to it and in main function we are trying to store ’10’ into it. In this program extern keyword explains compiler that there should exist another .c file where this variable is declared and defined. So let us create a c file named file1.c in the same project and write only one statement “int var;”.
The main purpose of using “extern” keyword is to extend the visibility of variables. So that they can be accessed between two different files which are part of a large program.
Constants in C Program
A constant in c program can be defined as a fixed value that the program may not alter during its execution. These fixed values can also be called as literals. Constants may be any of the basic data types like a floating constant, an integer constant, a character constant, or a string literal.
Simply to say constants are just like regular variables whose values cannot be modified after their definition.
We can define a constant in C in below two ways: 1. using #define preprocessor
SYNTAX : #define identifier value;
see the below example to understand the use of preprocessor #define.
#define ORGANISATION_CODE 10
printf(“Code of Organisation : %d”, ORGANISATION_CODE);
2. using const keyword
SYNTAX: const datatype identifier = value;
see the below example to understand the use of const.
constint ORGANISATION_CODE =10;
printf(“Code of Organisation : %d”, ORGANISATION_CODE);
There is no specific rule for naming of constants. But it is a good programming practice to naming constants in capital letters and variables in small letters.
Diffference between #define and const in C?
The #define preprocessor directive uses macros for defining constant values. Before compilation the preprocessor replaces every macro identifier in the code with its corresponding values from the directive. It does not use memory. The final code sent to the compiler already have the constant values in place. Do not put a semicolon at the end of #define statement.
The const uses memory for storage. The big advantage of const over #define is type checking.and Any thing that can be done with a normal variable, can also be done with const like have poitners to, can pass them around, typecast them e.t.c .
Normally const is a better option if we have a choice. Some situations are there where #define cannot be replaced by const. For example, #define can take parameters (See the below example). We can also use #define to replace some text in a program with another text.
char y =5;
int x =10;
For Life time valid video tutorials of this course with quiz and certification : Click Here
Downloadable video tutorials for TTRC Player : Click Here