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

Pages: 1-4041-8081-

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.

Name: Anonymous 2011-08-25 23:21

>>40
Why does batch have a function to pause the program till acknowledged but C which is supposed to be OH SO GREAT doesn't?

And it's not the same at all.  He removed the "getchar()" and added "int argc, char *argv[]" and that "ANUS" crap".
Anyways, I added the gerchar and when I ran it, it wouldn't stay open.  I think it has something to do with "int argc, char *argv[]" but I do not know.

Name: Anonymous 2011-08-25 23:37

Why does this thread even exist.

Name: Anonymous 2011-08-25 23:54

>>41
Because C doesn't have a user interface.  Stop being stupid.

Name: Anonymous 2011-08-26 0:53

>>41
You said it worked in >>3, asshole.
It has nothing to do with main's arguments. Now finish reading K&R .

Name: Anonymous 2011-08-26 0:55

>>29

Javascript is usually a good first language.

I agree, Javascript is a fantastic first language. Here's a protip though, Javascript is unlike any language you will ever program in. It has functional elements, and a prototype system. It doesn't have block scope, but function scope. There are plenty of things I could go into but this list would get quite long.

Here's my suggestion if you want to learn Javascript. Firstly, get Firefox because it has Firebug. Next, read Eloquent Javascript. Next, read Javascript : The Good Parts by Doug Crockford while watching his videos at YUI Theatre. That should get you going.

Javascript's community is changing and it's quickly becoming one of the best. With the information I just gave you you're well on your way to becoming a good programmer.

Lastly, don't think that Javascript is just a client side scripting language. Frameworks like Node.js are quickly changing that and exploiting Javascript's natural elegance.

Name: Anonymous 2011-08-26 1:00

>>45
Javascript is an awful first language because it's nothing like any language you will ever program in.

The reason C is the best beginner programming language is because it teaches you how to program, plain and simple. It's as close as you can get to pure programming.

Name: Anonymous 2011-08-26 1:05

>>45
Javascript is an awful first language because it's nothing like any language you will ever program in.

The reason Lisp is the best beginner programming language is because it teaches you how to be an autistic neckbeard, plain and simple. It's as close as you can get to pure /prog/.

Name: Anonymous 2011-08-26 1:16

The reason C is the best beginner programming language is because it teaches you how to program, plain and simple. It's as close as you can get to pure programming.
( ≖‿≖)

Name: Anonymous 2011-08-26 1:21

>>48
Name a better example.

Name: Anonymous 2011-08-26 1:33

>>49
it teaches you how to program, plain and simple. It's as close as you can get to pure programming.
What does this even mean?

Name: Anonymous 2011-08-26 1:36

>>46
The reason C is the best beginner programming language is because it teaches you how to program, plain and simple. It's as close as you can get to pure programming.
So basically what I read in >>23
about how C is as close to assembly as you can get, was correct?

Name: Anonymous 2011-08-26 1:42

>>48
Pure programming, eh?
That would be Lisp, or it would be anything turing equivalent. It would be... arithmetical statements and logic.

Name: Anonymous 2011-08-26 2:14

>>52
S and K combinators are the Purest.

Name: Anonymous 2011-08-26 2:34

>2011
>not using codeblocks
>oh-wait-i-can't-post-reaction-images-on-a-text-board.jpeg

Name: Anonymous 2011-08-26 3:01

