C language Declaration and Data-types

In this lesson we are learning about:
1. Types of tokens in C language
2. C Language delimiters
3. C Identifiers
4. C Constants
5. C Variables
6. C Variable Declaration and Initialization
7. C variable Defining Rules
8. C Data types
9. C Type Modifiers
10. C Type Conversion
11. C Constant and Volatile Variable or type qualifier

1. Types of tokens in C language

Token is the smallest unit of a program and compiler identifies them as token.
Below are the types of Tokens available in C language.
1. Keywords: They are the special words that have been reserved by compiler and has special meaning for each word.
2. Variables: They are the name given by the programmer for a memory location to store a value.
3. Constants: They are assigned to the variables.
4. Operators: They are different types of expressions that are used in C.
5. Strings: They are sequence of characters.

2. C Language delimiters

Below is the list of delimiters that are used in C.
Colon 			: 		Used to define a label [for goto].
Semi-colon		;		Used as end of statement.
Parentheses		()		Used in expressions and functions.
Square Braces	        []		Used to declare an array.
Curly Braces	        {}		Used to provide the scope of set of statements.
Hash			#		It is a pre-processor directive.
Comma			,		variable separator.

3. C Identifiers

Identifier is a name given to a variable, function, structure, union, pointer, and an array. Generally, if we give a symbolic name to any of the above element is called as an Identifier.

4. C Constants

C Constants are the values applied to a variable, and it will not change during the execution of a program.
C Constants:
Numeric Constants
1. Integer Constant. Ex:  23, 34, -25
2. Floating or Real constant. Ex: 12.34, -54.32
Character Constant
1. Single Character Constant. Ex: ‘a’, ‘b’, ‘c’
2. String Constant. Ex: “Hello”, “world”, “124”
Below is an example for different types of constants in C:
#include<stdio.h>

int main()
{
	int iNum = 10;
	float fNum = 23.45;
	char c = 'a';
	double dNum = 123.4567;
	char string[30] = "ProDeveloperTutorial";

	printf("Int = %d, Float = %f, Char = %c, Double = %5.2f String = %s\n", iNum, fNum, c, dNum, string);

	return 0;
}
gcc c_constants.c -o constants.out
./constants.out
Output:
Int = 10, Float = 23.450001, Char = a, Double = 123.46 String = ProDeveloperTutorial

5. C Variables

A C Variable is a name given to a memory location to store the value. Declaring a variable will help C compiler to know the type of variable, it’s scope and memory required to store the values.
The value assigned to a variable can be changed during the execution of the program.
Example:
int age;
Here age is a variable name of type int. Hence compiler will allocation 4 bytes of storage in the memory and will give the name as “age” to that memory location.

6. C Variable Declaration and Initialization

Declaration means assigning a name to a memory location. Any variable name in C, before using it, should be declared.
Variable declaration should be done in the declaration section of a program. Usually it will be in the starting of the function after opening braces for local variables. For global variables it will be after the inclusion of Header files.
Declaring a variable will ensure 2 things to the compiler:
1. The data type of the variable.
2. Name of the variable.
Syntax:
data_type variable_name;
Ex:
int num;
Initializing a variable:
A variable after declaration can be initialized using assignment “=” operator.
Syntax:
	Variable_name = constant;
Or
data_type variable_name = constant;

	Example:
	
int num = 10;

marks = 30;

As you can see above, the value from the right [30] is assigned to left [marks].

Multiple assignment:

A same constant value can be assigned to multiple variables.

Example
	num_1 = num_2 = num_3 = 45;
Here num_1, num_2, num_3 will be assigned with the value 45.
Dynamic Initialization:
Initialization of a variable at run time is called as Dynamic Initialization.
Example to show all the above 3 types of initialization:
#include<stdio.h>

int main(int argc, char const *argv[])
{
	int marks_1;
	int marks_2 = 20;
	int marks_3, marks_4;

	int total;

	marks_1 = 40;

	marks_3 = marks_4 = 50;

	total = marks_1 + marks_2 + marks_3 + marks_4;

	printf("marks_1 = %d \n", marks_1 );
	printf("marks_2 = %d \n", marks_2 );
	printf("marks_3 = %d \n", marks_3 );
	printf("marks_4 = %d \n", marks_4 );
	printf("Total = %d \n", total );


	return 0;
}
gcc variable_initialization.c –o variable_initialization
./variable_initialization
Output:
marks_1 = 40
marks_2 = 20
marks_3 = 50
marks_4 = 50
Total = 160

