"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > CS- Week 1

CS- Week 1

Published on 2024-08-01
Browse:742

bis̊mi allãhi alrāḥmani alrāḥīm
Machines only understand binary. When we write a list of human-readable instructions for a computer, machines only understand what we now call machine code. This machine code consists only of 1's and 0's.
Using a special program called a compiler, we can turn the source code into machine code.

We can judge good code according to 3 criteria:

  • correctness (does the code produce the desired result?),
  • design (is the code design or structure well structured?),
  • style (How nicely is the code written?).

Hello World!

If we want to print some text on the screen in the C programming language, we use the printf function:

#include 

int main(void)
{
    printf("salom, dunyo\n")
}
The

printf function prints the text hello, world. The special \ character in it tells the compiler that the next character is a special instruction. And the next n symbol 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 printf function.
Libraries is a collection of ready-made functions that we can use in our code.

Variables

Let's write some code in C that greets the user by asking him what his name is:

#include 
#include 

int main(void)
{
    string answer = get_string("Ismingiz nima? ");
    printf("Assalomu alaykum, %s\n", answer);
}

The capabilities of the cs50.h library, developed specifically for the CS50 course, will be used throughout this course. One of them is the get_string function. The get_string function is used to retrieve the text entered by the user.
answer is a place reserved to remember a special user-entered text, which we call a variable. answer is of type string. Also there are many data types besides 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 is for int (integers).

Conditional operators

Let's ask the user to enter x and y variables of type int and compare the input numbers against each other:

#include 
#include 

int main(void)
{
    int x = get_int("x ni kiriting: ");
    int y = get_int("y ni kiriting: ");

    if (x 



Here we are creating two variables of type int (integer), x and y. Their values ​​are filled using the get_int function of the cs50.h library. Using the conditional operator, we compare the x and y values ​​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:
Conditional 1

We can improve the program by coding as follows:

#include 
#include 

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 katta\n");
    }
    else
    {
        printf("x soni y soniga teng\n");
    }
}

All possible cases are now considered. Let's see its block diagram:
Conditional 2

Repetition operators

Let's print "meow" 3 times:

#include 

int main(void)
{
    printf("meow\n");
    printf("meow\n");
    printf("meow\n");
}

The code we wrote works correctly, but we can improve our program by avoiding repetitions in it:

#include 

int main(void)
{
    int i = 0;
    while (i 



In this, the variable i of type int is created and the value 3 is assigned to it. Then a while loop is created that continues for i We can further improve the design of our program by using the for loop:

#include 

int main(void)
{
    for (int i = 0; i 



a for loop takes three arguments.
The first argument: int i = 0 initializes our counter.
The second argument: i Finally, the i argument tells us that every time i is incremented by one.
We can also create our own function:

void meow(void)
{
    printf("meow\n");
}

void - means that the function does not return any value. In parentheses (void) - means that the function does not accept any parameters.
We use this created meow function inside the main function:

#include 

void meow(void);

int main(void)
{
    for (int i = 0; i 



The function prototype is given as void meow(void) at the top of the code so that we can call the meow function inside the main function.

Arithmetic operators and abstraction

Let's make a calculator in C:

#include 
#include 

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);
}

The get_int function asks the user to enter values ​​for the integer variables x and y. The printf function then prints the value of x y using the format code for an integer - the %i symbol.

Arithmetic operators are mathematical operations supported by the compiler. Arithmetic operators in C include:

  • - to add;
  • - - to subtract;
  • * - to multiply;
  • / - for division;
  • % - to calculate the remainder when one number is divided by another number.

Abstraction is the art of simplifying our code by breaking down a problem into smaller pieces.
We can abstract our above code as follows:

#include 
#include 

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;
}

In this, a separate add function is declared, which accepts integers a and b as parameters and returns their sum, and our add(x, y) function is called by taking integers x and y as arguments in the main function.

Comments

Comments are the basic parts of a computer program, the comments that we make clear and concise to other programmers as well as to ourselves, explaining what the code we write is doing. We just use two // tags to write a comment:

#include 
#include 

int main(void)
{
    // Musbat butun son kiritilsin
    int n;
    do
    {
        n = get_int("Musbat butun son kiriting: ");
    }
    while (n 



Data types

Data Types specifies the type of data that can be stored in a variable. For example, variables can store numbers, characters, or boolean values. The type of the variable tells the computer how to handle that data.
Common data types in C are:

  • bool: can hold boolean values ​​such as true (true) or false (false).
  • char: can store only one character.
  • float: a real number with decimal values.
  • int: an integer without a decimal point.
  • long: can store an integer larger than int because it uses more bits.
  • string: can store a sequence of characters (eg a word).

Each species has its own limits. For example, due to memory limitations, the maximum value of an int can be 4294967295. If we try to count an int past its maximum value, this will cause the variable to store an invalid value (integer overflow).
Improper use of memory can cause errors or problems in our code. To avoid problems, we need to make sure we are using the correct data type.

This article uses CS50x 2024 source.

Release Statement This article is reproduced at: https://dev.to/udilbar/cs50-week-1-4p7i If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3