← Back to Swift Course | Chapter 2: Variables & Types | Lesson 4 of 8

Integers क्या हैं

Integers whole numbers हैं, जैसे 1, 2, या -7, बिना किसी fractions या decimal points के।
Syntax
swift
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

markup
// 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)")

Integer Arithmetic

Swift integers पर standard arithmetic operators support करता है: addition, subtraction, multiplication, integer division, और remainder operator।

Note: Integer division किसी भी fractional part को truncate करता है -- 17 / 5, 3 है, 3.4 नहीं।

उदाहरण: Integer Arithmetic

markup
// 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)")

Sized Integer Types

Int से आगे, Swift explicitly sized types प्रदान करता है जैसे Int8, Int16, Int32, और Int64 जब आपको control करना हो कि एक value बिल्कुल कितनी memory उपयोग करती है, जैसे specific binary formats के साथ interface करते समय।

Note: 9_000_000_000 जैसे numeric literals में underscores compiler द्वारा नज़रअंदाज़ किए जाते हैं और सिर्फ readability में मदद करते हैं।

उदाहरण: Sized Integer Types

markup
// 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)")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि Int हमेशा 32-bit है; modern platforms पर Int 64-bit है, native word size से मेल खाते हुए।
  2. Int8 जैसे fixed-size integer type को overflow करना और silent wraparound के बजाय एक runtime crash से आश्चर्यचकित होना।
  3. किसी expression में Int और UInt (या Int और Double) को पहले explicitly convert किए बिना सीधे mix करना।
चैप्टर सारांश
  • Int default 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.