DEV Community

Gilad Ri
Gilad Ri

Posted on • Updated on

Practice #1

First, we should have an environment where we can write and run ours code. This type of program is called: Integrated Development Environment (IDE).

A list of Recommended IDEs:

In this tutorial, we will use Eclipse. But feel free to use any other IDE that you want. It completely depends on personal preferences. I don't teach here how to download and install an IDE on purpose, because the version of the IDE and the download and install instructions may be a little different when you read this post. Just follow the instructions on the website of the IDE you chose and it will be totally fine. (If you need any help, just send me an email about that).

Ok, so let's begin.

Open the IDE and then create new Console App project. After that, just make sure that the source file you are writing in has a ".c" suffix and not a ".cpp" suffix (if not, just rename the file).

Now, write your first code:

#include <stdio.h>

int main()
{
    printf("Hello World!\n");
}
Enter fullscreen mode Exit fullscreen mode

And run it! (Usually, you can do that by pressing F5. Again, just follow the instructions of the IDE you chose). In a console, (which will be opened), you have to see "Hello world!". If you saw that, congratz - you wrote and ran your first code!

Let's write some more complicated code now:

#include <stdio.h>

int getUserInput() {
    int input;
    // Gets input from the user
    printf("Insert a number:\n");
    scanf("%d", &input);
    return input;
}

void isDivideWithoutRemaining(int x, int y) {
    // The '%' operator performs the modulo operation
    if (x % y == 0) {
        int z = x / y;
        printf("%d number can be divided without a remainder by %d and the result is %d", x, y, z);
    } else {
        printf("%d cannot be divided without a remainder by %d", x, y);
    }
}

int main()
{
    // Gets two numbers from the user
    int x = getUserInput();
    int y = getUserInput();
    // Checks if the first number can be divided without a remainder by the second number
    isDivideWithoutRemaining(x, y);
    // Success
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

For example:

Insert a number:
10
Insert a number:
2
10 number can be divided without a remainder by 2 and the result is 5
Enter fullscreen mode Exit fullscreen mode

Read here about modulo, if you needed (The '%' operator in C).

If you are using Visual Studio, for now, just write above your code the follows:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
Enter fullscreen mode Exit fullscreen mode

any time you uses scanf. If you want to understand why just Google it. It's really not important. You can see another solution here.

Write this code and make sure you understand what it does.

That's all for today, Have fun keep practicing some C by yourself! (There is no better way, trust me. If you need an idea, try to write a code which gets 2 numbers from the user and then prints all the mathematics operations. For example, if our variables are x and y it will print: x+y, x-y, x*y, x/y with appropriate explanations and comments).

Always remember, it's much C-mpler than you thought!

Regards,
Gilad

Top comments (0)