-   Programming (
-   -   problems running c programs in bash (

KDE4me 04-29-2003 11:59 PM

problems running c programs in bash
Help me,
I'm new to Linux and Linux programming.

I've compiled a program hello.c to hello (Prints out "Hello World")
and when I try to run it from bash here's what happens:
$cc -o hello hello.c
$ ./hello

(I get no output)

yet when I run it from Konqueror (CTRL + E) and use the command "./hello" Konqueror shows "Hello World".

The book I'm using says something about changing the path to include the directory in which you are compiling. I've done that and it makes no difference, it still won't run in bash.

What am I doing wrong in bash?

Dark_Helmet 04-30-2003 12:25 AM

Since it should be such a small program, post the source so we can look at it. This works for me:


#include <stdio.h>

int main(int argc, char *argv[])
  printf("Hello World!\n");

  return 0;

$ gcc -o hello hello.c
$ ./hello
Hello World!

There was a thread I'd seen a while ago about not putting the newline (the '\n') at the end of the string. According to it, the command prompt would "overwrite" the last message your program prints. Make sure you have the newline and try again.

KDE4me 04-30-2003 01:03 AM

Thanks Dark_Helmet!
I looked at the code and I didn't put the newline in, so I inserted the newline and it worked. Exactly like you said!

But this raises the problem if I don't want to put a new line in how will I get output?
for example in c++
cout >> "Enter something: ";
cin >> something; //this will be on the same line
cout >> "Here's what you entered: " + something;

if I were to insert a newline all the time it would appear:
Enter something:

On a different note, in C can I use
cout >> etc?
or is it only printf();
On another note, is it worth learning C for linux or should I just learn C++? (I already know how to program in C++ for windows)


whansard 04-30-2003 01:23 AM

The prompt only overwrote the output because the
program ended. If the program was still running,
waiting for input, the line wouldn't be overwritten.

KDE4me 04-30-2003 01:55 AM

so are you saying that the last line of my program would always have to have a newline so the prompt doesn't overwrite it? This doesn't seem like an efficient way to end the program.

whansard 04-30-2003 02:36 AM

If you print something to the screen, and you want it
to be there when you exit the program, then put a newline
at the end. You have complete control whether you want
a newline or not. I have a program with a counter that
keeps printing a number to the same place. if programs
automatically put in a newline whether i wanted one or
not, it would be less versatile, not more.
i think you should be getting mad at bash for having
a carriage return, but not a newline, rather than C for
making you put a newline if you want one.
when i took pascal and c classes 10 years ago, they
taught us "Hello World!\n"
Actually i can't remember the pascal commands.
I remember being frustrated with C back then too.
With a tool like C, where you have so much control,
there's so much more stuff you can mess up.

KDE4me 04-30-2003 05:14 AM

Now that you point that out, I have changed my opinion. I supposed writing over stuff like that is beneficial. Thanks, you have opened my eyes to a different point of view.

Dark_Helmet 04-30-2003 09:54 AM

Ahhh, Pascal, the good old days I forget what you need in the uses statement for this, but:

write(<text>);        <= No newline
writeln(<text>);      <= With newline

Having to add a newline to the last output of a program isn't a terrible injustice. Assume the shell didn't do anything to put a newline (which, I think should be the way things are). Then your hello world program would have looked like:

Hello World!$

Or, on my system:

Hello World![user@myhost user]$

You're mashing the last output with the prompt without the newline. I've just got an unnatural hatred for doing that... it just looks really bad to me.

About your other questions...
No, cout is strictly a C++ construct. You can't use it in standard C. However, you can use standard C within C++ programs. Perhaps it's a subtle point, but if you use any C++ constructs, your code is C++ . For standard C, all I am aware of (since I've had little reason to search for anything else) are the printf(), scanf(), fprintf(), fscanf(), and getch() functions. Those should be just about all you need

Use of cout does not automatically generate a newline at the end of the statement. You still have to manually insert them. For example:

cout << "This\n";
cout << " is an ";
cout << "example\n";

is an example

Or for your prompt:

cout << "Enter a number: ";
cin >> user_input;
cout << "This is what you entered: " << user_input

Enter a number: 6
This is what you entered: 6

The newline after the prompt is added when the user presses Enter.

The vast majority of code written for Linux will be in standard C. When you say you know C++ from Windows, be careful. From personal experience, Borland compilers offer a lot of non-standard function call to make life easier (can't remember any off-hand). Those functions won't port over to a Linux compile unless you code up a version of it yourself. I was amazed to see how dependent I was on them.

KDE4me 04-30-2003 08:50 PM

Thanks you guys, you've been a great help. wan't a Borland compiler, it was a MS compiler. Does that make it worse?

Tinkster 04-30-2003 09:39 PM


it wan't a Borland compiler, it was a MS compiler. Does that make it worse?
Yes, since MS is always way behind the standards :)
or, even worse, trying to make their own ;)


whansard 05-01-2003 12:22 AM

with EVERYTHING microsoft will make their own
standard, which is incompatible with the real standard,
which means it's not a standard.
they don't want their software interoperating with other
software, because that makes other software easier
to use. It is to their benefit to make things not work
together with anything except their own stuff. expect
it in advance.

All times are GMT -5. The time now is 01:03 AM.