0

I'm really new in C and I have the Code Blocks compiler. When compiling a really simple program. Sometimes it works but sometimes it doesn't. I'm guessing it has something to do with the things I select when starting a new project. I usually select a console application C language and I have no idea what to select next does anyone know whats going on and how to fix it? This is my code:

#include <stdio>

int main()
{
  printf ("hello world !");
  getchar ();
  return 0;
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • you may want to change the include to `` in `C` or `` in `c++` – Ivaylo Strandjev Nov 08 '13 at 13:59
  • 2
    It should be `#include `. – Shafik Yaghmour Nov 08 '13 at 13:59
  • 1
    Next time you ask, remember to say exactly which error message you get. – Guilherme Bernal Nov 08 '13 at 14:01
  • 7
    When posting a question on Stackoverflow, please be specific in your details. Statements such as *"sometimes it doesn't work"*, or *"I still get errors"* don't help. Instead, post the exact error message, and the circumstances under which the compiler emits it. – JBentley Nov 08 '13 at 14:01
  • The tag `compiler` should be applied to questions concerning the programming of compilers or for questions about the detailed inner workings of compilers. Don't use `compiler` for questions about options and settings for a particular compiler, use the name of the compiler you are interested in instead. – user207421 Nov 14 '13 at 00:49

2 Answers2

1

#include <stdio.h> and not #include <stdio>.

Header files in C are saved with .h file extension.

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

headers in C, by convention, end with .h.

so it should be #include <stdio.h> instead of #include <stdio>.

However, if you are using a C++ compiler, with your file being called foo.cpp, then you can use #include <cstdio> and add using namespace std; to use getchar() function.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 1
    I do not think `using namespace std;` is required for using `getchar()` - is `getchar()`a part of the std namespace? Also this question is not tagged C++. – Sadique Nov 08 '13 at 14:04
  • 1
    but he is using a C++ IDE(codeblocks), and there is a fair amount of chance he is using a C++ file extension. People often get confused when they try to use an IDE first, for learning. I have to check if getchar recides in std namespace(googling) – Aniket Inge Nov 08 '13 at 14:06
  • I do not believe its required. - http://ideone.com/CgGNxi – Sadique Nov 08 '13 at 14:06
  • -1 [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) ... that is awful advice for a beginner and the question is not tagged *C++*. – Shafik Yaghmour Nov 08 '13 at 14:07
  • By this answer here, it can be implementation dependent @Acme http://stackoverflow.com/questions/10460250/cstdio-stdio-h-namespace – Aniket Inge Nov 08 '13 at 14:08
  • @ShafikYaghmour what if by implementation, `getchar()` does not reside in `std` namespace? Your code will have portability issues. – Aniket Inge Nov 08 '13 at 14:09
  • If it is `implementation dependent` then its still not advisable. – Sadique Nov 08 '13 at 14:09
  • why is it not advisable? @Acme and what exactly is not advisable? – Aniket Inge Nov 08 '13 at 14:13
  • @ShafikYaghmour the question was tagged with [tag:C++] in the beginning along with [tag:C] – Daan Timmer Nov 08 '13 at 14:25
  • @ShafikYaghmour I have answered for both C and C++. Not "only" C++. – Aniket Inge Nov 08 '13 at 14:28
  • And as to WHY I have answered for C++ then see comment #1 on this answer – Aniket Inge Nov 08 '13 at 14:29
  • @Aniket - regarding _but he is using a C++ IDE(codeblocks)_ Code::Blocks ***is an IDE only***. _It has no affiliation with any particular type of compiler_, i.e. it will work with many. I use it all the time (exclusively in fact) with minGW, for ANSI C work. – ryyker Nov 14 '13 at 01:00