• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips

data type

NSLog datatype formatting option in Objective-C

March 4, 2013 By Ravi Shankar Leave a Comment

201303040743.jpg

NSLog in Objective-C is function call that is useful during debugging of a program. A typical NSLog example is

NSLog(@” How to use NSLog”);

The above statement prints the message ” How to use NSLog ” in console window. If you want to print the value of Objective-C variable or data type then you need to pass the relevant formatting parameter depending on data type. We have already seen different datatype and qualifiers in Objective-C, below is example Objective-C program with NSLog formatting option for each datatypes.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])

{

int i = 5;

float f = 5.3;

double d = 66.76;

long int li = 22;

short int si = 12;

char c = ‘W’;

signed int sint = 34;

unsigned int uint = –23;

int o = 024;

int h = 0xAD;

long long ll = 45;

long double lf = 34.5;

unsigned long long ull = 12;

NSString *msg = @”NSLog Message”;

  

  

@autoreleasepool {

NSLog(@” print int %d”, i);

NSLog(@” print float %f”, f); // use %e for exponential

NSLog(@” print double %f”, d); // use %e for exponential

NSLog(@” print long int %li”, li);

NSLog(@” print short int %i”, si);

NSLog(@” print char %c”, c);

NSLog(@” print signed int %i”, sint);

NSLog(@” print unsigned int %u”, uint);

NSLog(@” print octal %o”, o);

NSLog(@” print hexadecimal %X”, h);

NSLog(@” print long long %lld”, ll);

NSLog(@” print long double %Lf”, lf);

NSLog(@” print unsigned long long %llu”, ull);

NSLog(@” print Object %@”, msg);

  

  

  

}

return 0;

}

The console output of the above Objective-C program is shown below

2013-03-04 07:25:10.187 NSLog Example[362:303] print int 5

2013-03-04 07:25:10.189 NSLog Example[362:303] print float 5.300000

2013-03-04 07:25:10.189 NSLog Example[362:303] print double 66.760000

2013-03-04 07:25:10.190 NSLog Example[362:303] print long int 22

2013-03-04 07:25:10.190 NSLog Example[362:303] print short int 12

2013-03-04 07:25:10.190 NSLog Example[362:303] print char W

2013-03-04 07:25:10.191 NSLog Example[362:303] print signed int 34

2013-03-04 07:25:10.191 NSLog Example[362:303] print unsigned int 4294967273

2013-03-04 07:25:10.192 NSLog Example[362:303] print octal 24

2013-03-04 07:25:10.192 NSLog Example[362:303] print hexadecimal AD

2013-03-04 07:25:10.193 NSLog Example[362:303] print long long 45

2013-03-04 07:25:10.193 NSLog Example[362:303] print long double 34.500000

2013-03-04 07:25:10.194 NSLog Example[362:303] print unsigned long long 12

2013-03-04 07:25:10.194 NSLog Example[362:303] print Object NSLog Message

int %d or %i

float %f or %e

double %f or %e

long int %li

short int %i

char %c

signed int %i

unsigned int %u

octal %o

hexa %X

long long %lld

long double %Lf

unsigned long long %llu

object %@

Filed Under: Apple, Develop, Programming Tagged With: Apple, data type, formatting option, NSLog, Objective C

Different data types in Objective-C

March 3, 2013 By Ravi Shankar Leave a Comment

Objective-C like any other programming languages has different data types like int, float, double, char and id. Data types are used for specifying the kind of data that is being stored in a variable. For example, the below code stores a single character to a variable “flag”

char flag = ‘1’

201303031336.jpg

Data type – char

The Objective-C Char data type can store single character of letter or number or special characters. This is done by enclosing the character with in single quotes.

var example1 = ‘W’ (store letter)   

var example2 = ‘3’ (store number)

var example3 = ‘:’ (store special character)

var example4 = ‘/n’ (backlash character is a special character)

Data type – int

int data type is used for storing positive or negative whole numbers. The range of values that can stored depends on the 32 bit or 64 bit operating system.

int count = 10

int exoctal = 036

int hex = 0xADD2

in the above example 0 preceding the number represents octal value and hex number is preceded with 0x

Date type – float & double

float is used for storing positive or negative decimal numbers.

float exfloat = 2.45

float exfloat2 = -0.345

float exfloat3 = 1.2e4

As shown in the last example, float data type can be represented in scientific notation 1.2e4 which is equivalent 1.2x10th power of 4.

double data type can store twice the range of float.

Data type – BOOL

BOOL is used for storing YES or NO i.e 1 or 0

BOOL flag = 1

Data type – id

id data type is used for storing object of any type.

id myfirstObject

Qualifiers

Qualifiers in Cbjective-C are used for increasing range of the data type and the range is system dependant. Qualifiers are placed just before the data type as shown below. The different qualifiers are long, long long, short, signed and unsigned.

Filed Under: Apple, Develop, iPhone, Programming Tagged With: Apple, data type, iphone, Objective C

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup
  • Show hidden files and folders on Mac
  • How to request a refund for apps in App Store

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy