0

I made my first program in C without any problems at all at my first try, now that I want a new workspace and a new project, it goes south before I've even written anything.

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("hello world\n");
    return 0;
}

The code above should just print out "hello world". Instead, I get the following error code:

C:\Users\DavidH\Desktop\bingolotto\main.c:1
(function (exports, require, module, __filename, __dirname) { #include <stdio.
                                                              ^
SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3
Press any key to continue . . .

I'm guessing I've got some faulty directories or something similar, but I've made new workspaces and tried at least two new projects to no prevail.

I'd really like to know what's going on here :)

1 Answers1

5

You practically need to use a C compiler (not something else like node.js). I recommend using GCC on the command line, e.g. in a terminal (order of program arguments to gcc matters a lot). And I recommend enabling all warnings and debug info (which are not enabled by default), so compile with:

 gcc -Wall -Wextra -g helloworld.c -o helloworld.exe

Improve your code to get no warnings at all.

Read the documentation of every used function (e.g. of printf), even if at first you don't understand all of it. Download the specification of C11, e.g. n1570, and look inside and refer to it.

Be scared of undefined behavior. It is tricky (your C program could apparently seem to work most of the time, and still be very wrong and buggy).

Learn to use the debugger, e.g. use gdb (and perhaps other tools, like valgrind).

You probably should learn to use some version control system (I recommend git) and some build automation tool (such as GNU make or ninja).

Be aware that coding conventions and style matter a lot in C (see for example this).

PS. You might consider installing some Linux distribution on your laptop, it is very student- and developer- friendly and mostly made of free software (some very helpful to novice and/or expert C developers, both as tools and as examples). You also should read (for inspiration) the source code of some existing free software projects (e.g. on github).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I use gcc template, minGW complier and GNU debugger. I added those lines you recommended and it worked perfectly, thanks! – David Hermansson Dec 11 '17 at 10:02
  • I do recommend installing Linux on your laptop. – Basile Starynkevitch Dec 11 '17 at 10:03
  • debugger and git is something I grew fond of during my C# lectures, I just need some time to get to know C Haven't got a laptop, but I already have a linux environment installed – David Hermansson Dec 11 '17 at 10:04
  • 2
    What has Linux got to do with anything? "Developer friendly" ...yeah right. I have barely touched Linux in 15 years and I work pretty much exclusively with C programming. Please keep advise objective. Tool recommendations are also off-topic here. – Lundin Dec 11 '17 at 10:12
  • Linux has most of its programs as free software in C, so people can glance into them. The C standard library and the Linux kernel is also free software (unlike on Windows), and you can also look into it. And `valgrind` works well on Linux (but perhaps not on Windows). – Basile Starynkevitch Dec 11 '17 at 10:14