LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2005, 10:27 PM   #1
uphu
Member
 
Registered: Dec 2004
Posts: 44

Rep: Reputation: 15
Starting C


Ok, I'm starting to learn C, and I already know a bunch of java and php so its not the language that is confusing me as much as the compiling. I have GCC installed, but I have no idea how to use it. I am also unsure of how to save my files written in C. And no, I have not found any good tutorials written on this. So how would I write a simple hello world program? How do I save it (what extention do I use)? How do I compile it in GCC?
 
Old 01-08-2005, 10:37 PM   #2
melinda_sayang
Member
 
Registered: Dec 2003
Location: Petaling Jaya
Distribution: Ubuntu
Posts: 475

Rep: Reputation: 31
Save it as blabla.c
The in console do this:
$ cd /to/where/the/file/is
$ gcc blabla.c -o blabla
$ ./blabla
 
Old 01-08-2005, 11:16 PM   #3
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
Well, assuming you're not using any libraries that need to be linked in, the tags
-o <outfile>
-Wall (all warnings)
-D_GNU_SOURCE (helpful for certain functions, but decreases portability)
are fairly standard. Syntax is the same as most unix programs:
Code:
gcc [FLAGS] infile
If you don't have the -o, it will make the file a.out, which is handy if you don't feel like typing a lot of stuff while you're in debugging phases.

When you get to libraries with special stuff, they'll tell you in the man pages. For example, ncurses requires the -lncurses flag, and GMP requires -lgmp.
 
Old 01-09-2005, 01:55 AM   #4
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
I use it slightly differently,

$gcc -o whatever whatever.c
 
Old 01-09-2005, 02:00 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Or the simpler:
Code:
$ make whatever
(without a makefile, make knows how to build simple executables)
 
Old 01-09-2005, 02:29 PM   #6
uphu
Member
 
Registered: Dec 2004
Posts: 44

Original Poster
Rep: Reputation: 15
Ok, thanks for the help, I got it working. However, I found out that the problem was actually with my Hello World file. For Java, I use the Green Tea Press tutorial to learn, and I found it extremely good. So for C, I decided to learn from the same tutorial converted to C ( http://www.ibiblio.org/obp/thinkCS/cpp/english/ , its C++, but I would expect the print statements to be the same). The hello world file they used was:

Code:
#include <iostream.h>

void main ()
{
  cout << "Hello, world." << endl;
}
This didn't work, it returned a bunch of syntax errors. Then I looked elsewhere and found:

Code:
#include <stdlib.h>

int main(int argc, char **argv) {
     printf("Hello world!\n");
     return 0;
}
This worked fine, and now I am very confused, what is with the first one? How can a language have two completely different print statements?

Also, does anyone have any recommendations for a C or C++ tutorial? I really just need to learn the syntax of it.

Last edited by uphu; 01-09-2005 at 02:32 PM.
 
Old 01-09-2005, 03:10 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
How can a language have two completely different print statements?
Not a single language, C and C++ are different languages with differing ways of printing things.
The first version is C++ only, and the second one is regular C.
 
Old 01-09-2005, 03:32 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You can compile almost every C-file with a C++ compiler. But not the other way around: compiling C++ code with a C compiler. This is what you have tried.

Pu simply, C++ is like C with extensions. A C-compiler does not recognise these extensions.

BTW the C++ compiler is called "g++".
 
Old 01-09-2005, 03:54 PM   #9
uphu
Member
 
Registered: Dec 2004
Posts: 44

Original Poster
Rep: Reputation: 15
Ok, thanks, I was unaware of this. I thought GCC was a C++ compiler, but I guessed wrong.

could one of you point me in the direction of g++'s main page? Google ignores +'s... :\
 
Old 01-09-2005, 04:08 PM   #10
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Your first example won't compile with g++ without modify it a little
Code:
#include <iostream>

using namespace std;
int main ()
{
  cout << "Hello, world." << endl;
  return 0;
}
g++ has the same syntax as gcc on command line
 
Old 01-09-2005, 04:13 PM   #11
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
If your C++ tutorial told you that a proper C++ "hello world" program is written as,
Code:
#include <iostream.h>

void main ()
{
  cout << "Hello, world." << endl;
}
I would throw it out the window because it's obsolete crap.

Other posters have shown how it should look in proper C++.

Since you're learning C, I hope you find a good C tutorial and not one of the same quality as your C++ tutorial.
 
Old 01-09-2005, 04:21 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by uphu
Ok, thanks, I was unaware of this. I thought GCC was a C++ compiler, but I guessed wrong.

could one of you point me in the direction of g++'s main page? Google ignores +'s... :\
To make things confusing () the software-package/product "GCC" contains compilers for C, C++ and other languages like fortran.

Both the C-compiler "gcc" and the C++-compiler "g++" are part of the "GCC" software package (GCC = GNU Compiler Collection). Its web-site is at
http://gcc.gnu.org/
 
Old 01-09-2005, 05:07 PM   #13
uphu
Member
 
Registered: Dec 2004
Posts: 44

Original Poster
Rep: Reputation: 15
oh, in that case I just need to download the C++ library? I think its on one of my install disks actaully.

Do you happen to know how I can check which libraries I currently have installed for GCC?
 
Old 01-09-2005, 05:08 PM   #14
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Just so you know, if you enclose C++ in quotes, google won't ignore the +'s (e.g. "C++")
 
Old 01-09-2005, 05:39 PM   #15
uphu
Member
 
Registered: Dec 2004
Posts: 44

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by itsme86
Just so you know, if you enclose C++ in quotes, google won't ignore the +'s (e.g. "C++")
I should've known that >.<

but it still depends
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
starting xprogram without starting X server. bruse Linux - Newbie 9 04-26-2005 07:05 PM
Terminal cmd for starting Matlab M-file editor without starting matlab fubzot Linux - Software 2 02-15-2005 06:49 AM
Won't Boot - Starting Printer Service - Starting CUPS jeansond Linux - Newbie 0 10-11-2004 06:39 PM
Starting apps after starting xfce?? gtgoku Slackware 2 08-21-2004 01:49 PM
starting kpppd w/ ximian or starting ximian w/ kde ergo_sum Linux - Newbie 8 12-02-2003 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration