Integers क्या हैं
In this page:
let name = 42 // Int
let name: Int8 = value // sized: Int8, Int16, Int32, Int64
let result = a + b // + - * / %
Declaring Integers
Int type whole numbers represent करता है और Swift में integer values के लिए default choice है, whole-number literals से अपने आप infer किया गया।
उदाहरण: Declaring Integers
// Declare a constant `apples`, set to `5`
let apples = 5
// Declare a constant `oranges`, set to `3`
let oranges = 3
// Print "Total fruit: \(apples + oranges)" to the console
print("Total fruit: \(apples + oranges)")
Login to try C/C++/Java/PHP code in the editor
Integer Arithmetic
Swift integers पर standard arithmetic operators support करता है: addition, subtraction, multiplication, integer division, और remainder operator।
उदाहरण: Integer Arithmetic
// Declare a constant `a`, set to `17`
let a = 17
// Declare a constant `b`, set to `5`
let b = 5
// Print "\(a) / \(b) = \(a / b), remainder \(a % b)" to the console
print("\(a) / \(b) = \(a / b), remainder \(a % b)")
Login to try C/C++/Java/PHP code in the editor
Sized Integer Types
Int से आगे, Swift explicitly sized types प्रदान करता है जैसे Int8, Int16, Int32, और Int64 जब आपको control करना हो कि एक value बिल्कुल कितनी memory उपयोग करती है, जैसे specific binary formats के साथ interface करते समय।
9_000_000_000 जैसे numeric literals में underscores compiler द्वारा नज़रअंदाज़ किए जाते हैं और सिर्फ readability में मदद करते हैं।उदाहरण: Sized Integer Types
// Declare a constant `small` (a `Int8`), set to `100`
let small: Int8 = 100
// Declare a constant `big` (a `Int64`), set to `9_000_000_000`
let big: Int64 = 9_000_000_000
// Print "Int8: \(small), Int64: \(big)" to the console
print("Int8: \(small), Int64: \(big)")
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
Intहमेशा 32-bit है; modern platforms परInt64-bit है, native word size से मेल खाते हुए। Int8जैसे fixed-size integer type को overflow करना और silent wraparound के बजाय एक runtime crash से आश्चर्यचकित होना।- किसी expression में
IntऔरUInt(याIntऔरDouble) को पहले explicitly convert किए बिना सीधे mix करना।
Intdefault integer type है और platform के native word size से मेल खाता है (modern systems पर 64-bit)।- Swift में
Int8,Int16,Int32,Int64जैसे sized variants भी हैं, औरUIntजैसे unsigned versions। - Arithmetic operators (+, -, *, /, %) integers पर उम्मीद के अनुसार काम करते हैं।
- Integer overflow checked है और runtime पर trap करता है चुपचाप wrap करने के बजाय, जब तक आप
&+जैसे overflow-safe operators उपयोग न करें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: