How to determine whether variables such as int and long are assigned in C language

Source: Internet
Author: User
Tags printf

Bo Master this time to write some C program code, because before the C is not a lot of understanding, it encountered a lot of bottlenecks, of course, there are many interesting methods can be used to solve these problems, the following problem is the blogger encountered a trouble.

Declare the int, long type, and other local variables, after using some methods to assign values to these variables, to determine whether these variables are really assigned initial value, how to do?

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:

#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 (9);  
    Auto_value_long = My_sub (12587, 22587);  
      
    printf ("Globle_value:%d\n", globle_value);  
    printf ("Auto_value_int:%d\n", auto_value_int);  
    printf ("Auto_value_long:%ld\n", 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.

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.