← Back to C Course | Chapter 1: Introduction & Basics | Lesson 21 of 21

C के Fixed-Width Integers

Fixed-width integers एक exact size वाले whole-number types हैं, एक ऐसे jar की तरह जो किसी भी computer पर हमेशा exactly same मात्रा रखता है। ये programs को हर जगह same तरह behave कराते हैं।
Syntax
c
#include <stdint.h>

int32_t variable_name = value;   // also int8_t int16_t int64_t
uint32_t variable_name = value;  // also uint8_t uint16_t uint64_t

Portability Problem

int और long जैसे plain types की C standard में कोई guaranteed size नहीं है -- int आज आमतौर पर 32 bits है लेकिन पुराने systems पर 16 bits था, इसलिए एक specific width मानने वाला code किसी अलग platform पर compile होने पर चुपचाप गलत behave कर सकता है।

उदाहरण: The Portability Problem

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("int size here: %zu bytes", sizeof(int));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Signed Fixed-Width Types

<stdint.h> int8_t, int16_t, int32_t, और int64_t define करता है, हर एक guaranteed है कि हर conforming platform पर exactly उतने bits की हो, जो exactly वही है जो protocol formats, file formats, और hardware registers को चाहिए।

उदाहरण: Signed Fixed-Width Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
// Program execution starts in main()
int main() {
	// Declare id and set it to 100000
	int32_t id = 100000;
	// Print formatted text to the screen
	printf("%d", id);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Unsigned Fixed-Width Types

Unsigned counterparts uint8_t, uint16_t, uint32_t, और uint64_t same naming pattern follow करते हैं और raw byte buffers, bit flags, और ऐसी किसी भी चीज़ के लिए standard choice हैं जिसे कभी negative value represent नहीं करनी चाहिए।

उदाहरण: Unsigned Fixed-Width Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
// Program execution starts in main()
int main() {
	// Declare flags and set it to 255
	uint8_t flags = 255;
	// Print formatted text to the screen
	printf("%u", flags);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Fixed-Width Values Print करना

क्योंकि int32_t के पीछे का underlying type platform के हिसाब से बदल सकता है, printf format specifiers जैसे %d इसके लिए strictly portable नहीं हैं -- <inttypes.h> PRId32 और PRIu64 जैसे macros देता है जो हर fixed-width type के लिए सही specifier में expand होते हैं।

उदाहरण: Printing Fixed-Width Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
#include <inttypes.h>
// Program execution starts in main()
int main() {
	// Declare id and set it to 42
	int32_t id = 42;
	// Print formatted text to the screen
	printf("%" PRId32, id);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Fixed-Width Types कब इस्तेमाल करें

Exact size मायने रखने पर fixed-width types इस्तेमाल करें -- binary file formats, network packets, embedded registers -- लेकिन everyday counters और loop variables के लिए, plain int simpler रहता है और अक्सर वही है जिसे platform का CPU सबसे efficiently handle करता है।

उदाहरण: When to Use Fixed-Width Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include fixed-width integer types
#include <stdint.h>
// Program execution starts in main()
int main() {
	// Declare filePacketSize and set it to 1024
	int32_t filePacketSize = 1024;
	// Declare loopCounter and set it to 0
	int loopCounter = 0;
	// Print formatted text to the screen
	printf("%d %d", filePacketSize, loopCounter);
	// Return 0 to signal that the program finished successfully
	return 0;
}
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 bits है, जब int32_t width guarantee करता है।
  2. int32_t या uint8_t इस्तेमाल करने से पहले #include <stdint.h> भूल जाना।
  3. एक fixed-width value को <inttypes.h> के macros जैसे PRId64 की बजाय %d से print करना।
चैप्टर सारांश
  • यह chapter C, इसका इतिहास, एक compiler कैसे setup करें, और एक पहले program की structure introduce करता है।
  • आप syntax, statements, comments, keywords, और identifiers सीखते हैं, जो हर C program के basic rules बनाते हैं।
  • यह data types, variables, constants, type conversion, और sizeof भी cover करता है, जो control करते हैं कि values कैसे store होती हैं।

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.