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

Every time you visit for the FIRST TIME today... [Part 1]

Name: Anonymous 2013-10-20 22:56

Post a random function that you made. May be from any project you've done or make one impromptu.

Any QUALITY is allowed. No bullying!

Name: Anonymous 2013-12-11 23:26

>>120
That's not GNU-style - the indentation is two characters, not three!

Name: VIPPER 2013-12-12 5:39

>>121
GNU-style uses two characters indentation!

Name: Anonymous 2013-12-12 6:01

>>122
That's odd, indent -gnu does, in fact, use two space indentation.

This doesn't make sense. I distinctly remember that GNU style uses tabs three spaces wide. If my memory is faulty on this, what else have I forgotten? Will all the doorknobs switch sides again? What if the semantics of calloc changes in the middle of me programming and I don't think to re-check the manpage?

Thanks a lot, /prog/. Now I'm not going to be able to sleep for weeks.

Name: VIPPER 2013-12-12 6:38

>>123
nice get

indentation = 2 spaces
tab = 4 indentation

example of GNU-style code:

if ((thing || thing2 || thing3 || thing4 || thing5)
&& (thing6 || thing7 || thing8 || thing9 || thing10))
if (1)
{
yes_i_use_goto: /* check the special indentation for labels! */
niggers ();
yes_i_use_goto2:
more_niggers ();
if (1)
omg_tabs (); /* check the tab! */
}

Name: VIPPER 2013-12-12 6:40

>>123
manpage
man is sexist! use info.

Name: Anonymous 2013-12-12 10:16

A cute little noise maker


#include <stdio.h>

int main(int argc, char** argv) {
int byte;
for(;; byte++) {
fputc(byte << (byte >> (byte >> 7)), stdout);
}
}


I was trying to rewrite one I had found on /prog/ or was it /jp/? If you recognize it, could you post the original?

Name: VIPPER 2013-12-12 11:54

