-1

I'm learning C over the summer with the book "C Programming - A modern approach", now to the problem:

When I write:

#include <stdbool.h>

the compiler can't run the program because it can't include bool. When I use bool straight away it knows what it's supposed to do.

Is this something new in Visual Studio 2017 or do I start the documents wrong and get C# or C++? (I don't know those languages but I supposed boolean is well integrated in them)

I start my compiler by "Windows Desktop Wizard -> Empty project -> Source file -> Add -> New project -> C++ file (.cpp)"

It's not a huge problem (since it mostly makes things easier) but I want to learn C and not C++/C#.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    The `.cpp` suffix is for C++, so you're programming in C++ and not C. And C++ have a `bool` type without needing the `` header file. – Some programmer dude Jun 13 '18 at 08:39
  • 1
    Why do you choose C++ file in the compiler? There's no C option? – myradio Jun 13 '18 at 08:40
  • C# is so much easier to learn. I admire you for going so low level but learning languages especially your first takes years. If you find C too hard give C# a try. – Jeremy Thompson Jun 13 '18 at 08:45
  • @JeremyThompson What you wrote is true, but .NET with its very large libraries makes many things seem like they are working "by magic". There is a very big abstraction that doesn't show you what your computer can do and how it can do it. Starting from C (with its "low level" feel) I fell you have better instruments to explore the inner working of .NET – xanatos Jun 13 '18 at 08:56
  • @xanatos you're right. I wish I had the patience to learn C 20 yrs ago – Jeremy Thompson Jun 14 '18 at 08:26

2 Answers2

2

Give the (your) file the extension .c, not .cpp. In this way the Visual Studio will use the C language instead of using the C++ language. But note the response of Bathsheba... and a so answer that expands on what he wrote.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 1
    I would also set the project to 'compile as C'. – Baldrick Jun 13 '18 at 08:40
  • @Baldrick The default setting should be something akin "autosense". If I add a .c file and then write `bool test = true` I get errors both on `bool` and `true`... Some [msdn](https://learn.microsoft.com/en-us/cpp/build/reference/tc-tp-tc-tp-specify-source-file-type) on this: *By default, CL assumes that files with the .c extension are C source files and files with the .cpp or the .cxx extension are C++ source files.* – xanatos Jun 13 '18 at 08:50
  • Fair enough. I guess with the project compile setting you could probably force .cpp files to compile as C too. Thanks for the info! – Baldrick Jun 13 '18 at 09:42
2

Despite pretentions to the contrary, The C++ compiler that ships with Visual Studio is not a C compiler (it is in my opinion an excellent C++ compiler for targeting Windows). Even if you name a source file .c and are careful with the compiler settings it is still not a C compiler.

Your best bet here is to use a genuine C compiler if you want to write C programs; gcc in cygwin is a good toolset.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483