A variable is a storage area that our programs can manipulate. The variables in Objective-C contain a specific type that defines their size and layout. They also include the range of the values and operations that we can apply.
We must specify the name of the variable in Objective-C by following these rules:
Type | Details |
| It is a datatype with the size of one byte or 8 bits. |
| It contains the common size of integers of either 2 or 4 bytes. |
| It indicates the floating-point value with single precision. |
| It indicates the floating-point value with double precision. |
| It shows the absence of a type. |
Below are some examples of the declaration of variables on Objective-C:
#include <stdio.h>// Declaring the variable:extern int x, y;extern int z;extern float ans;int main () {// Defining the variable:int x,y;int z;float ans;/*Initializing the variable */x = 30;y = 6;z= x * y;printf("The answer is: %d \n", z);ans = 2.0/4.0;printf("The answer is: %f \n", ans);return 0;}
,
). In this line, we declare three variables of the same integer type: x
, y
, and z
.a
of the char
data type.b
of the float
data type.c
of the double
data type without any initialization.We can use the assignment =
operator in Objective-C to define or initialize the above variables.
Variable declaration helps inform the compiler about the existing variable. We use a variable as an alias for a memory location to make it understandable for humans. Compilers need the declaration of a variable while linking the program, and it has its meaning only at the compile time.
We can use the keyword extern
to declare a variable at any place. We can declare the variable more than one time in the code. However, we can define it only one time in a function, file, and block of code.
Here is an example of variable declaration and definition. In the following code, we define and initialize the variables locally and globally, i.e., inside or outside of the main()
function.
#include <stdio.h>// Declaring the variable:extern int x, y;extern int z;extern float ans;int main () {// Defining the variable:int x,y;int z;float ans;/*Initializing the variable */x = 30;y = 6;z= x * y;printf("The answer is: %d \n", z);ans = 2.0/4.0;printf("The answer is: %f \n", ans);return 0;}
extern
variables of int
and float
type. The extern
keyword in Objective-C helps us declare variables globally.int
and float
types, x
, y
, and z
, inside the main()
method.x=30
, y=6
, z=x*y
, and print on the console.Note: There are also some rules about the declaration of functions. According to these, we can specify the name of the function at the time of declaration, and it can be defined properly anywhere else.
There are two types of expressions in Objective-C:
#include <stdio.h>//Declaring the functionint checkStatus();// Main functionint main() {//Calling the functionint status = checkStatus();}//Defining the functionint checkStatus() {printf("I love Educative!");return 1;}
checkStatus()
method and return a rvalue
to the variable status act as lvalue
.checkStatus()
function.Compound data types are those which can be constructed using primitive data types. Some compound data types in Objective-C are as follows:
Array
or List
Pointer
Structure
, etc.An array is a collection of unordered values of the same data types. The variable definition helps specify the location and size of the variable. It contains the type
and alias to array type
, followed by size
in square brackets [
,]
.
type arrayName[arraySize];
Note: We must use the correct primitive data type for the variable declaration. An array
arrayName
contains the consecutive memory locations of the same data types.