>>126
`
>char**, not char *argv[] or just int main (void)
>not giving a value to byte, not even byte = rand()
>not knowing that for(;; byte++) causes undefined behavior
>not returning 0, EXIT_SUCCESS or EXIT_FAILURE (ignore this if you are using C99 or C11 but in that case you should be using for (int byte = value; expr; byte++)
>not checking for fputc errors

Name: Anonymous 2013-12-12 12:15

>>127
Stop calling yourself a VIPPER when you act like an imageredditor.

Name: VIPPER 2013-12-12 12:30

>>128
also, you made me cry ;_;

Name: Anonymous 2013-12-12 12:50

Name: Anonymous 2013-12-12 13:01

/prog/ - reddit discussion

Name: VIPPER 2013-12-12 13:06

>>132
nigger

Name: Anonymous 2013-12-12 17:44

A post has been deleted. Don't make posts like that again.

Name: Anonymous 2013-12-12 18:29

>>134
Why didn't you didn't delete >>129?

Name: Anonymous 2013-12-12 18:51

>>135
Err, I mean >>127.

Name: Anonymous 2013-12-12 19:03

>>127,131
Since when is mememtexting an ``acceptable'' joke here?

Name: Anonymous 2013-12-12 21:43

>>135
Difference between stupid but ignorable, and absolutely insufferable.

Post >>127 at least had something related to programming in it.

I don't really check the board that often, but if I spot posts like >>129 again they'll be blacklisted.

Name: VIPPER 2013-12-12 22:09

>>138
Love you mod

Name: VIPPER 2013-12-12 23:08

>>138
wait! why didn't you deleted >>128,132?

Name: Anonymous 2013-12-12 23:40

>>140
I don't want to delete too many posts. 1 every so often is okay, but I dislike removing large swaths of posts (unless it's just one person posting 10 - 20 times in a row).

I was thinking of deleting those as well, since it annoys me when reddit keeps getting mentioned, but meh.

Name: Anonymous 2013-12-13 19:05

i'm studying for my music trivia final, here's what my high tech auto study code looks like:


$ cat autostudy2.sh
while true
do
espeak <terms.txt
done

Name: VIPPER 2013-12-19 3:45


int
one (void)
{
return 1;
}

Name: Anonymous 2013-12-22 12:23

module NanoTime where
import System.Clock

getNanoTime :: IO Integer
getNanoTime = do
tspec <- getTime Realtime
return ((s tspec) + (n tspec))
where s = (*(10^9)) . fromIntegral . sec
n = fromIntegral . nsec

Name: Anonymous 2013-12-24 7:29

int strcmp(const char *u, const char *v) {
union {const char *c; const unsigned char *u;} a, b;
for(a.c = u, b.c = v; *a.u == *b.u && *a.u != '\0'; a.u++, b.u++);
return *a.u - *b.u;
}

Name: Anonymous 2013-12-24 9:32

>>145
uuuuuuuuuuuuuuuuuuuuUUUuuuuuU!

Name: Anonymous 2013-12-24 17:02

>>145
Out of curiosity, why does the comparison need to be between unsigned values? I may be missing something, but that seems completely unnecessary, and I think it suffers from implicit char <-> unsigned char conversion at the '\0' check, even though the point of the method is to avoid conversion.

Name: Anonymous 2013-12-24 22:40

>>146
Am I kawaii? u.Ug.u~

Name: Anonymous 2013-12-25 15:22

>>147
Out of curiosity, why does the comparison need to be between unsigned values?
Don't really remember actually, it's been some time since I wrote this but I *think* I ran it through a test suite and it failed 1 test or so (out of 15) and the union fixed it somehow.
implicit char <-> unsigned char conversion at the '\0'
Wouldn't 0 and '\0' be both int or what do you mean? Suggestions welcome. Also, have you even READ DA STANDARD Section 6.4.4?

Name: Anonymous 2013-12-25 19:52

>>149
Well, if it passed a test there certainly is a difference. I was just interested what that difference is, since it wasn't immediately obvious.

My [b]STANDARD[/b] doesn't have a section 6.4.4 (I'm just looking at the ANSI C standard now, I'll check the others when I'm on a machine with access to them), but section 3.1.3.4 Character constants says

... An integer character constant has type int

So that's pretty unambiguous - I mistakenly thought it would be char.

Actually, though, it seems the difference is solely in the return statement, and shows up when one of the final characters doesn't have the sign bit set, but another does.

#include <stdio.h>

int diff1(const char a, const char b)
{
return a - b;
}

int diff2(const char a, const char b)
{
const unsigned char au = (const unsigned char)a;
const unsigned char bu = (const unsigned char)b;
return au - bu;
}

int main(void)
{
const char *str1 = "abCθDξ";
const char *p = str1;
for (++p; *p != '\0'; ++p)
printf("diff1(%c, %c) = %d\ndiff2(%c, %c) = %d\n",
*p, *(p-1), diff1(*p, *(p-1)), *p, *(p-1),
diff2(*p, *(p-1)));
return 0;
}


$ ./a
diff1(b, a) = 1
diff2(b, a) = 1
diff1(C, b) = -31
diff2(C, b) = -31
diff1(<CE>, C) = -117
diff2(<CE>, C) = 139
diff1(<B8>, <CE>) = -22
diff2(<B8>, <CE>) = -22
diff1(D, <B8>) = 140
diff2(D, <B8>) = -116
diff1(<CE>, D) = -118
diff2(<CE>, D) = 138
diff1(<BE>, <CE>) = -16
diff2(<BE>, <CE>) = -16


That certainly seems like something somebody would put in a test suite.

Name: Anonymous 2013-12-25 22:00

... An integer character constant has type int
Yeah, once you think about it, it somehow makes sense: char only seems to be used as string (char*) inside the stdlib and single characters are int -- I guess because of things like EOF, which is sometimes defined as -1, which might overlap in the code page with some other character if it were 8 bit.
The real problem to me is that char can be signed or unsigned; this usually just annoys me and is probably one of my main uses of union...

Anyway, new day, new function:
void* memset(void *p, int v, size_t n) {
unsigned char v_, *p_;
for(v_ = v & 0xFF, p_ = p; n--; p_[n] = v_);
return p;
}

Name: Anonymous 2013-12-26 18:22

Is there any reason this is bad practice?


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

void *error(char *f, char *fmt, ...) {
va_list args;
va_start(args, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);

if(f)
perror(f);

exit(1);
}

int main(int argc, char **argv) {
int c;
FILE *f;
(f = fopen(*++argv, "r")) || error("fopen", "Couldn't open %s", *argv);

while((c = fgetc(f)) != EOF)
putchar(c);

return(0);
}

Name: VIPPER 2013-12-26 18:48

>>152
while((c = fgetc(f)) != EOF)
in a system where char is unsigned and sizeof (char) == sizeof (int) EOF is a vaild character
better use while((c = fgetc (f)) != EOF || (!feof (f) && !ferror (f))

exit (1);
you cause a implemented defined behavior by returning 1, the standard values are 0, EXIT_SUCCESS, EXIT_FAILURE

fprintf(stderr, "\n");
since \n is a single character I would use fputc ('\n', stderr); but this is not really a problem

void *
you don't return something from that function, you should retrun void and in C11 you can add _Noreturn with the void

(f = fopen(*++argv, "r")) || error("fopen", "Couldn't open %s", *argv);
what if *++argv == NULL? undefined behavior. you must check it. I would use if (argv[0] && argv[1]) (f = fopen (argv[1], "r")) || error ("fopen", "Couldn't open %s", argv[1]);

In my opinion using a error function that destroys the progam is bad for two reasons:
1) the program must start from main and end in main and each function should have one return, using exit() makes my cry ;-;
2) in most cases closing the program on a error is a bad idea, it can create data loss. The worst is what it's used in libraries

other problems:
you don't check the return value of fprintf, vfprintf, putchar and you don't do anything if fgetc (f) == EOF && ferror (f), you don't use GNU Style

Name: Anonymous 2013-12-26 22:45

>>153
Ok All of these are fucking stupid.
better use while((c = fgetc (f)) != EOF || (!feof (f) && !ferror (f))
No. K&R2 goes through the entire fucking book using the style used in >>152. Your scenario is ridiculous

you don't return something from that function, you should retrun void and in C11 you can add _Noreturn with the void
I need to return something or it would not have compiled since I used error in an expression and the value was not ignored.

what if *++argv == NULL?
Then fopen would have failed and error would have caught it.

In my opinion using a error function that destroys the progam is bad for two reasons:
Your opinion isn't bad practice. exit exists to be able to terminate the process from any function. Your code may have to look much worse if you insist on exiting through main.

you don't check the return value of fprintf, vfprintf, putchar
I don't need to check the return value. If it failed writing the output than nothing can be done. What am I going to do if it fails writing the error? Try writing another error?

you don't use GNU Style
GNU Style is shit.

Name: VIPPER 2013-12-26 23:31

>>154
Your scenario is ridiculous
why?

Then fopen would have failed and error would have caught it.
Not really, it's undefined behavior (last time I checked the sandard, it may have changed)

Your code may have to look much worse if you insist on exiting through main.
yes same goes for goto

also please don't try to attack me, I have weak feelings ;-;

Name: Anonymous 2013-12-26 23:38

>>155
Looks like technically passing NULL to fopen is undefined though every implementation I've seen handles it fine.

Also, are you saying that you should never use goto either? Because forward error handling is an example of decent goto use.

Name: VIPPER 2013-12-27 1:22

>>156
Also, are you saying that you should never use goto either?
No, I would use goto AND exit() when I should (in this case for example). I am just saying that they are ``bad practice''

Name: Anonymous 2013-12-27 1:59

This is the first time I've ever wanted to filter someone on /prog/ ][.

Name: VIPPER 2013-12-27 2:57

>>158
why? anyway, feel free but you hurt my feelings

Name: VIPPER 2013-12-27 3:01

>>158
Anyway, I couldn't wait for better reply by a pedophile who sages threads

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