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

Pages: 1-

Nested Functions in C

Name: Anonymous 2012-09-07 18:07

Do you do it? I do it because I think it makes the code neater.

Name: Anonymous 2012-09-07 18:15

No, but I've done it. I know Rob Pike does it because that's where I learned it from, and everybody knows that Rob Pike does every thing THE RIGHT WAYTM.

Name: Anonymous 2012-09-07 18:20

There are no nested functions in C. You must be talking about GCC or C++.
HIBT?

Name: Anonymous 2012-09-07 18:35

>>3
You really think GCC is the only compiler to support this?

Name: Anonymous 2012-09-07 18:48


int hax(int my){
  int anus(void){
     return printf("HAX MY ANUS\n");
  }
  return anus();
}

Name: Anonymous 2012-09-07 18:49

>>5
CLOSURES!

Name: Anonymous 2012-09-07 18:50

>>5
you'd make a great javascript hacker! :D

Name: Anonymous 2012-09-07 19:01

>>5
Why does hax take an int as an argument it never uses? Use warnings!

Name: Anonymous 2012-09-07 19:05

>>5
[code]#define hax(my) printf("HAX MY ANUS\n")
[b][i][o][u]ZOMG OPTIMIZED[/u][/o][/i][/b]

Name: Anonymous 2012-09-07 19:21

>>9
BBCODE failure and optimization failure in the same post?
#define hax(my) puts("HAX MY ANUS")

Name: Anonymous 2012-09-07 20:08

K&R a shit.

Name: Anonymous 2012-09-07 20:33


#define RANDUM_STRING "HAX MY ANUS"
static unsigned int randum = 0;

/* Algorithm: HAXMYANUS RANDUMB
 * Guaranteed to produce random numbers each time */
int rand(){
  int randum_num = rand_(strlen(RANDUM_STRING) % randum,randum);
  randum *= (randum+randum_num)*5
  return randum_num;
}

int rand_(int depth,int a){
  if(depth == 0) a;
  rand_(depth-1,(int)((a*a+depth)/2)
}

Name: Anonymous 2012-09-07 20:35

>>12

#define RANDUM_STRING "HAX MY ANUS"
static unsigned int randum = 10;

/* Algorithm: HAXMYANUS RANDUMB
 * Guaranteed to produce random numbers each time */
int rand(){
  int randum_num = rand_(strlen(RANDUM_STRING) % randum,randum);
  randum *= (randum+randum_num)*5
  return randum_num;
}

int rand_(int depth,int a){
  if(depth == 0) a;
  rand_(depth-1,(int)((a*a+depth)/2)
}


THERE WE GO, FIXED IT

Name: Anonymous 2012-09-07 20:35

K&R is a shit.

Name: Anonymous 2012-09-07 20:49

>>12-14
Fuck off Haskal fagstorm

Name: Anonymous 2012-09-08 19:00

As always the good threads die after shitty memes

Name: Anonymous 2012-09-08 22:21

>>16
good thread

there was nothing good here

Name: Anonymous 2012-09-09 3:36

I don't because I write in C, I do not write in GNU-C.

>>4
Yes.

>>5,6
That is not a closure, GNU-C nested functions as implemented in GCC does not work as closures.

>>10
Your optimization is invalid since puts might not return the same thing printf would do.

Name: Anonymous 2012-09-09 4:32

>>18
And in which cases would puts not return the same thing as printf with a newline added.

Name: Anonymous 2012-09-09 4:53

>>19
puts returns an unspecified positive number on success. printf returns the number of characters written.
Learn to read, retard.

Name: Anonymous 2012-09-09 5:49

>>20
And just what are you going to do if writing to STDOUT fails?

Name: Anonymous 2012-09-09 6:02

>>19,21
Are you somehow confusing what the function returns to what it writes to stdout?

Name: Anonymous 2012-09-09 7:17

Name: Anonymous 2012-09-09 13:41

>>22
Failing to write to STDOUT is the only reason why printf with a single string argument would return a value less than the length of the string. puts would return a negative value in this case. So, here's how to make it really equivalent:
#define hax(my) (puts("HAX MY ANUS") + strlen("HAX MY ANUS\n"))

Name: Anonymous 2012-09-09 19:29

fucking niggers

Name: Anonymous 2012-09-10 0:32

>>21
Write an error message to stderr, of course.  The most likely cause of a write to stdout failing is a disk filling up when stdout is redirected to a file on said disk.  If stderr is redirected somewhere else, your chances of recovery are much better.

Name: Anonymous 2012-09-10 10:12

>>26
How is knowing how many characters were written helpful in that case?

Name: Anonymous 2012-09-10 10:51

>>27
For recovery, you total mormon.

Name: Anonymous 2012-09-10 20:25

>>28
If stdout is fucked, it's fucked. You're not going to magically recover it. The best you can do is print an error to stderr and then either move on or exit instead of dicking about trying to fix stdout.

Name: Anonymous 2012-09-11 1:47

>>27
It isn't. You should be using ferror to check for write errors anyway.

Returning the number of characters written is really only useful when you need to do some accounting for complex format strings.

Name: Anonymous 2012-09-11 2:46

>>27
Whether it is useful or not doesn't matter, using puts(s) instead of printf("%s\n", s) is not a correct optimization.

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