Variables, types, and arithmetic · משתנים, סוגים, וחשבון
Every value has a type
- In C, every variable has a type that says what kind of value it holds.
- The three you use most:
int(a whole number),double(a number with a decimal point), andchar(a single character). - You must give the type when you create the variable:
int n = 5;ordouble pi = 3.14;.
לכל ערך יש סוג
- ב-C, לכל משתנה יש סוג המגדיר מה סוג הערך שהוא מחזיק.
- שלושת הסוגים שהכי משתמשים בהם:
int(מספר שלם),double(מספר עם נקודה עשרונית), וchar(תוו יחיד). - עליך להגדיר את הסוג בעת יצירת המשתנה:
int n = 5;אוdouble pi = 3.14;.
Doing arithmetic
- C does math with
+,-,*, and/, just like a calculator. *and/happen before+and-. Use brackets( )to change the order.- The
%operator gives the remainder of a division:17 % 5is2.
ביצוע חישובים אריתמטיים
- C מבצע מתמטיקה עם
+,-,*ו-/, בדיוק כמו מחשבון. *ו-/מתבצעים לפני+ו--. השתמש בסוגריים( )כדי לשנות את הסדר.- המפעיל
%נותן את ה-שארית של חלוקה:17 % 5הוא2.
Integers divide differently
- When you divide two
ints, C throws away the fraction:7 / 2is3, not3.5. - To keep the fraction, at least one side must be a
double.7 / 2.0is3.5. - You can cast an
intto adoublewith(double):(double)sum / count.
חלוקה של מספרים שלמים
- כאשר מחלקים שני
int, C מושלך את השבר:7 / 2הוא3, ולא3.5. - כדי לשמור על השבר, צד אחד לפחות חייב להיות
double.7 / 2.0הוא3.5. - ניתן להמיר (cast)
intל-doubleבאמצעות(double):(double)sum / count.
Reading input
scanfreads typed input into a variable.scanf("%d", &n);reads oneintinton.- Note the
&beforen:scanfneeds the address of the variable so it can fill it in. - In this playground, Run first asks you for the input, one value per line; Check answer feeds the example input for you.
קריאת קלט
scanfקורא קלט מקלדת למשתנה.scanf("%d", &n);קורא תא אחדintלתוךn.- שימו לב ל-
&לפניn:scanfזקוק לכתובת של המשתנה כדי למלא אותה. - במחולל זה, Run (הפעל) תחילה מבקש ממך קלט, ערך אחד בכל שורה; Check answer (בדוק תשובה) מזין את דוגמת הקלט עבורך.
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("%d\n", a / b); // 3 (integer division)
printf("%.1f\n", a / 2.0); // 3.5 (one side is a double)
printf("%d\n", a % b); // 1 (remainder)
return 0;
}
Common mistakes
- Declare a variable with its type:
int n;. - Integer division truncates:
7 / 2is3, not3.5.
טעויות נפוצות
- הכרז משתנה עם סוגו:
int n;. - חלוקה של מספרים שלמים חותכת שברים:
7 / 2הוא3, ולא3.5.
Now you try
- Use
/and%for whole-number math, and adouble(or a cast) when you need a fraction. - For the function tasks, do not write a
main— the checker provides one. - Press Run to compile and run, then Check answer.
כעת תנסו בעצמכם
- השתמש ב
/וב%לחישובים של מספרים שלמים, והשתמש בdouble(או בהמרה/ cast) כשאתה זקוק לשבר. - לעבודות פונקציה, אל תכתב
main— הבוחן מספק אותו. - לחץ על Run כדי להרכיב ולהריץ, ולאחר מכן לחץ על Check answer.
Variables hold a value · משתנים מחזיקים ערך
A variable is a named box of a fixed type; assigning replaces it. · משתנה הוא תיבה בעלת שם מסוג קבוע; הצבה מחליפה אותה.
Read one integer n with scanf. A box holds 6 items. Print two lines exactly: Full boxes: then the value of n / 6, and Left over: then the value of n % 6. · קראו מספר שלם אחד n באמצעות scanf. תיבה מחזיקה 6 פריטים. הדפיסו שתי שורות בדיוק: Full boxes: ולאחר מכן את הערך של n / 6, ולאחר מכן Left over: ולאחר מכן את הערך של n % 6.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete double average3(int a, int b, int c) so it returns the average of the three as a double. Watch out for integer division — divide by 3.0, not 3. Do not write a main. · השלם double average3(int a, int b, int c) כך שיחזיר את הממוצע של השלושה כ-double. הקפד על חלוקת מספרים שלמים — חלק ב-3.0, לא ב-3. אל תכתב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write double hyp(double a, double b) that returns the hypotenuse length: the square root of a*a + b*b. Use sqrt from <math.h>. Do not write a main. · כתוב double hyp(double a, double b) שמחזיר אורך היתר: שורש ריבועי של a*a + b*b. השתמש ב-sqrt מ-<math.h>. אל תכתב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.