Declaration-In-C
What is a declaration?
A declaration is a statement to determine an identifier, including the name of a variable or function. Declarations tell the compiler or interpreter about the identifier’s meaning and its usage in the program.
The usage of declaration in any program directly depends on the programming language. All variables must be declared with a data type before assigning a value in C language.
Declaring an identifier assures the compiler of the existence of the identifier with its type and name so that it can compile the program without getting complete details.
- A variable declaration tells about the variable's name with its data type and value (if assigned).
- A function declaration describes the function name, return type, parameters, and how to call it.
Syntax of declaring a variable
data_type variable_name;
Variables are declared in such ways:
int x;
A variable x is declared of data type integer.
int x=5;
A variable x is declared of data type integer with value 5.
char y;
A string variable y is declared (data type: char).
char abc[10];
A string array named abc is declared, which stores 10 characters.
Syntax of declaring a function
return_type function_name(parameters);
Types of Declaration of a Function
- Without parameters and return values
- With parameters and without return values
- Without parameters and with return values
- With parameters and return values
void function();
A function named function is declared of the void type.
void function(int , int);
A void function named function is declared with two integer parameters.
int function();
It is an integer return type function named function declared without parameters and will return some output.
int function(int, int);
An integer function named function is declared with two integer parameters and will return some values.
Types of Variable Declaration
There are two types of declaration of variables in C:
- Primary Type Declaration
- User-Defined Type Declaration
a. Primary Type Declaration
A variable is declared using in-built data types such as int, float, char, etc.
We can declare variables using primary data types in the following ways:
- Declaring a single variable at a time
int a;
- Declaring multiple variables of the same data types in the same line.
int x, y=5, abc;
- Declaring multiple variables of different data types in multiple lines.
float z= 20.88;
char s;
int v;
b. User-Defined Type Declaration
In this declaration type, the user defines the data type. Type definition in C allows the definition of an identifier that shows the existing data type. It can be used again in the program to declare variables.
Commonly used user-defined data types are struct, typedef, union, etc.
- We can group multiple data items of different data types using structures. Structures are defined using the struct keyword.
- In a union, the data items share a common memory location which helps to access one member at a time.
- Typedef is a keyword used to describe the data time of the variable.
Examples of user-defined type variable declaration:
struct apple{
int red;
char fruit;
};
Structure apple is made with multiple data items of different types.
typedef char fruit;
fruit apple, banana;
We can make different data types using the typedef keyword.
A program to understand the concept of declaration in C
CODE
#include <stdio.h>
#include <conio.h>>
int max(int n1, int n2); //function declaration
int main () {
//local variable definition
int a = 100;
int b = 200;
int r;
r= max(a, b);
printf("Maximum value is : %d\n", r );
return 0;
}
int max(int n1, int n2) {
//local variable declaration
int result;
if (n1 > n2)
result = n1;
else
result = n2;
return result;
}
OUTPUT
![DECLARATION IN C](https://static.tutorialandexample.com/c/declaration-in-c1.png)
Important points to remember while using declaration in C
- Declaration of variable or function ends with a semicolon.
Examples:
void f();
int a;
- Declaring the data type or return type is necessary.
- Inside a function declaration, only data types of parameters are declared.
- Both primary and user-defined data types can be used for declaration.
- The definition is done under curly braces, but the declaration is done under parenthesis.
- When to declare and where to declare is a crucial point to remember.
- We can declare variables and functions both locally and globally.
- We must declare compound and complex statements before beginning the statements.
Why use declaration in C?
- We use declarations to name variables, functions, namespaces, and classes.
- Only after declaration can we access any name.
- The declaration is necessary to make the elements visible.
- The declaration is used with the scope, which defines the nature and properties of an object.
- We can declare a variable or function more than one type.
The declaration is not to be confused with the definition in C
- The definition in C defines the code or any data item related to the function, whereas a declaration is used to name variables, functions, etc.
- Definition allocates memory to the variable, but the declaration takes no memory.
- We can refer to definition as the code inside a function, and giving a name to the function with its return type and parameters is the declaration.
- We can define variables and functions only once, but declaration can be done multiple times.