Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Learning C through K&R book

Name: Anonymous 2011-08-24 21:12

I already compiled and ran my first code but there's this thing.
When I run it, cmd flashes and disapears.(I'm using Dev-C++ on windows to make it)
Anyways, I think there was a command or function to make the cmd pause till I press enter or something like that so that I am able to read whatever the output was, the thing is, I cant remember the name of the function or how to write it.
Can one of you tell me which it is, if there is one.
BTW here's my code:


#include <stdio.h>

main ()
{
  printf("Hello World!");
}

Name: Anonymous 2011-08-24 21:27

god forbid you flip back a page and find out

Name: Anonymous 2011-08-24 21:30

Add this after your call to printf:
getchar();

Name: Anonymous 2011-08-24 21:32

>>2
You mean in the book, or in /prog/?

Name: Anonymous 2011-08-24 21:39

If you're using Dev-C++, you're doing it wrong.

Name: Anonymous 2011-08-24 21:47

use geany, then you dont have to fool with any getchar()/<conio.h> bullshit

Name: Anonymous 2011-08-24 21:49

>>3
That worked, thank you.
>>5
What would you suggest I use in windows?
I was thinking of using VISUAL C++ 2010 EXPRESS but it looked to me like it only worked for C++.

Name: Anonymous 2011-08-24 21:49

>>3

getchar() considered harmful.

Name: Anonymous 2011-08-24 21:59

>>8
Seriously?
What do I use then?

Name: Anonymous 2011-08-24 22:02

>>9
Disregard >>8, getchar is the most portable solution. Also, he is a flaming homosexual.

Name: Anonymous 2011-08-24 22:24

>>8,9,10
Run it in the fucking terminal like it was meant to be, you idiot.

Name: Anonymous 2011-08-24 22:27

>>11
faggot detected. choke on a penis.

Name: Anonymous 2011-08-24 22:56

>>11
Being able to just click the RUN button and see the results of my endeavours instead of having to type:

win+R,
cmd,
G:\prog\Ch1\,
Project1.exe

seems like a better use of my time.

Name: n3n7i 2011-08-24 23:05

Rather than just double-clicking your compiled exe, you could run it from inside a cmd Prompt...?

Eg C:/Dev-C> myProg.exe

Name: n3n7i 2011-08-24 23:09

=) Make a shortcut to cmd

Properties > -Start in "name_of_this_directory/" (save repeat chdir)

Doubleclick shortcut
myprog.exe

...
exit

Name: n3n7i 2011-08-24 23:13

i use a compile batch too =)

"ex.bat"
tcc -run examples/%1


...shortcut to prompt
C:/tcc/> ex myProg.c

exit

...Still end up using getch() though =)

Name: Anonymous 2011-08-24 23:25

What does the
"/%1"
#and
getch()

do?

Name: Anonymous 2011-08-24 23:30

This thread is too advanced for /prog/.

Name: i_have_a_raging_boner !!kCq+A64Losi56ze 2011-08-24 23:43

>>17
It implies that you're too stupid to use your IDE.

Name: Anonymous 2011-08-24 23:59

20 GET

Name: Anonymous 2011-08-25 1:10

You could try Code::Blocks if you're not willing to use the terminal. (If you are try MinGW.)

It keeps your program's window open after it terminates.

And getchar() is a horrible solution. Like >>11 says a console program is meant to run in a console. Putting a getchar at the end is a pointless hack.

What are we going to do when the OP comes back complaining the getchar method doesn't work sometimes when there's a scanf in his program? I'd rather avoid any discussion of scanf.

Name: Anonymous 2011-08-25 1:33

<Check them doubles

Name: Anonymous 2011-08-25 1:56

>>21
What was that?
You wanted a discussion about scanf?

YOU GOT IT!

But seriously, you got me intrigued. And besides knowing is half the battle as they say... or something.

Anyways, you got me thinking and searching and I'm starting to wonder if I should really try to learn C or just go to C++ because of this quote I found while doing some more searching:
Maybe it's time to consider another language. When Kernighan and Ritchie originally developed C in 1978, it was considered functional, but low level. Now, it is the primary alternative to assembly language.[1]
[1]: http://www.astahost.com/info.php/Programming-Windows_t17949.html
What do you think?

