Question on C

Yohhan

Senior member
May 17, 2002
263
0
0
I'm having the strangest problem with C. I'm using Redhat 8.0, and I'm trying to run the simplest of programs:

#include <stdio.h>

int main(void) {
printf("hi");
return 0;
}


I compile with gcc hi.c which puts the object code into a.out

Then I try to run a.out, but I get no output whatsoever to the screen. It just gives me the prompt again. I can't for the life of me figure this out...
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Hm. How are you running a.out? gcc doesn't give any errors? Try doing 'echo $?' immediately after you run gcc, if it's non-zero, then something bad happened with gcc. Try running a.out in gdb and see what happens.
 

AEB

Senior member
Jun 12, 2003
681
0
0
i have used this compiler a little and seems like you are doing it ok. whereas im not a C buff in C++ yu wouldnt write
int main(void){ it would be like int main(){ and i usually #include<iostream.h> but like i said i dont know syntax for C
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: AEB
i have used this compiler a little and seems like you are doing it ok. whereas im not a C buff in C++ yu wouldnt write
int main(void){ it would be like int main(){
int main() is fine, although I don't know much about the difference.

and i usually #include<iostream.h> but like i said i dont know syntax for C
iostream.h has the c++ stream stuff like cout, cin, etc, which are usually used for input/output in c++. In c, you use printf, getchar (? I guess), etc. Those functions are in stdio.h. You can even use them in c++ if you #include <cstdio> (I believe that's the right syntax )
 

Yohhan

Senior member
May 17, 2002
263
0
0
How would I run a.out in gdb? I'm not sure how to do so.

Strangest thing. I add a \n to the end of the string and it prints now. I take it out again, and it doesn't... If anyone knows the reasoning behind this I'd be interested to hear it.
 

AEB

Senior member
Jun 12, 2003
681
0
0
in c++ when doing strings the compiler can usually guess when the string ends, there is a sort of built in terminating character the /0. i am gussing that yuo adding the /n is terminating the string so the compiler see it has a finite length. computers hate infinite things,
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Yeah, try printf("%s", "hi");

In this case, "hi" will be interpreted as a NULL terminated string and should be printed correctly.
 

manly

Lifer
Jan 25, 2000
12,888
3,660
136
Originally posted by: BingBongWongFooey
Originally posted by: AEB
i have used this compiler a little and seems like you are doing it ok. whereas im not a C buff in C++ yu wouldnt write
int main(void){ it would be like int main(){
int main() is fine, although I don't know much about the difference.
C was an interestingly different language before it was standardized by ANSI. It had a variety of implicit defaults, partly because of the lack of function prototypes.

In C, if the function parameter list is empty, that actually means you can call the function with any or no arguments at all (this is different from varargs). Any arguments would be ignored since you couldn't identify them. So that's why C's function(void) is equivalent to C++'s function(). Similarly, functions were assumed to return int unless specified otherwise.

AFAIK, ANSI C tried to remain as K&R C (1st Edition) compatible as possible, but I don't know how common pre-ANSI C code would fare with an ANSI C compiler.

To use gdb, first compile with debugging symbols by passing the -g flag to gcc. To perform an actual debug session, you would invoke:

gdb a.out

gdb is a CLI tool so you'll need to consult a manual to figure anything out. If you've used a symbolic debugger before, then
you can get basic help from within gdb.

Offhand, I don't see anything wrong with the hello program shown. Nor do I have a RH Linux 8.0 system to test on. Wait a second, since you replied with an update, I think you should take a close look at the beginning of the shell prompt after running a.out. It probably looks like hi~yohhan/: $. Or whatever a bash prompt looks like, since I'm using Windows right now. In other words, "hi" is printed before the following shell prompt. '\n' would just add a newline to make things obvious.

Funny that AEB finds it appropriate to give C++ APIs as a solution to a C question.
 

Shuten

Member
Jul 16, 2001
116
0
0
Are you sure the program just isnt printing to the end of you command line. Like if you have your command line localhost/> and you type

localhost/> a.out

that program might print out

localhost/> a.outhi

So check that. I would bet that is your culprit
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Shuten
Are you sure the program just isnt printing to the end of you command line. Like if you have your command line localhost/> and you type

localhost/> a.out

that program might print out

localhost/> a.outhi

So check that. I would bet that is your culprit
What manly mentioned is definitely possible, but the output would not appear after your command, since your shell prints a newline right when you hit enter, before the command is actually run.
 

arcain

Senior member
Oct 9, 1999
932
0
0
Originally posted by: Yohhan
How would I run a.out in gdb? I'm not sure how to do so.

Strangest thing. I add a \n to the end of the string and it prints now. I take it out again, and it doesn't... If anyone knows the reasoning behind this I'd be interested to hear it.

The usual reason for that is because the output stream is buffered. The newline flushes the buffer. You can explicitly flush the buffer by doing:

fflush(stdout);

after your printf statement (of course that only flushes stdout, fflush can be used to flush any streams).

However it is odd that the buffer is not automatically flushed after your program returns (it should be).

Also what manly says is also the most likely oversight (no newline, so prompt would immediately follow output).
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |