مقدمة في التعامل مع المتغيرات في لغة C وأنواعها الأساسية
لغة C هي لغة مكتوبة بشكل ثابت.
هذا يعني أن أي متغير له نوع مرتبط به ، وهذا النوع معروف في وقت التجميع.
هذا يختلف تمامًا عن كيفية التعامل مع المتغيرات في Python و JavaScript و PHP واللغات المفسرة الأخرى.
عندما تقوم بإنشاء متغير في C ، يجب عليك تحديد نوع المتغير في الإعلان.
في هذا المثال نقوم بتهيئة متغيرage
مع النوعint
:
int age;
يمكن أن يحتوي اسم المتغير على أي حرف كبير أو حرف صغير ، ويمكن أن يحتوي على أرقام وشرطة سفلية ، ولكن لا يمكن أن يبدأ برقم.AGE
وAge10
هي أسماء متغيرات صالحة ،1age
ليس.
يمكنك أيضًا تهيئة متغير عند الإعلان ، وتحديد القيمة الأولية:
int age = 37;
بمجرد أن تعلن عن متغير ، يمكنك حينئذٍ استخدامه في كود البرنامج الخاص بك ، ويمكنك تغيير قيمته في أي وقت ، باستخدام=
عامل على سبيل المثال ، مثل فيage = 100;
، بشرط أن تكون القيمة الجديدة من نفس النوع.
في هذه الحالة:
#include <stdio.h>
int main(void) {
int age = 0;
age = 37.2;
printf("%u", age);
}
سيقوم المترجم بإصدار تحذير في وقت الترجمة ، وسيحول الرقم العشري إلى قيمة عدد صحيح.
الجأنواع البيانات المضمنة هيint
وchar
وshort
وlong
وfloat
وdouble
وlong double
. دعنا نتعرف أكثر على هؤلاء.
أعداد صحيحة
يوفر لنا C الأنواع التالية لتحديد قيم الأعداد الصحيحة:
char
int
short
long
في معظم الأوقات ، من المحتمل أن تستخدم ملفint
لتخزين عدد صحيح. لكن في بعض الحالات ، قد ترغب في اختيار أحد الخيارات الثلاثة الأخرى.
الchar
تُستخدم الكتابة بشكل شائع لتخزين أحرف مخطط ASCII ، ولكن يمكن استخدامها للاحتفاظ بأعداد صحيحة صغيرة من-128
إلى127
. يستغرق 1 بايت على الأقل.
int
يأخذ 2 بايت على الأقل.short
يأخذ 2 بايت على الأقل.long
يأخذ 4 بايت على الأقل.
كما ترى ، لا نضمن نفس القيم للبيئات المختلفة. ليس لدينا سوى إشارة. تكمن المشكلة في أن الأرقام الدقيقة التي يمكن تخزينها في كل نوع بيانات تعتمد على التنفيذ والبنية.
نحن نضمن ذلكshort
ليس أطول منint
. ونحن مضمونونlong
ليس أقصر منint
.
يحدد معيار مواصفات ANSI C القيم الدنيا لكل نوع ، وبفضله يمكننا على الأقل معرفة الحد الأدنى للقيمة التي يمكن أن نتوقعها تحت تصرفنا.
إذا كنت تقوم ببرمجة لغة C على Arduino ، فسيكون للوحة المختلفة حدود مختلفة.
على لوحة Arduino Uno ،int
يخزن قيمة 2 بايت تتراوح من-32,768
إلى32,767
. On a Arduino MKR 1010, int
يخزن قيمة 4 بايت تتراوح من-2,147,483,648
إلى2,147,483,647
. فرق كبير.
على جميع لوحات Arduino ،short
يخزن قيمة 2 بايت تتراوح من-32,768
إلى32,767
.long
تخزين 4 بايت تتراوح من-2,147,483,648
إلى2,147,483,647
.
أعداد صحيحة بدون إشارة
بالنسبة لجميع أنواع البيانات المذكورة أعلاه ، يمكننا الاعتماد مسبقًاunsigned
لبدء النطاق من 0 ، بدلاً من رقم سالب. قد يكون هذا منطقيًا في كثير من الحالات.
unsigned char
سوف تتراوح من0
على الأقل255
unsigned int
سوف تتراوح من0
على الأقل65,535
unsigned short
سوف تتراوح من0
على الأقل65,535
unsigned long
سوف تتراوح من0
على الأقل4,294,967,295
مشكلة الفائض
بالنظر إلى كل هذه الحدود ، قد يظهر سؤال: كيف يمكننا التأكد من أن أرقامنا لا تتجاوز الحد؟ وماذا يحدث إذا تجاوزنا الحد؟
إذا كان لديك ملفunsigned int
رقم 255 ، وقمت بزيادته ، ستحصل على 256 في المقابل. كما هو متوقع. اذا كان لديكunsigned char
رقم 255 ، وقمت بزيادته ، ستحصل على 0 في المقابل. يتم إعادة تعيينه بدءًا من القيمة الأولية المحتملة.
اذا كان لديكunsigned char
رقم 255 وقمت بإضافة 10 إليه ، ستحصل على الرقم9
:
#include <stdio.h>
int main(void) {
unsigned char j = 255;
j = j + 10;
printf("%u", j); /* 9 */
}
إذا كانت لديك قيمة موقعة ، فإن السلوك غير محدد. سيعطيك بشكل أساسي عددًا كبيرًا يمكن أن يختلف ، كما في هذه الحالة:
#include <stdio.h>
int main(void) {
char j = 127;
j = j + 10;
printf("%u", j); /* 4294967177 */
}
بمعنى آخر ، لا يحميك C من تجاوز حدود النوع. عليك أن تعتني بهذا بنفسك.
تحذيرات عند التصريح عن النوع الخطأ
عندما تقوم بتعريف المتغير وتهيئته بقيمة خاطئة ، فإن ملفgcc
المترجم (الذي ربما تستخدمه) يجب أن يحذرك
#include <stdio.h>
int main(void) {
char j = 1000;
}
hello.c:4:11: warning: implicit conversion from 'int' to
'char' changes value from 1000 to -24
[-Wconstant-conversion]
char j = 1000;
~ ^~~~
1 warning generated.And it also warns you in direct assignments:
#include <stdio.h>
int main(void) {
char j;
j = 1000;
}
But not if you increase the number using for example +=
:
#include <stdio.h>
int main(void) {
char j = 0;
j += 1000;
}
Floating point numbers
Floating point types can represent a much larger set of values than integers can, and can also represent fractions, something that integers can’t do.
Using floating point numbers, we represent numbers as decimal numbers times powers of 10.
You might see floating point numbers written as
1.29e-3
-2.3e+5
and in other seemingly weird ways.
The following types:
float
double
long double
are used to represent numbers with decimal points (floating point types). All can represent both positive and negative numbers.
The minimum requirements for any C implementation is that float
can represent a range between 10^-37 and 10^+37, and is typically implemented using 32 bits.
double
can represent a bigger set of numbers.
long double
can hold even more numbers.
The exact figures, as with integer values, depend on the implementation.
On a modern Mac, a float
is represented in 32 bits, and has a precision of 24 significant bits, 8 bits are used to encode the exponent.
A double
number is represented in 64 bits, with a precision of 53 significant bits, 11 bits are used to encode the exponent.
The type long double
is represented in 80 bits, has a precision of 64 significant bits, 15 bits are used to encode the exponent.
On your specific computer, how can you determine the specific size of the types? You can write a program to do that:
#include <stdio.h>
int main(void) {
printf("char size: %lu bytes\n", sizeof(char));
printf("int size: %lu bytes\n", sizeof(int));
printf("short size: %lu bytes\n", sizeof(short));
printf("long size: %lu bytes\n", sizeof(long));
printf("float size: %lu bytes\n", sizeof(float));
printf("double size: %lu bytes\n", sizeof(double));
printf("long double size: %lu bytes\n", sizeof(long double));
}
In my system, a modern Mac, it prints:
char size: 1 bytes
int size: 4 bytes
short size: 2 bytes
long size: 8 bytes
float size: 4 bytes
double size: 8 bytes
long double size: 16 bytes
Download my free C Handbook
More clang tutorials:
- Introduction to the C Programming Language
- C Variables and types
- C Constants
- C Operators
- C Conditionals
- How to work with loops in C
- Introduction to C Arrays
- How to determine the length of an array in C
- Introduction to C Strings
- How to find the length of a string in C
- Introduction to C Pointers
- Looping through an array with C
- Booleans in C
- Introduction to C Functions
- How to use NULL in C
- Basic I/O concepts in C
- Double quotes vs single quotes in C
- How to return a string from a C function
- How to solve the implicitly declaring library function warning in C
- How to check a character value in C
- How to print the percentage character using `printf()` in C
- C conversion specifiers and modifiers
- How to access the command line parameters in C
- Scope of variables in C
- Can you nest functions in C?
- Static variables in C
- C Global Variables
- The typedef keyword in C
- C Enumerated Types
- C Structures
- C Header Files
- The C Preprocessor