problems running c programs in bash
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
(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?
Since it should be such a small program, post the source so we can look at it. This works for me:
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.
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:
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)
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.
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.
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.
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.
Ahhh, Pascal, the good old days I forget what you need in the uses statement for this, but:
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:
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:
Thanks you guys, you've been a great help. Umm...it wan't a Borland compiler, it was a MS compiler. Does that make it worse?
or, even worse, trying to make their own ;)
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 04:31 AM.|