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.
- على عكس بايثون، 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. يستدعيها الفاحص بقيم متعددة. لا تكتب 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. · اضغط تشغيل لرؤية المخرجات هنا.