A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guidelines that must be followed in Objective-C while defining the variable's name, the guidelines are:
- The names of variables may also include underscores, letters, and numbers.
- Names for variables can either begin with a letter or an underscore.
- Objective C is considered a case-sensitive language. As a result, upper-case and lower-case alphabets are regarded as separate.
Basic Variable Types
Following are the basic variable types in Objective-C:
S.no | Type | Description |
---|
1 | char | It is an 8-bit datatype that is one byte in size. |
---|
2 | int | It includes integers that are typically 2 or 4 bytes in size. |
---|
3 | float | It provides a single-precision floating-point value indication. |
---|
4 | double | It provides a double-precision floating-point value indication. |
---|
5 | void | It demonstrates the lack of a type. |
---|
Objective-C also supports various other types of variables like Enumeration, Array, Pointer, Structure, Union, etc.
Definition of Variable
When a variable is defined, the compiler is informed of where and how much storage space to allocate for the variable. A variable definition, which also comprises a list of one or more variables of that type, specifies a data type.
Syntax:
type var_name;
Here, the type must be a valid Objective-C data type including char, w char, int, float, double, bool or any user-defined object, etc., and the variable list may consist of one or more identifier names separated by commas. Here are several declarations that are legitimate :
int i;
char c;
float f;
double d;
Initialize the Variables
To define or initialize the variables in Objective-C, we can use the assignment = operator.
Syntax:
type var_name = value;
Example:
extern int x = 45; // declaration of x;
int x = 45; // definition and initializing x.
char y = 'y'; // the variable y has the value 'y'.
Variables with static storage lifetime are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is indeterminate for definitions without an initializer.
Variable Declaration and Memory
Declaring a variable helps the compiler know about the variable that already exists. To make a memory location more comprehensible for humans, we give it an alias using a variable. A variable must be declared for the compiler to use it while linking the program; otherwise, it is meaningless.
Anywhere we want to define a variable, we can do so by using the keyword extern. The variable can be declared more than once in the code. However, each function, file, and block of code can only have one definition of it.
Example 1:
Here is an illustration of the declaration and definition of a variable. The variables are defined and initialized both locally and globally in the following code, either inside or outside of the main() method.
ObjectiveC
// Objective-C program to illustrate the variables
#import <Foundation/Foundation.h>
// Variable declaration
extern int p, q;
extern int r;
int main ()
{
// Variable definition
int p, q;
int r;
// Actual initialization
p = 7;
q = 5;
r = p * q;
NSLog(@"value is : %d \n", r);
return 0;
}
Output:
value is : 35
Explanation: Here, we declare int-type extern variables. In Objective-C, the extern keyword enables us to declare variables worldwide. Then in the main() method, three int-type variables, p, q, and r, are defined. Then initializing r=p+q, p=10, and q=20, and print the output on the console.
Example 2:
ObjectiveC
// Objective-C program to illustrate the variables
#import <Foundation/Foundation.h>
// Variable declaration:
extern float f;
int main ()
{
// Variable definition:
float f;
// Actual initialization
f = 70.0/4.0;
NSLog(@"value of f : %f \n", f);
return 0;
}
Output:
value of f : 17.500000
Explanation: Here, we declare int-type extern variables. In Objective-C, the extern keyword enables us to declare variables worldwide. Now, in the main() method, int-type variable f is defined. Then initialize the value of f and print output on the console.
Lvalues and Rvalues
In the Objective C, there are two types of expressions.
- lvalue-Expressions that make a memory location reference are known as "lvalue" expressions. An assignment's left or right side may both have a lvalue.
- rvalue-The term "rvalue" refers to a data value that is kept in memory at a specific address. An rvalue can occur on the right side of an assignment statement but not the left because it is an expression that cannot have a value assigned to it.
Since variables are lvalues, they can be found on an assignment's left side. Since numerical literals are rvalues, they cannot be assigned and cannot be used in left-side clauses.
Example:
int x = 1;
Similar Reads
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 min read
Pointers in Objective-C
In Objective-C, Pointers are the variables that hold the memory address of another variable. You must have declared the pointer variable before its use. The size of the pointer depends on the architecture of the system. The pointer variable can be defined as a char, int, float, double, or any other
5 min read
Operators in Objective-C
The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using vari
11 min read
Structures in Objective-C
Objective-C is an object-oriented programming language that adds small talk-style messaging to the C programming language. Follows a bottom-up programming approach. It incorporates concepts from both procedural and object-oriented programming languages. Objective-C support structures. So in this art
5 min read
While loop in Objective-C
Just like other programming languages Objective-C also supports while loop. While loop is a loop that is used to repeat a statement or a group of statements till the given condition is true. Every time while loop checks the condition before executing its body. Or we can say that we can use a while l
3 min read
Polymorphism in Objective-C
One of the fundamental concepts in object-oriented programming is polymorphism. It enables objects to take on a variety of forms depending on their context. In Objective-C, polymorphism allows a class to have multiple behaviors depending on the type of data it is acting on. It is one of the key char
4 min read
Posing in Objective-C
In Objective-C, posing is a technique that allows a class to wholly replace another class within a program. The replacing class is said to "pose as" the target class. All messages sent to the target class are instead received by the posing class. This article focuses on discussing posing. What is Po
5 min read
Typedef in Objective C
A typedef allows us to define a data type with a new name, for example, typedef unsigned char CAT. Now from now, onwards CAT can be used as an abbreviation for unsigned char, for example, CAT num1, num2;. Or in other words, we can say that typedef is a keyword that is used to assign the new name to
3 min read
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read