טווחי סוג נתונים ומקרואים שלהם ב- C ++
לרוב הפעמים בתכנות תחרותיות יש צורך להקצות את המשתנה את הערך המקסימלי או המינימלי שסוג הנתונים יכול להחזיק אך לזכור מספר כה גדול ומדויק יוצא עבודה קשה. לכן ל- C ++ יש מאקרו מסוים לייצוג מספרים אלה כך שניתן להקצות אותם ישירות למשתנה מבלי להקליד בפועל את כל המספר.
THE
בואו נסתכל על דוגמה:
C++ #include // for int char macros #include // for float double macros #include using namespace std ; int main () { // Displaying ranges with the help of macros cout < < 'char ranges from: ' < < CHAR_MIN < < ' to ' < < CHAR_MAX < < endl ; cout < < ' n nshort int ranges from: ' < < SHRT_MIN < < ' to ' < < SHRT_MAX < < endl ; cout < < ' n int ranges from: ' < < INT_MIN < < ' to ' < < INT_MAX < < endl ; cout < < ' n long int ranges from: ' < < LONG_MIN < < ' to ' < < LONG_MAX < < endl ; cout < < ' n float ranges from: ' < < FLT_MIN < < ' to ' < < FLT_MAX < < endl ; return 0 ; }
תְפוּקָה
char ranges from: -128 to 127
nshort int ranges from: -32768 to 32767
int ranges from: -2147483648 to 2147483647
long int ranges from: -9223372036854775808 to 9223372036854775807
float ranges from: 1.17549e-38 to 3.40282e+38סוגי נתונים ומאקרו לטווח שלהם
להלן רשימה של חלק ממקרו מקרואים מסוג הנתונים:
| סוג נתונים | לָנוּעַ | מאקרו לערך MIN | מאקרו לערך מקסימום |
|---|---|---|---|
| לְהַשְׁחִיר | -128 עד +127 | Char_min | Char_max |
| CHAR קצר | -128 עד +127 | Schar_min | Schar_max |
| צ'אר | 0 עד 255 | - | Flying_max |
| קצר int | -32768 עד +32767 | Shrt_min | Shrt_max |
| int קצר לא חתום | 0 עד 65535 | - | USHRT_MAX |
| Int | -2147483648 עד +2147483647 | Int_min | Int_max |
| לא חתום | 0 עד 4294967295 | - | Uint_max |
| ארוך int | -9223372036854775808 עד +922372036854775807 | LONG_MIN | LONG_MAX |
| ארוך לא חתום | 0 עד 18446744073709551615 | - | Usong_max |
| ארוך ארוך | -9223372036854775808 עד +922372036854775807 | Ship_min | Llong_max |
| לא חתום ארוך ארוך | 0 עד 18446744073709551615 | - | Ullong_max |
| לָצוּף | 1.17549E-38 עד 3.40282E+38 | Flt_min | Flt_max |
| צף (שלילי) | -1.17549E -38 עד -3.40282E+38 | -Lt_min | -Flt_max |
| לְהַכפִּיל | 2.22507E-308 עד 1.79769E+308 | Dbl_min | Dbl_max |
| כפול (שלילי) | -2.22507E -308 עד -1.79769E+308 | -Dbl_min | -Dbl_max |
מגבלות סוג נתונים ב- C ++ מודרני
גישת המקרו לעיל למגבלות העליונות והתחתונות מסוג הנתונים היא גישת שפת ה- C הישנה שירשה על ידי C ++. אבל ל- C ++ יש גם שיטה משלה לספק למתכנתים את אותו מידע.
C ++ מציע את numeric_limits <> תבנית כיתה כחלופה מודרנית למאקרו אלה. תבנית זו מספקת גישה מוכוונת יותר לאובייקט לגישה למגבלות סוג נתונים. זה מוגדר בתוך
בואו נסתכל על דוגמה:
C++ #include #include using namespace std ; int main () { // Displaying ranges with the help of macros cout < < 'short int ranges from: ' < < numeric_limits < short int >:: min () < < ' to ' < < numeric_limits < short int >:: max () < < endl ; cout < < ' n int ranges from: ' < < numeric_limits < int >:: min () < < ' to ' < < numeric_limits < int >:: max () < < endl ; cout < < ' n long int ranges from: ' < < numeric_limits < long >:: min () < < ' to ' < < numeric_limits < long >:: max () < < endl ; cout < < ' n float ranges from: ' < < numeric_limits < float >:: min () < < ' to ' < < numeric_limits < float >:: max () < < endl ; return 0 ; }
תְפוּקָה
short int ranges from: -32768 to 32767
int ranges from: -2147483648 to 2147483647
long int ranges from: -9223372036854775808 to 9223372036854775807
float ranges from: 1.17549e-38 to 3.40282e+38מומלץ להשתמש בגישה זו כדי למצוא את הגבולות העליונים והתחתונים של סוג הנתונים במקום מקרואים מכיוון שהיא בטוחה יותר וקריאה יותר בהשוואה לגישה המבוססת על מאקרו.