Name: n3n7i 2011-08-25 2:14

>>17
>> C:/tcc/> ex.bat (<<Script) "myProg.c" (<< Argument 1 // %1)
Calls
tcc.exe -run examples/myProg.c
^Compiler ^switch ^Dir ^File

Just use scanf then?

char c;
scanf("%c", &c);
printf("Entered Char %c", &c);

Name: Anonymous 2011-08-25 2:59

*printf("Entered Char %c", c);

Name: Anonymous 2011-08-25 3:22

>>13
In case you don't know already, everyone who has not moved to *nix should have a "terminal here" context menu item.

Name: Anonymous 2011-08-25 3:39

>>26
Well what do you know!
shift+right click
I learn new things on /prog/ everyday!
Still, I still have to type the name of the file when just clicking run would be much much faster.

Name: Anonymous 2011-08-25 4:05

>>27
Try [first few letters] + tab

Name: Anonymous 2011-08-25 5:02

>>23
Hmmm... I had written a long reply to this, in which I argued C isn't a portable assembler, and contemplated the nature of programming languages and paradigms, but decided not to post it. You're only at hello world level (no offense intended; it's the truth), and this board is overflowing with spammers and shit-posters anyway.


C or just go to C++
There's a pretty distinct divide in opinion amongst programmers on this. (The ones who want to fit in will pick C having never used C++ or even given it a thought.) The best you can do is learn a language you have a reason to learn. That way you'll be able to motivate yourself, whether it be C, C++ or some other.
After that you'll have to be genuinely interested.

Javascript is usually a good first language, because it can be used to extend your browser (which you probably spend a large amount of time at).


scanf
It doesn't mix well with line buffered consoles. A catch-22: you need to already know you're at the end of a line in order to get to it without risk of retrieving the next line.


Also, /prog/ is being surprisingly Windows tolerant. What's up with that?

Name: Anonymous 2011-08-25 5:50

>>29
Reddit.

Name: Anonymous 2011-08-25 8:00

>>30
/v/

Name: Anonymous 2011-08-25 10:08

>>29
You're only at hello world level (no offense intended; it's the truth)
None taken.  I actually have some VB background but I didn't bring it up because this is /prog/ and from what I've seen you guys don't seem to like thems kind around here.

Name: Anonymous 2011-08-25 13:07

Kaploosh!

Name: Anonymous 2011-08-25 16:48

>>32
>I actually have some VB background
GET THE TORCHES AND PITCHFORKS!

Name: Anonymous 2011-08-25 17:09

>>34
An imageboardian, here‽ GET THE TORCHES AND PITCHFORKS!!

Name: Anonymous 2011-08-25 17:09

>>34
Upvoted for catering to general population.

Name: Anonymous 2011-08-25 21:00

I use getch() all the time when I make programs for my coworkers that require them to look at that output and don't want to have to explain to them how to do it from the command prompt.

There is really nothing wrong with it, and in some circumstances it can save you a lot of time.

Name: Anonymous 2011-08-25 21:08

>>1
Found your problem. You need to declare your main function's return type.

Here try this.


#include <stdio.h>

#define ANUS 12

int main(int argc, char *argv[]) {

  printf("Hello World!\n");
 
  return(ANUS);
}


That should do the trick.

Name: Anonymous 2011-08-25 21:40

>>38
The arguments and the type for main() wasn't in the book so I didn't put that there.
Mind if I ask why you put those there and why those seem to stop the getchar() thing from pausing the cmd so I can see the result of my code?

Name: Anonymous 2011-08-25 22:14

>>39
The code posted in >>38 won't be any different than what you did.

Basically when you run a program, like you did, it will open up an instance of the command prompt (if you're on windows), run the code, and then when it's finished it will close that command prompt because there is no need for it to stay open when it's done.

You can get around this one of two ways. Using getchar() will wait for you to enter a character. You're not actually doing anything with that character, you're just exploiting the fact that the program is stalled and you can see what was output.

The second, and better, way to do it is to open your own instance of the command prompt. Navigate to the folder your program is in by entering "cd C:\path of your program\".
Then enter "program.exe" to run that program.

Don't bother hitting compile and run in the second case, just compile.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List