C basics: main, printf, and functions · בסיסי C: main, printf, ופונקציות
C is a compiled language
- C is one of the oldest and most important languages. Linux, databases, and even Python are built with C.
- Unlike Python, C is compiled: the computer first turns your code into a program, then runs it.
- C is strict: every statement ends with a semicolon
;, and every variable needs a type.
C היא שפת מורכבת
- C היא אחת השפות הישנות והחשובות ביותר. לינוקס, בסיסי נתונים, ואף פיתון נבנו בעזרת C.
- לעומת Python, C מועבר: המחשב מוּרֵם תחילה את הקוד שלך לתוכנית, ולאחר מכן מריץ אותה.
- C היא מחמירה: כל הודעה מסתיימת ב-פסיק-נקודה
;, וכל משתנה דורש סוג.
The C skeleton
- Every C program starts in a function called main. Your code goes inside main.
#include <stdio.h>gives youprintf, which prints text.return 0;at the end ofmainmeans "the program finished without an error".
השלד של C
- כל תוכנת C מתחילה בפונקציה הנקראת main. הקוד שלך נמצא בתוך main.
#include <stdio.h>מספק לךprintf, שמדפיס טקסט.return 0;בסוףmainפירושו "התוכנית הסתימה ללא שגיאה".
#include <stdio.h>
int main(void) {
printf("C runs from inside main.\n");
int year = 11;
printf("Grade %d\n", year);
return 0;
}
printf and types
printfprints text. The\ninside the text means "start a new line".- To print a value, use a placeholder:
%dfor anint,%ffor adouble,%cfor one character. printf("Grade %d\n", year);puts the value ofyearwhere the%dis.- Every variable needs a type first:
int year = 11;ordouble pi = 3.14;.
printf וסוגים
printfמדפיס טקסט. ה-\nבתוך הטקסט פירושו "להתחיל שורה חדשה".- כדי להדפיס ערך, השתמש ב-מקום חלופי:
%dעבורint,%fעבורdouble,%cעבור תוו אחד. printf("Grade %d\n", year);מציב את הערך שלyearבמקום בו מופיע ה-%d.- לכל משתנה יש צורך בסוג תחילה:
int year = 11;אוdouble pi = 3.14;.
Common mistakes
- Every statement ends with
;, andmainreturnsint(return 0;). printfneeds the right format (%d,%f) and\nfor a new line.
טעויות נפוצות
- כל הודעה מסתיימת ב
;, וה-mainמחזירint(return 0;). printfזקוק לפורמט המתאים (%d,%f) ול\nלשורה חדשה.
Now you try
- Each task pre-fills the code you need. Write inside
main, or complete the function shown. - Some tasks ask for a function only — do not write your own
main; the checker adds one. - Press Run to compile and run, then Check answer. Your code compiles and runs on the server.
כעת תנסו בעצמכם
- בכל משימה המערכת ממלאה מראש את הקוד הנדרש. כתוב בתוך
main, או השלם את הפונקציה המוצגת. - חלק מהמשימות מבקשים פונקציה בלבד — אל תכתוב
mainמשלך; הבוקר יוסיף אותו. - לחץ על Run כדי למסוף ולהריץ, ולאחר מכן לחץ על Check answer. הקוד שלך מוסף ומורץ על השרת.
Compile then run · לקומפיל ולאחר מכן להריץ
C source is compiled to a program, which you then run. · קוד מקור ב-C מתקומפל לתוכנית, אותה תריצים לאחר מכן.
Inside main, use printf(...) to print exactly Hello, C! on its own line. · בתוך main, השתמשו בprintf(...) כדי להדפיס בדיוק Hello, C! בשורה עצמאית.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete the function int square(int n) so it returns n * n. The checker calls it with several values. Do not write a main — the checker provides one. · שלים את הפונקציה int square(int n) כך שתחזיר n * n. הבודק (checker) יקרא אותה עם ערכים שונים. אל תכתוב main — הבודק מספק אותו.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Inside main, declare int grade = 11;. Then use printf to print two lines: first Hello!, then Grade 11 — print the number with a %d placeholder, not by typing 11. · בתוך main, הגיערו את int grade = 11;. לאחר מכן השתמשו בprintf כדי להדפיס שתי שורות: תחילה Hello!, ואז Grade 11 — הדפיסו את המספר עם מקום חסר %d, ולא על ידי כתיבה ישירה של 11.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.