Of course, if you do not assign a value to a local variable, this can cause the entire program to crash, because its contents are directed by the system to the garbage memory.
let's look at a piece of code here:
Copy Code code as follows:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int globle_value;
int my_sum (int value1, int value2);
long my_sub (long value1, long value2);
int main (void)
{
int auto_value_int;
long Auto_value_long;
auto_value_int = my_sum (15, 9);
Auto_value_long = my_sub (12587, 22587);
printf ("Globle_value:%dn", globle_value);
printf ("Auto_value_int:%dn", auto_value_int);
printf ("Auto_value_long:%ldn", Auto_value_long);
System ("PAUSE");
return 0;
}
int my_sum (int value1, int value2)
{
return value1 + value2;
}
long my_sub (long value1, long value2)
{
return value2-value1;
}
Description:
I first defined a global variable, which, of course, was automatically initialized by the system to 0, but two different types of local variables were not initialized, but were assigned values through two function calls. But now, to think of a problem, two function calls are not executed successfully? How can I judge if I do not succeed, or do not achieve the effect I want?
At first, bloggers did not think of a good solution, also consult others how to do, not too much harvest, however, the blogger thought of a C language function--sprintf, it can be different types of variables stored in the character array, we can then judge whether the character array is empty.
here is the modified code:
Copy Code code as follows:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int globle_value;
int my_sum (int value1, int value2);
long my_sub (long value1, long value2);
int main (void)
{
int auto_value_int;
long Auto_value_long;
Char temp[20] = {0};
auto_value_int = my_sum (15, 9);
Auto_value_long = my_sub (12587, 22587);
printf ("Globle_value:%dn", globle_value);
sprintf (temp, "%d", auto_value_int);
if (strcmp (temp, "")!= 0)
{
printf ("Auto_value_int:%dn", auto_value_int);
}
sprintf (temp, "%ld", Auto_value_long);
if (strcmp (temp, "")!= 0)
{
printf ("Auto_value_long:%ldn", Auto_value_long);
}
System ("PAUSE");
return 0;
}
int my_sum (int value1, int value2)
{
return value1 + value2;
}
long my_sub (long value1, long value2)
{
return value2-value1;
}
The screenshot of the run is shown below:
Thus, the problem was solved.