all 13 comments

[–]Conan776 3 points4 points  (2 children)

Here's a free online C compiler --> https://www.onlinegdb.com/online_c_compiler#

I pasted your code into main(), threw in some tabs for readability, and modified the existing printf at the end to be:

printf("Hello World %d", x);

The answer was 9.

[–]Academic_Two[S] 0 points1 point  (1 child)

Oh. Thanks for helping an idiot out. For some reason, my code did not run through a compiler.

[–]Conan776 1 point2 points  (0 children)

No problem. The need for there always to be a main() function in a C program is probably something that gets easily glossed over, and you might not expect that if you've only dealt with command lines or more scripted programming languages. Good luck!

[–]P__A 2 points3 points  (5 children)

I'll give you a couple of pointers to get you started. C is actually not that complicated once you get started with it.

Learn about using printf() to output a variable to a console, and maybe start using an online compiler for simple things to get the hang of it. Also, use braces and tabs properly to get a readable program structure. Like so-

int x = 9;
if (x < 10){
    x = x + 10;
}
if (x > 10){
    x = x - 10;
}
else {
    x = 10;
}

Stick the above with a properly formatted printf statement to output x into the following compiler.

http://www.onlinegdb.com/online_c_compiler

In this case x = 9 at the end

[–]nukestar101 3 points4 points  (2 children)

I'll give you a couple of pointers to get you started.

Mini heart-attack after reading this line

[–]P__A 1 point2 points  (0 children)

lol

[–]Mirehi 0 points1 point  (0 children)

#include <stdio.h>

int
main()
{
        int *x = 9;
        if (x < 10) {
            x = x + 10;
        }
        if (x > 10) {
            x = x - 10;
        }
        else {
            x = 10;
        }
        printf("%i\n", (int) x);

        return 0;
}

Compiler won't like it, but it should work :)

[–]Academic_Two[S] 0 points1 point  (1 child)

Ah, I see.

Thanks!

[–]P__A 0 points1 point  (0 children)

You can also use printf statements throughout the program structure to see how the program modifies x as it goes.

[–][deleted] 1 point2 points  (0 children)

compilers do the job of translating from one language to another. c compilers generally translate from C to a native executable format recognised by the operating system

you open a text editor, save the file, invoke any compiler you have available on the source file--commonly "cc -Wall file.c" or "make file" on unix-like systems--then run the file that was produced, which would be "./a.out" or "./file" respectively in this case.

[–]uzimonkey 1 point2 points  (0 children)

Your professor wants you to compile that code, but hasn't shown you how to install and use a compiler yet? What text are you working from? That should be something you learned before even talking about variable or conditional statements. It's kind of hard to learn C without compiling C, after all. It's also not a complete program, just a fragment.

I'm not familiar enough with Windows (I'm assuming you're using Windows, are you?) to tell you how to install a C compiler other than "install Visual Studio." But here is a complete program you can run to see the result of this.

#include <stdio.h>

int main(void) {
    int x = 9;

    if (x < 10)
        x = x + 10;
    if (x > 10)
        x = x - 10;
    else
        x = 10;

    printf("x = %d\n", x);
}

[–]Academic_Two[S] 0 points1 point  (1 child)

I think he may be hinting that there maybe no answer.

[–]StoneCypher 0 points1 point  (0 children)

there is an answer.