C के Fixed-Width Integers
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
intइस्तेमाल करना और मान लेना कि यह हर जगह 32 bits है, जबint32_twidth guarantee करता है।int32_tयाuint8_tइस्तेमाल करने से पहले#include <stdint.h>भूल जाना।- एक 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 होती हैं।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers