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!
Any QUALITY is allowed. No bullying!
calloc
changes in the middle of me programming and I don't think to re-check the manpage?
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! */
}
#include <stdio.h>
int main(int argc, char** argv) {
int byte;
for(;; byte++) {
fputc(byte << (byte >> (byte >> 7)), stdout);
}
}
`
>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
$ cat autostudy2.sh
while true
do
espeak <terms.txt
done
int
one (void)
{
return 1;
}
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
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;
}
'\0'
check, even though the point of the method is to avoid conversion. 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? ... An integer character constant has type int
#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;
}
Yeah, once you think about it, it somehow makes sense:... An integer character constant has type int
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.char
can be signed or unsigned; this usually just annoys me and is probably one of my main uses of union
...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;
}
#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);
}
while((c = fgetc(f)) != EOF)in a system where char is unsigned and sizeof (char) == sizeof (int) EOF is a vaild character
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 problemvoid *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]);
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 voidI 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, putcharI 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 StyleGNU Style is shit.
Your scenario is ridiculouswhy?
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, 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''