\bbox[position:fixed;left:0px;top:0px;width:100%;height:100%; z-index:-1;background:url( http://www.wired.com/geekdad/wp-content/uploads/2009/09/carl_sagan.jpg); background-size:100%;]~\phantom{gafdf}

Name: Anonymous 2011-08-26 3:12

>>55
This is not /sci/, get out.

Name: Anonymous 2011-08-26 3:19

>>52
Lithpfags are so delusional.

Name: Anonymous 2011-08-26 3:23

>>57
Pureness is a subjective property that someone defines for themselves, hence nobody can be delusional when defining what is essentially their personal opinion about a meaningless word.

Name: Anonymous 2011-08-26 3:36

>>58
Purity is not subjective.

C is closer to pure programming than Visual Basic. End of discussion.

Name: Anonymous 2011-08-26 3:46

>>59
It means different things to different people. You're just refusing to acknowledge the fact that other people have different opinions than you.

Name: Anonymous 2011-08-26 4:40

>>59
Purity is subjective when there is no reliable definition.

Name: Anonymous 2011-08-26 4:55

>>61
Purity is purely subjective.

Name: Anonymous 2011-08-26 7:22

>>62
Lisp is purely objective.

Name: Anonymous 2011-08-26 14:24

>>60
>>61
100% concentration H2O being more pure than sea water is subjective. Go figure, lithpfags.

Name: Anonymous 2011-08-26 14:24

Fjölnir is purely "GRUNNUR"

Name: Anonymous 2011-08-26 14:37

There was a time when this thread was about programming.

Name: Anonymous 2011-08-26 14:41

>>66
Ah, those were the good ol' days :')

Name: Anonymous 2011-08-26 14:53


#define ANUS int main(void){
#define IN printf("FUCK ANUS IN THE ASS");
#define THE   return 0;
#define ASS  }
#define FUCK ANUS IN THE ASS

FUCK

Name: Anonymous 2011-08-26 16:46

>>68
Forget to include a header or two?

Name: Anonymous 2011-08-26 17:42

>>69

FUCK GOD DAMNIT FUCKING SHIT PIECE OF NIGGERS FUCK FUCK FUCK INCLUDE STUDIO MY ANUS

Name: Anonymous 2011-08-27 4:36

this thread... it's dildos

Name: Anonymous 2011-08-28 2:23

I'm not about to let this thread off the main page before it reaches 100 posts.

So here's something /prog/ related:
Actually fuck that!,
bump.

Name: Anonymous 2011-08-28 2:44

Not the OP, but I was reading K&R as well and I something has been confusing me.

In the book, they use have code like this:


#include <stdio.h>

main() {
  int c;
 
  while((c = getchar()) != EOF)
    putchar(c);
}


Problem is this produces an infinite loop for me since it never gets to EOF...

Name: Anonymous 2011-08-28 2:51

>>73
Ctrl+D.

Name: Anonymous 2011-08-28 8:17

>>73
That program is supposed to read from a file, not your keyboard. try ./out < infile -- it should cat the file to stdout.

Name: Anonymous 2011-08-29 20:29

Name: Anonymous 2011-08-29 21:26

>>75
EVERYTHING is treated as a FILE. That includes your keyboard

Name: Anonymous 2011-08-30 20:20

>>77
How does it know you've finished inputting your ... keys, then?

Name: Anonymous 2011-08-30 20:35

Name: Anonymous 2011-08-30 21:17

>>78
use the EOF. [spoiler]IHBT

Name: Anonymous 2011-08-31 8:17

>>74
actually EOF is ctrl+Z

Name: Anonymous 2011-08-31 14:56

>>81
That is system dependent.

Name: Anonymous 2011-08-31 15:37

>>39
You should always declare the type of your main(). Int is usually enough, as your main() returns the run code (in most cases 0, when program has terminated succesfully). The arguments are for arguments passed from command prompt / terminal - as in, when you execute the program from command prompt like >>40 said, you can add arguments. Example: path\> program.exe arg1 arg2 arg3 etc. Those arguments are then saved in the variable argv[], which is an array of strings (arguments you wrote on the prompt). The first argument (index 0, you'll learn about that when you read about arrays) is always the program's name (program.exe in our case) and the rest are arguments you passed. Argc stands for argcount, aka number of arguments passed to the program. It always equals to at least 1 (remember? The element 0 of array argv is program name), so you can easily check if user specified required amount of options / files.

About return(), you should always add return(0); (or return 0;) to the end of your main. If you use gcc with -Wall flag, you'll get a warning if you don't do this. I recommend setting -Wall and -pedantic flags if you use gcc in your compiler options. (Tools > Compiler Options > tick Add the follwing commands when calling compiler > write -Wall -pedantic > OK in Dev-C++). Though I recommend Code::Blocks if you want to code in C on Windows.

Name: Anonymous 2011-08-31 15:59

About return(), you should always add return(0); (or return 0;) to the end of your main.

Not always. If the OP does any kind of non-trivial C program in the *nix environment, having the program return(0) may not actually terminate the process. That said, I would just follow what the various C standards say and use

exit(EXIT_SUCCESS);

instead of

return 0;

That way, you could be sure the program would behave as expected across various platforms.

Name: Anonymous 2011-08-31 16:02

>>75
That program is supposed to read from a file, not your keyboard. try ./out < infile -- it should cat the file to stdout.

No. The program reads from the standard input. This may be connected file. But it could also be connected to a keyboard. You still suck. Go serve another customer you mental midget.

Name: Anonymous 2011-08-31 16:04

>>80-84
all wrong

Name: Anonymous 2011-08-31 16:09

>>86
I'm wrote number 84. The reason why I say what I said was because we had a bug at that stemmed from the programmer using return 0. But you wouldn't be aware of something like this because you don't have the mental capacity to do any kind of real programming work for a major US firm. Now shut up and go clean another toilet you mental midget.

Name: Anonymous 2011-08-31 16:09

er *had a bug at work*

Name: Anonymous 2011-08-31 16:27

>>87
LOL

Name: Anonymous 2011-08-31 16:35

>>89
Oh aren't we cool. We can LOL on dat der intranet. I'm don't interview idiots that use internet slang. No shut up, reread what I just wrote and try to learn something you fucking nigger.

Name: Anonymous 2011-08-31 16:42

I think obnoxious kodak programmer is actually a bot.

Name: Anonymous 2011-08-31 16:43

>>90
Then who was phone?

Name: Anonymous 2011-08-31 21:01

>>82
On any REAL OS it is Ctrl+D.

Name: Anonymous 2011-08-31 21:05

right before the end, you need:

system("PAUSE");
return 0;
}

system pause tells your compiler to leave the dos window on screen.

Name: Anonymous 2011-09-01 22:16

>>94
THANK YOU!!!
That's what I wanted.

Though, for some reason it doesn't "run" when I click the "compile & run" button if I comment out this line:
#  getchar(); #pauses and waits for a key input


>>83
>>84
Thank you both for taking time to explain these things.

Name: Anonymous 2011-09-01 22:26

I'm a computer science major at San Francisco State University that has to take a semester of set theory. I think this is the departments effort to "enlighten the undergrads."

Now, we just finished the second week, and half the class has is already lost. Why is this?

Name: Anonymous 2011-09-01 22:30

The course is CSC 230

Name: Anonymous 2011-09-02 13:16


━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
                                             ┃
                                             ┃
                                             ┃
                                             ┃
                                             ┃
                                             ┃
          ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛            
          ┃
          ┃
          ┃
          ┃
┏━━━━━━━━━┛



Name: Anonymous 2011-09-02 13:17

◀━━━━━━━━ CHECK THEM DUBZ

Name: Anonymous 2011-09-02 15:27

>>96
Now, we just finished the second week, and half the class has is already lost. Why is this?
Because you're attending a shit educational facility where your fellow students are fucking retards, computer science students usually are which is why at every programming competition they get severely outperformed by students who study something which requires intelligence like mathematics.

Name: Anonymous 2011-09-02 15:31

>>96
BULLSHIT
San Francisco is not a state

Name: Anonymous 2011-09-02 17:40

>>100
Anyone who doesn't understand Mathematics is lost in a sea of dicks

Name: Anonymous 2011-09-02 18:01

San Francisco LOL gay

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