The method of judging whether the variable of int,long type is assigned value in C language

Source: Internet
Author: User
Tags printf sprintf strcmp

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.