a n00b problem

Hello!

I'm a C/C++ newbie that pretends to learn by myself this programming language and I'm reading the C++ language tutorial for my first steps into it. The first example of this tutorial is the tipical "Hello World!" but I get an error compiling the code:

#include<stdio.h>

using namespace std;

int main()
{
printf("Hello World\n");
return 0;
}

hello.c:3: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'namespace'


According to the tutorial this shouldn't appear and that's why I'm asking.

For more notes I,m compiling under Ubuntu and using gcc 4.3.2-1ubuntu12

lotta thx
C and C++ are two distinct languages.

Choose one or the other. (Line 3 is C++, everything else is C.)

C
1
2
3
4
5
6
7
#include <stdio.h>

int main()
  {
  printf( "Hello world!\n" );
  return 0;
  }


C++
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
  {
  cout << "Hello world!\n";
  return 0;
  }


Hope this helps.
oh, thank you Duoas, I didn't figure that. I thought that C++ was C with some adds. BTW, is it better/more complete/more useful/more efficient to use C or C++?
You can actually use C code in C++ :


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main ()
{
 cout << "Hello World!" << endl;
 printf( "Hello World!\n" );
  return 0;

}
C++ was specifically designed to be backwards-compatible with C. Hence, you can use most of the C language in C++.

is it better/more complete/more useful/more efficient to use C or C++?
It depends on your requirements. C++ makes most things a lot easier for the programmer.

Hope this helps.
thank to both of you, but I one more question for you ;)

When I try to compile the Hello World C form the compiler couldnĀ“t find the file iostream and still appear the namespace error, why?

man, I feel like a completely noob here xD
My wild guess is that you are trying to compile a C++ program with a C compiler.

For example, the GNU compiler comes with gcc -- the C compiler, and g++ -- the C++ compiler.

If that doesn't help please post back with more information.

Hope this helps. ;-)
Did you installed gcc correctly?
Trying doing sudo apt-get install build-essential

I don't know if this will help but oh well.

Last edited on

***************
hello.c:3: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'namespace'
***************

If your trying to compile on ubuntu, gcc takes the file name into account.... hello.c is interpreted by cc to be a C file.

Rename your file either hello.cc or hello.cpp and recompile.

(oh yeah, and don't forget to use the stdc++ lib for linking: gcc -o hello hello.cc -lstdc++)
Last edited on
or just rename it to .cc/.cpp and call g++
I believe a standard gcc hello.cc would work fine, and than run it by ./a.out
Once you get familiar with compiling than try and do the file naming, I don't believe you need to include -lstdc++ when linking. I gcc -o <what you want it named> <file to be compiled> All the time and never have to use the -lstdc++...

either way works I suppose, (currently on windows), but::
gcc for C and g++ for C++
And if you didn't install gcc correctly...
sudo apt-get install gcc

**Note: In Ubuntu 9.04 if you haven't already discovered, the root terminal is disabled and must be accessed through the normal terminal through sudo -i

Unless you know how to alter that. ;)

In the end, this is really about understanding your compiler suite, and what it's actually doing. Regardless of how you invoke it, the gnu compiler suite is sensitive to file extensions, and attempts to compile as such.

Additionally, just because certain compilers (or wrappers) automatically include certain libraries for linking, it's still important to understand what's happening, so there's nothing wrong with telling a noob don't forget to ensure the supporting libraries are included.

That said, the example was nothing more than the appropriate command line, that will work on any platform. Note just using gcc hello.cc won't link on all platforms, here is the output for an up to date Arch Linux:

[kyenos@xxxxxx tmp]$ gcc hello.cc
/tmp/ccBbwkV1.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

So don't make assumptions, just because it works on your favorite platform.

c++ is c with some adds, but it is also a lot more. Object oriented, overloading functions, and classes are all new add ons,
but it also gets rid of useless c commands and uses much cleaner syntax. Also you can still you libraries from c if you wished...
Last edited on
Topic archived. No new replies allowed.