Monday, August 31, 2009

data type and expression

The Objective-C programming language provides three other basic data types: float, double, and char.

A variable declared to be of type float can be used for storing floating-point numbers (values containing decimal places).The double type is the same as type float, only with roughly twice the accuracy. Finally, the char data type can be used to store a single character, such as the letter a, the digit character 6, or a semicolon.

 

In Objective-C, any number, single character, or character string is known as a constant. For example, the number 58 represents a constant integer value.The string @”Programming in Objective-C is fun.\n” is an example of a constant character string object.

type int 

(base 10)

Two special formats in Objective-C enable integer constants to be expressed in a base other than decimal (base 10)

(base 8)

the integer is considered to be expressed in octal notation that is, in base 8.

example: octal constant 0177 = value 127 (1 ×64 + 7 × 8 + 7)An integer value can be displayed in octal notation by using the  format characters  %o in the format string of an NSLog call.

(base 16)

If an integer constant is preceded by a 0 and a letter x (either lower case or uppercase), the value is considered to be expressed in hexadecimal (base 16) notation.

example: rgbColor = 0xFFEF0D;         

NSLog (“Color is %#x\n”, rgbColor);

 

 

Type float

Floating-point constants can also be expressed in so-called scientific notation

You can use a variable declared to be of type float to store values containing decimal places.

2.25 × 10-3 or 0.00225 or 2.25e-3

scientific notation, the format characters %e should be specified  The format characters %g can be used to let NSLog decide
whether to display the floating-point value in normal floating-point notation or in scientific notation.

This decision is based on the value of the exponent: If it’s less than –4 or greater than 5 %e (scientific notation) format is used; otherwise, %f format is used.

Type double

The type double is similar to the type float, but it is used whenever the range provided by a float variable is not sufficient.Variables declared to be of type double can store roughly twice as many significant digits as can a variable of type float. Most computers
represent double values using 64 bits.

Type char

A character constant is formed by enclosing the character within a pair of single quotation marks. So ’a’, ’;’, and ’0’ are all valid examples of character constants.

//program to store and print value

 

#import  <Foundation/Foundation.h>

int main(int argc ,char *argv[])
{
NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];

int intVar=101;
float floatVar=234.56;
double doubleVar=8.44e6;
char charVar ='w';

NSLog (@"integer value is %i",intVar );

NSLog (@"float value is %f", floatVar);

NSLog (@"double value is %e",doubleVar );

NSLog (@"double value is %g",doubleVar );

NSLog (@"char value is %c",charVar );

return 0;
[pool drain];
}

GNUmakefile

include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME=LogTest
LogTest_OBJC_FILES=prog401.m
include $(GNUSTEP_MAKEFILES)/tool.make

Output

Fullscreen capture 8312009 94802 AM.bmp

source code download

No comments:

Post a Comment