At the end of this lesson-
- 1.You will be able to explain data type and it’s classification.
- 2. You will be able to explain memory range and format specifier of data type.
- 3. You will be able to describe the keywords of ‘C’ programming language.
- 4. You will be able to declare variable and constant in ‘C’ program.
- 5. You will be able to describe the rules of declaring variable in ‘C’ program.
Data Type: A data type specifies the type of data that a variable can store such as integer, floating, character, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it.
There are the following data types in C language:
Primary or Basic or Built-in Data types:
- char:It stores a single character and requires a single byte of memory in almost all compilers. For example- ‘A’, ‘a’, ‘+’ etc.
- int: It is used to store an integer number. For example- 10, 300, 6000 etc.
- float: It is used to store decimal numbers (numbers with floating point value) with single precision. For example- 9.81, 345.7633 etc.
- double: It is used to store decimal numbers (numbers with floating point value) with double precision. For example- 843.345678, 3293.837234 etc.
‘void’ data type: ‘void’ type means no value. This is usually used to specify the type of functions which returns nothing.
Below the list of ranges along with the memory requirement and format specifiers of different data types on 32 bit gcc compiler:
DATA TYPE | MEMORY (BYTES) | RANGE | FORMAT SPECIFIER |
---|---|---|---|
char | 1 | −128 to 127 | %c |
unsigned char | 1 | 0 to 255 | %c |
int | 2 | −32,768 to 32,767 | %d |
unsigned int | 2 | 0 to 65,535 | %u |
long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
unsigned long int | 4 | 0 to 4,294,967,295 | %lu |
float | 4 | %f | |
double | 8 | %lf | |
long double | 10 | %Lf |
Modifiers: Modifiers are keywords which prefixed with basic data types to specify the amount of memory space to be allocated for a variable. For example- signed and unsigned modifiers are used in ‘char’ and ‘int’ data type. On the other hand, long and short modifiers are used in ‘int’ and ‘double’ data type.
Token: Each and every smallest individual units in a C program that are meaningful to the compiler are known as C tokens. Or We can say C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Tokens can be classified as follows:
- 1. Keywords ( eg: auto, break,int,short, while etc.)
- 2. Identifiers (eg: main, total, etc.)
- 3. Constants (eg: 9.81, 3.1416, 10, 20, etc.)
- 4. Strings (eg: “total”, “hello” etc.)
- 5. Special symbols (eg: (), {}, #, $, @, &, etc.)
- 6. Operators (eg: +, /,-,*, etc.)
Keywords: Keywords are pre-defined or reserved words in a programming language. Each keyword is meant to perform a specific function in a program. Since keywords are referred names for compiler, they can’t be used as variable name. Keywords are written in lowercase letters. C language supports 32 keywords which are given below:
Identifiers: Each element in a C program is given a name called identifiers.They are used to identify a particular element or used for naming of variables, functions, array etc. in a program. Identifiers are the user-defined names consisting of ‘C’ standard character set. Each identifier of a program must have a unique name.
Following rules must be followed for naming identifiers:
- 1. The first character must always be an alphabet or an underscore( _ ). For example- Count, a7, _number, etc are valid but 22Roll is not valid.
- 2. It should be formed using only letters(A-Z, a-z), numbers(0-9), or underscore( _ ). For example-
- roll_number, roll22, etc are valid but don’t_use, my@roll, &a are not valid.
- 3. A keyword cannot be used as an identifier. For example- for, while, if, etc. can not be used.
- 4. It should not contain any white space character. For example- ‘roll_number’ is valid but ‘roll number’ is not valid.
- 5. Lower case and upper case characters can be used but they carry different meaning. For example- number and NUMBER are different in meaning.
- 6. It should be up to 31 characters long as only first 31 characters are significant.
Variable: In C language, when we want to use some data value in our program, we can store it in a memory space and name the memory space so that it becomes easier to access it.The naming of an address is known as variable. Variable is the name of memory location. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A single variable can be used at multiple locations in a program. A variable name must be meaningful. A variable must be declared first before it is used somewhere inside the program. Variable names are just the symbolic representation of a memory location. A variable name is written following the rules for naming identifiers. Format for declaring a variable is given bellow:
- Data_type variable_name;
- For example: int number;
The Programming language C has two main variable types:
- 1. Local Variables
- 2. Global Variables
Local Variables: Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block. When a local variable is defined – it is not initialized by the system, you must initialize it yourself. When execution of the block starts the variable is available, and when the block ends the variable ‘dies’.
Global Variables: Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it. Global variables are initialized automatically by the system when you define them!. If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use global variables and local variables with the same name.
Constants: A constant is a value or an identifier whose value cannot be altered in a program. Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined. They are also called as literals.
Constant in C language can be declared in two ways:
- 1. Using ‘const’ keyword
- 2. Using #define pre-processor
Format of declaring constant using ‘const’ keyword:
- const ConstType ConstName = ConstValue;
- For example: const float PI=3.1416;
Format of declaring constant using #define pre-processor:
- #define ConstName ConstValue
- For example: #define PI 3.1416
Strings: A string is an array of characters ended with a null character(\0). This null character indicates that string has ended. Strings are always enclosed with double quotes(“ “).
Let us see how to declare String in C language−
- char string[20] = {‘s’,’t’,’u’,’d’,’y’, ‘\0’};
- char string[20] = “demo”;
- char string [] = “demo”;
Special symbols: The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose.[] () {}, ; * = #
- Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts.
- Parentheses(): These special symbols are used to indicate function calls and function parameters.
- Braces{}: These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement.
- comma (, ): It is used to separate more than one statements like for separating parameters in function calls.
- semi colon(; ): It is an operator that essentially invokes something called an initialization list.
- asterick (*): It is used to create pointer variable.
- assignment operator: It is used to assign values.
- pre processor(#): The pre-processor is a macro processor that is used automatically by the compiler to transform your program before actual compilation.
Operators: Operators will be discussed in the next lesson.
Lesson Evaluation-
Knowledge Based Questions:
- a. What is data type?
- a. What is modifier?
- a. What is token?
- a. What is keyword?
- a. What is identifier?
- a. What is variable?
- a. What is local variable?
- a. What is global variable?
- a. What is constant?
- a. What is string?
Comprehension Based Questions:
- b. ‘There are some rules for declaring a variable’-explain.
- b. When long integer is used instead of integer? Explain.
- b. In C language, is “int roll@no;” valid or invalid? Explain.
- b. What do you understand by float type? Write down with example.
- b. Write down the differences between variable and constant?
- b. Write down the advantages of using constant in program.
Creative Questions:
Multiple Choice Questions:
Written by,
- Mizanur Rahman (Mizan)
- Lecturer of ICT, Shaheed Bir Uttam Lt. Anwar Girls’ College , Dhaka Cantonment
- Author at www.edupointbd.com
- Software Engineer at mands IT
- Former Lecturer of ICT, Cambrian College, Dhaka
- Email: mizanjust@gmail.com
- Cell: 01724351470