Machines only understand binary. When we write a list of human-readable instructions for a computer, machines understand only what we now call machine code. This machine code consists of only 1's and 0's.
With a special program called Compiler
, we can turn the original code into machine code.
We can judge good code according to 3 criteria:
correctness (
The
#include <stdio.h> int main(void) { printf("salom, dunyo\n") }
. The special character in it tells the compiler that the next character is a special instruction. And the symbol n after it means "new line" (new line).
The expression on the first line of code is a very special command that says we want to use the capabilities of a library called stdio.h. This library allows us to use the function printf.
Libraries
is a set of ready-made functions that we can use in our code.
Variables
#include <cs50.h> #include <stdio.h> int main(void) { string answer = get_string("Ismingiz nima? "); printf("Assalomu alaykum, %s\n", answer); }
string
. Also, there are many other data types such as int, bool, char, etc.
%s is a placeholder called format code
that tells the printf function to prepare to accept some string variable.
There are also format codes for other data types, for example:
%i - for
int
(integers).
Conditional operators
#include <cs50.h> #include <stdio.h> int main(void) { int x = get_int("x ni kiriting: "); int y = get_int("y ni kiriting: "); if (x < y) { printf("x soni y sonidan kichik\n"); } }
), variables x and y. Their values are filled using the get_int function of the cs50.h library. Using the conditional operator, we compare the values of x and y and display a message on the screen depending on the result.
Block diagram is a way we can check how a computer program works. With this method we can check the efficiency of our code.
Let's see the block diagram of our code above:
We can improve the program by coding as follows:
#include <cs50.h> #include <stdio.h> int main(void) { int x = get_int("x ni kiriting: "); int y = get_int("y ni kiriting: "); if (x < y) { printf("x soni y sonidan kichik\n"); } else if (x > y) { printf("x soni y sonidan katta\n"); } else { printf("x soni y soniga teng\n"); } }
Repetition operators
to the screen 3 times:
#include <stdio.h> int main(void) { printf("meow\n"); printf("meow\n"); printf("meow\n"); }
#include <stdio.h> int main(void) { int i = 0; while (i < 3) { printf("meow\n"); i++; } }
The
#include <stdio.h> int main(void) { for (int i = 0; i < 3; i++) { printf("meow\n"); } }
Second argument: i < 3 - checked condition.
Finally, the i++ argument means that every time our number i increases by one.
We can also create our own function:
void meow(void) { printf("meow\n"); }
#includevoid meow(void); int main(void) { for (int i = 0; i < 3; i++) { meow(); } } void meow(void) { printf("meow\n"); }
meow funksiyasini asosiy funksiya ichida chaqira olishimiz uchun funksiya prototipi kodning yuqori qismida void meow(void) sifatida berilgan.
Keling, C tilida kalkulyator yasaymiz:
#include <cs50.h> #include <stdio.h> int main(void) { // x qiymati kiritilsin int x = get_int("x: "); // y qiymati kiritilsin int y = get_int("y: "); // Qo'shish amalini bajarish printf("%i\n", x + y); }
get_int funktsiyasi yordamida foydalanuvchidan butun son bo'lgan x va y o'zgaruvchilariga qiymat berishi so'ralyabdi. Keyin printf funksiyasi butun son uchun format kodi - %i belgisi yordamida x + y qiymatini chop etadi.
Arifmetik operatorlar kompilyator tomonidan qo'llab-quvvatlanadigan matematik operatsiyalardir. C tilida arifmetik operatorlarga quyidagilar kiradi:
Abstraksiya - bu muammoni kichik-kichik bo'laklarga bo'lib hal qilish orqali kodimizni soddalashtirish san'ati.
Biz yuqoridagi kodimizni quyidagicha abstraktlashimiz mumkin:
#include <cs50.h> #include <stdio.h> int add(int a, int b); int main(void) { // x qiymati kiritilsin int x = get_int("x: "); // y qiymati kiritilsin int y = get_int("y: "); // Qo'shish amalini bajarish printf("%i\n", add(x, y)); } int add(int a, int b) { return a + b; }
Bunda parametr sifatida a va b butun sonlarini qabul qilib oladigan va ularning yig'indisini qaytaradigan alohida add funksiyasi e'lon qilingan va asosiy funksiya ichida argument sifatida x va y butun sonlarini olib add(x, y) funksiyamiz chaqirilyabdi.
Kommentlar - kompyuter dasturining asosiy qismlari bo'lib, yozgan kodimiz nima vazifa bajarayotganini ifodalovchi, boshqa dasturchilarga, shuningdek o'zimizga tushunarli hamda qisqa qilib qoldirgan izohlarimizdir. Kommentni yozish uchun shunchaki ikkita // beligisidan foydalanamiz:
#include <cs50.h> #include <stdio.h> int main(void) { // Musbat butun son kiritilsin int n; do { n = get_int("Musbat butun son kiriting: "); } while (n < 1); }
Ma'lumotlar turlari o'zgaruvchida saqlanishi mumkin bo'lgan ma'lumotlar turini belgilaydi. Misol uchun, o'zgaruvchilar raqamlar, belgilar yoki mantiqiy qiymatlarni saqlashi mumkin. O'zgaruvchining turi kompyuterga ushbu ma'lumotlarni qanday boshqarishni aytadi.
C tilidagi umumiy maʼlumotlar turlari:
Har bir turning o'ziga xos chegaralari bor. Misol uchun, xotiradagi cheklovlar tufayli int ning eng yuqori qiymati 4294967295 bo'lishi mumkin. Agar biz int ni uning eng yuqori qiymatidan o'tkazib sanashga harakat qilsak, bu o'zgaruvchida noto'g'ri qiymat saqlanishiga (integer overflow) olib keladi.
Xotirani noto'g'ri ishlatish kodimizdagi xatolar yoki muammolarga olib kelishi mumkin. Muammolarni oldini olish uchun biz to'g'ri ma'lumot turidan foydalanayotganimizga ishonch hosil qilishimiz kerak.
Ushbu maqolada CS50x 2024 manbasidan foydalanilgan.
The above is the detailed content of CS- Week 1. For more information, please follow other related articles on the PHP Chinese website!