What is going on here. Copy this C code , compile and run. What do you think is going on? Note I don't want the answer, I know what's going on here. This is just something for the rookies to play with...

Also this is not the best way to write code...

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

int* myint(void)
{
	static int myint = 9;
	fprintf(stdout, "myint now equals->%d\n", myint);
	return &myint;
}

int main(int argc, char**argv)
{
	*(myint()) = 12356;
	myint();

	exit(EXIT_SUCCESS);
}

>int main(int argc, char**argv)
If you don't use the command line arguments, why include them?

>exit(EXIT_SUCCESS);
Do you have any particular reason for calling exit rather than simply returning?

Narue's version

/*The Narue Version*/
/*As for why I include the extras(exit(EXIT_SUCCESS),int argc, char**argv)...I don't know must be habit*/
#include <stdio.h>

int* myint(void)
{
	static int myint = 9;
	fprintf(stdout, "myint now equals->%d\n", myint);
	return &myint;
}

int main()
{
	*(myint()) = 12356;
	myint();

	return 0;
}

You don't have to change the code (there's nothing wrong with it), I was just curious.

What is going on here. Copy this C code , compile and run. What do you think is going on? Note I don't want the answer, I know what's going on here. This is just something for the rookies to play with...

Also this is not the best way to write code...

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

int* myint(void)
{
	static int myint = 9;
	fprintf(stdout, "myint now equals->%d\n", myint);
	return &myint;
}

int main(int argc, char**argv)
{
	*(myint()) = 12356;
	myint();

	exit(EXIT_SUCCESS);
}

no replies??

i am not a rookie :(
in my very limited knowledge i can say since it is static , its life time is until the complete program terminates. since we get its address of that variable as the return and then we are modifying it, we get the updated value in the next calls...
but i know there s something else to it.. i want to know if there s something extra..

(I'm not sure if this is correct, but avert your eyes if you're still working on it)
**********************************************************

I think the statement in main is interpreted as a redeclaration of the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.