LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 04-29-2003, 11:59 PM   #1
KDE4me
Member
 
Registered: Apr 2003
Location: Australia
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
Posts: 90

Rep: Reputation: 15
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?
 
Old 04-30-2003, 12:25 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

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

Code:
#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.

Last edited by Dark_Helmet; 04-30-2003 at 12:40 AM.
 
Old 04-30-2003, 01:03 AM   #3
KDE4me
Member
 
Registered: Apr 2003
Location: Australia
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
Posts: 90

Original Poster
Rep: Reputation: 15
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:
"theinput"

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)

Thanks.
 
Old 04-30-2003, 01:23 AM   #4
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
Posts: 3,171

Rep: Reputation: 51
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.
 
Old 04-30-2003, 01:55 AM   #5
KDE4me
Member
 
Registered: Apr 2003
Location: Australia
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
Posts: 90

Original Poster
Rep: Reputation: 15
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.
 
Old 04-30-2003, 02:36 AM   #6
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
Posts: 3,171

Rep: Reputation: 51
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.
 
Old 04-30-2003, 05:14 AM   #7
KDE4me
Member
 
Registered: Apr 2003
Location: Australia
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
Posts: 90

Original Poster
Rep: Reputation: 15
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.
 
Old 04-30-2003, 09:54 AM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
whansard:
Ahhh, Pascal, the good old days I forget what you need in the uses statement for this, but:
Code:
write(<text>);        <= No newline
writeln(<text>);      <= With newline
KDE4me:
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:
Code:
cout << "This\n";
cout << " is an ";
cout << "example\n";

-----
This
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.
 
Old 04-30-2003, 08:50 PM   #9
KDE4me
Member
 
Registered: Apr 2003
Location: Australia
Distribution: RedHat 9.0 / Slackware 9.0/ FreeBSD 4.8 / Solaris 8 x86 / Mandrake 9.0
Posts: 90

Original Poster
Rep: Reputation: 15
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?
 
Old 04-30-2003, 09:39 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
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 ;)

Cheers,
Tink
 
Old 05-01-2003, 12:22 AM   #11
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix, 98,2000 + various
Posts: 3,171

Rep: Reputation: 51
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: how to get the PID of programs started? J_Szucs Programming 11 01-16-2012 02:08 AM
Bash redirection being taken literally by programs R00ts Programming 13 05-26-2005 12:46 AM
Wine Problems: Not running any programs. euphoria Linux - Newbie 3 02-29-2004 02:23 AM
Programs from bash? MINDOFD Linux - Newbie 4 12-04-2002 03:38 AM
Problems on running java programs indro Linux - General 1 07-19-2001 11:34 AM


All times are GMT -5. The time now is 11:05 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration