Infrequently Asked Questions in comp.lang.c
Andrei Ellman

Just a bit of fun this - the comp.lang.c IAQ (Infrequently Asked Questions).

[url http://www.plethora.net/~seebs/faqs/c-iaq.html]

;D

AE.

Derezo

Some of those are pretty funny. Now, anyway.
When I was first learning C though, that page would have been helpful! ;D

Evert

I'm dumb!!

Quote:

10.7: Can I declare main as void, to shut off these annoying ``main returns no value'' messages? (I'm calling exit(), so main doesn't return.)

Certainly. You can also declare it as double. It may not compile, or it may crash, but who cares? No lousy bunch of whining lusers is going to tell you what to do.

CGamesPlay
Quote:

2.3: How does struct passing and returning work?
The structures are put into the low part of the VGA card's VRAM. They are then removed before the next video update. This is why struct passing was not supported for a long time; VGA cards were prohibitively expensive.

If you try to pass very large structures on the stack, you may see odd screen graphics.

So that's why my app wasn't working properly! :P

Bruce Perry

Ah, good. Just what I need to fend off the newbs. Andrei, I love you. ;D

23yrold3yrold
Quote:

1.6: I finally figured out the syntax for declaring pointers to functions, but now how do I initialize one?

With the assignment operator. You were perhaps expecting a screwdriver?

Heh :)

Quote:

1.8: What's the auto keyword good for?

Declaring vehicles.

Ha! In C++ we can build vehicle objects from scratch! C++ IS T3H B357357!!11`1

Quote:

2.10: Can I initialize unions?

Depends. They may go on strike when provoked. Luckily, if your program involves air traffic control, the ISO standard guarantees that Ronald Reagan will fire any unions that go on strike, and replace them with structs, which should be close enough.

;D

Bruce Perry
23yrold3yrold said:

Ha! In C++ we can build vehicle objects from scratch!

Reinvent the wheel, why don't you. ;)

runs

23yrold3yrold

runs down BP in his CTank: public CMilitaryVehicle: public CVehicle: public CAuto

Trumgottist

A public military vehicle? What is the world coming to?

Bruce Perry

My favourite so far:

"5.9: How would I initialize an entire array from standard input?

You have to use a loop. For instance, the following code reads the numbers zero through 99 into the array a.

for (i = 0; i < 100; ++i)
        a<i> = (scanf, ("%d", i));

Make sure to include <stdio.h>, or this may not work."

;D

Irrelevant

Ummm... That's just nonsense. :-/

Bruce Perry

No. That code works. Try it. ;)

23yrold3yrold

Actually, no. IIRC, scanf returns the number of successfully assigned values, not the values themselves. So it would be an array of a hundred one's.

Irrelevant

Exactly. Nyah. :P

It couldn't return the vals themselves, because then what would it return from:

int foo;
float bar;
scanf("%i%f", &foo, &bar);

Anyway, even if it did return the values, every loop it would ask you for a number and reset i.

...and you'd also get a typecasting error (from int to <b>int ). :P

As I said, it's nonsense. S'got too many holes.

It's still funny, though. ;D

CGamesPlay

"and reset i."
No, if you entered 99, it would stop the loop, if you entered <99, it would keep going.

Trumgottist

Note the spare comma and then see the note on that FAQ entry.

spellcaster
for (i = 0; i < 100; ++i)
        a<i> = (scanf, ("%d", i));

is equal to

for (i = 0; i < 100; ++i)
        a<i> = i;

read the code, Luke.

23yrold3yrold

Whoops. Missed the comma. :-X

That's messed up; someone want to explain to me what's going on there?

spellcaster

What's the problem?
scanf is called, the return value is discarded. "%d" is just a string and would return itself, if there woudln't be yet another comma which makes sure that i is returned.

Bruce Perry
Quote:

...and you'd also get a typecasting error (from int to int *). :P

No you wouldn't. It's one of the optional arguments, so a plain ANSI C compiler will just take its type at face value (after doing some promotions like char,short->int and float->double). GCC will warn if you want, because it knows how scanf and similar functions are supposed to work. However, it won't do any conversions; it would be violating the ANSI C standard if it did.

Don't worry guys, I didn't get it straight away either. I had to read the note too. ;)

OK, here's the deal. scanf is NOT called; all that happens is the code takes a pointer to scanf. When you get a comma, the left-hand expression is evaluated first and discarded (useful if it has a side-effect), and then the right-hand expression is evaluated. The whole expression has the value of the right-hand subexpression. Likewise, the "%d" (actually a pointer to such a string stored in read-only memory) is discarded, and the i is returned.

So, give or take a couple of 'left-hand blah of comma has no effect' warnings (not an accurate quote ;) ), it compiles and does exactly what it says on the tin. :)

PWNED. ;D

Andrei Ellman

Glad everyone seems to be enjoying the C IAQ.;D

After a bit of Googling around, I have found some IAQs for some other languages.

The following are also IAQs, but these are more serious than the above two.

Enjoy.

Thomas Fjellstrom

Bah. the perl one is somewhat lame... but I like th atoi subroutine ;)

# Q: How do I convert a string to a number?
# A: Use this atoi function:

        sub atoi {
          my $t;
          foreach my $d (split(//, shift())) {
            $t = $t * 10 + $d;
          }
        }
 
        $number = atoi("123");

Or:

# Q: How do I get the length of a variable?
# A: Use
        length('$variable');
# to find out how long a variable is.

Quote:

Q: Can I get a YACC grammar for the Perl language?
A: Sorry, but as you must surely be aware by now, the only animals supported by Perl are ruminants such as camels and llamas. However, Yacc support may be forthcoming with version 5.007.

Q: What are all those $@%* signs for?
A: Watch your $@*$!% mouth, buddy!

Ok Ok.. The ones near the end are pretty good... But the first few are totally lame.

Quote:

Q: How do I block warnings?
A: The simplest way is to do:
close STDERR;

Quote:

Q: How can I find out whether a number is odd?
A:
sub odd {
my $number = shift;
return !even ($number);
}

Q: How can I find out whether a number is even?
A:
sub even {
my $number = abs shift;
return 1 if $number == 0;
return odd ($number - 1);
}

Those are good ;D

:o

Quote:

Q: Perldoc isn't running properly. Where can I find the documentation?
A:
perldoc perldoc

Ok.. the next one is just bad. Funny. But just wrong.

Quote:

Q: What's the difference between single quoted strings and double quoted strings?
A: Single quoted strings act like q(); double quoted strings act like qq().

23yrold3yrold
Quote:

scanf is NOT called

That's what I thought; Spellcaster confused me :P

Thread #262904. Printed from Allegro.cc