7. C variable Defining Rules

Below are the rules to define a variable:
1. A variable name should start with a alphabet, or underscore.
2. There cannot be any space between the variable name.
3. There cannot be any other special character other then “_” underscore in a variable name.
4. Variable name should not be a C keyword.
5. Variable name can be combination of alpha numeric characters, but it SHOULD NOT start with a digit.

Example C program to show variable names:

#include<stdio.h>

int main()
{
	int IntegerVariable_1 = 340;
	float float_variaBLE = 120.32;
	char name[30] = "ProDeveloperTutorial";

/* Wrong variable names 
*	int break = 10;
*	int 1_value = 29;
*	int %^&;
*/

	return 0;
}

8. C Data types:

As we know from the above article, every C variable is associated with a data type.
A data type will help a compiler to decide how much memory to be allocated to that variable during compile time.
C data-types can be classified into 3 types:
Primary Data-type:
1. int
2. float
3. char
4. double
Derived Data-type:
1. Pointer
2. Functions
3. Arrays
User Defined Data-type:
1. Structure
2. Union
3. Enumeration
4. typedef
The basic data types can be combined with type modifiers to expand or shrink the number of range a variable can store.
Every data-type will be having a predefined internal storage space.
Below is the program to show the default storage space of basic data-types:
#include<stdio.h>

int main(int argc, char const *argv[])
{
	printf("The size of char  	= %lu\n", sizeof(char) );
	printf("The size of short  	= %lu\n", sizeof(short) );
	printf("The size of int  	= %lu\n", sizeof(int) );
	printf("The size of long  	= %lu\n", sizeof(long) );
	printf("The size of double  	= %lu\n", sizeof(double) );
	printf("The size of long double  = %lu\n", sizeof(long double ) );

	return 0;
}
Output:
The size of char  	= 1
The size of short  	= 2
The size of int  	= 4
The size of long  	= 8
The size of double  	= 8
The size of long double  = 16

9. C Type Modifiers

The keywords “signed” “unsigned” “short” and “long” are type modifiers in C.
These modifiers can be applied to basic data type to change the meaning of the basic data type.
All the basic data types by default are signed data types.
For example:
An “int” data type is a signed data type and can hold the value “-32768 to 32767”, hence an “unsigned int” can hold a value “0 to 65535”.
Hence an unsigned data type can only hold a positive integer. It will not hold an negative integer values.

10. C Type Conversion:

In C, when we do operations on two different data types, the resulting type will be automatically converted to the higher data type available.
Example program shows how type conversion takes place:
#include<stdio.h>

int main(int argc, char const *argv[])
{
	int num_1 = 67;
	int num_2 = 5;
	float num_3  = 5;

	/*Division of int with int the result will be int*/
	printf("Int with Int division = %d \n", (num_1 / num_2) );

		/*Division of int with float the result will be float*/
	printf("Int with Float division = %f \n", (num_1 / num_3) );

		/*Division of int with int type casted with float the result will be float*/
	printf("Int with Int type casted with float division = %f \n", ((float)num_1 / num_2) );
	return 0;
}
Output:
Int with Int division = 13
Int with Float division = 13.400000
Int with Int type casted with float division = 13.400000

11. C Constant and Volatile Variable or type qualifiers.

The keywords “const” and “volatile” are also called as type qualifiers.
const keyword will make a variable constant. Once the value is assigned to it, it cannot be changed. Hence for a constant variable, the value should be initialized during the declaration of that constant variable.
“volatile” keyword will make a variable to change the value anytime. By default all the variable will be a “volatile” varaibles.
Example of constant and volatile:
#include<stdio.h>

int main(int argc, char const *argv[])
{
	const int num = 10;
	 // num ++ // error: cannot assign to variable 'num' with const-qualified type 'const int'

	volatile int num_2 = 24;
	num_2 ++;
	
	return 0;
}
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *