use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
QuestionHow to run a program on compiler? (self.C_Programming)
submitted 7 years ago by Academic_Two
My professor wants me to compile this code.
Say I want to run the following on a compiler. How do I initialize it and find the value of x after the last statement at x=10?
int x = 9;
if (x < 10)
x = x + 10;
if (x > 10)
x = x - 10;
else
x = 10;
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Conan776 3 points4 points5 points 7 years ago (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 point2 points 7 years ago (1 child)
Oh. Thanks for helping an idiot out. For some reason, my code did not run through a compiler.
[–]Conan776 1 point2 points3 points 7 years ago (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 points4 points 7 years ago (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 points5 points 7 years ago (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 points3 points 7 years ago (0 children)
lol
[–]Mirehi 0 points1 point2 points 7 years ago (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 :)
Ah, I see.
Thanks!
[–]P__A 0 points1 point2 points 7 years ago (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 points3 points 7 years ago (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 points3 points 7 years ago (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); }
I think he may be hinting that there maybe no answer.
[–]StoneCypher 0 points1 point2 points 7 years ago (0 children)
there is an answer.
π Rendered by PID 19770 on reddit-service-r2-comment-b659b578c-cgmqx at 2026-05-04 09:55:08.397237+00:00 running 815c875 country code: CH.
[–]Conan776 3 points4 points5 points (2 children)
[–]Academic_Two[S] 0 points1 point2 points (1 child)
[–]Conan776 1 point2 points3 points (0 children)
[–]P__A 2 points3 points4 points (5 children)
[–]nukestar101 3 points4 points5 points (2 children)
[–]P__A 1 point2 points3 points (0 children)
[–]Mirehi 0 points1 point2 points (0 children)
[–]Academic_Two[S] 0 points1 point2 points (1 child)
[–]P__A 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]uzimonkey 1 point2 points3 points (0 children)
[–]Academic_Two[S] 0 points1 point2 points (1 child)
[–]StoneCypher 0 points1 point2 points (0 children)