codescracker


objective c

Objective-C Variables



« Previous Tutorial Next Tutorial »


A variable is simply a name given to a storage area that an Objective-C programs can manipulate. Each and every variable in Objective-C has a particular data ypes, determines the size and the layout of the variable's memory, and the range of the values that can be stored within that memory, and the set of operations which can be applied to that variable.

For variable naming instruction, refer Objective-C Basic Syntax, where there is a topic named identifier, when you can learn about the valid naming of a variable in Objective-C. And for the

Variable Definition in Objective-C

Here is the general form to define a variable in Objective-C:

type variable-name;

Or

type variable-name1, variable-name2, variable-name3, . . ., variable-nameN;

Here is an example, showing the valid variable definition in Objective-C

int num;
char ch;
float marks, total;

Variable Initialization in Objective-C

Here is the general form to initialize variable in Objective-C:

variable-name = value;

Or

variable-name1 = value1;
variable-name2 = value2;
variable-name3 = value3;
.
.
.
variable-nameN = valueN;

Here is the general form to define and initialize variable in Objective-C:

type variable-name = value;

Or

type variable-name1 = value1, variable-name2 = value2, variable-name3 = value3, ..., variable-nameN = valueN;

Here is an example, showing the valid variable definition and initialization in Objective-C:

int num = 10;
int num1=5, num2=10, num3=15;
char ch='y';
float marks=98.50;

Objective-C Variables Example

Here is a complete example program showing variable declarations and definitions in Objective-C:

/* Objective-C Variables - Example Program */
		
#import <Foundation/Foundation.h>
extern int a, b;
extern int c;
extern float f;
int main()
{
	int a, b;
	int c;
	float f;
	a = 10;
	b = 20;
	c = a + b;
	NSLog(@"value of c: %d \n", c);
	f = 70.0/3.0;
	NSLog(@"value of f: %f \n", f);
	return 0;
}

When the above code is compile and executed, it would produce the following result:

2013-06-03 13:18:37.932 demo[14019] value of c: 30 
2013-06-03 13:18:37.932 demo[14019] value of f: 23.333334 

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test