Thread: Check if a variable is null

  1. #1
    Registered User CatBrownie's Avatar
    Join Date
    Apr 2014
    Location
    Costa Rica
    Posts
    6

    Question Check if a variable is null

    Simple question, I have a variable "x".
    I want to know if that variable has nothing in it or null, to show a message that "You should add something".

    I've googled a lot but I didn't find something to help me with that.
    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To answer your question generically (and hopefully clearly enough):

    C doesn't have the general concept of null meaning a/any variable is unset or invalid. Only pointers can be null (the C language defines it as all upper case: NULL), where null is a special address used to signify that the pointer is not pointing to any valid address in memory. However, pointers are not initialized to null by default, you must do so if you want them initialized.

    There are more involved ways to create a systems where every variable keeps track of whether it's been used or not (e.g. reference counters), however it requires a lot of work and diligence on the programmers part, and a bit more advanced (or at least intermediate) skills.

    However, I think you have a very specific use case in mind, in which case you may be able to just have an extra flag variable you use that keeps track of whether variable x has a value yet. You initialize that flag to false, and once x gets a value, you set it to true. You check that flag, rather than x itself, to display the message.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Another generic way is to initialize x to a sentinal value which you know means "no value entered" for your program. For example, if your program does not require that a user be able to enter the value INT_MIN, then you could initialize x to this value and then use the test x == INT_MIN to determine whether a value has been recorded for x yet.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c99tutorial View Post
    Another generic way is to initialize x to a sentinal value which you know means "no value entered" for your program.
    I wouldn't describe that as generic, as a fairly important constraint is being able to identify a sentinel value that doesn't otherwise have meaning to your program.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It depends on the type. What type is x?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User CatBrownie's Avatar
    Join Date
    Apr 2014
    Location
    Costa Rica
    Posts
    6
    Quote Originally Posted by iMalc View Post
    It depends on the type. What type is x?
    Integer, I need something like, if some user wants to print a null variable, print that "Hey! You should add something before showing results!" or something like that.
    Few words,
    Code:
    int x;
    if(x = NULL){
    printf("Hey! You should add something before showing results!");
    }
    else {
    /* whatever */
    }
    Thanks for your responses.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    try this?

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
    	int x;
    	if(x != NULL)
    	{
    	printf("Hey! You should add something before showing results! \n");
    	printf("Input a value");
    	scanf("%d",x);
    	
    	}
    	else 
    	{
    	/* whatever */
    	}
    	return (0);
    }

  8. #8
    Registered User CatBrownie's Avatar
    Join Date
    Apr 2014
    Location
    Costa Rica
    Posts
    6
    Quote Originally Posted by Moody Barrawi View Post
    try this?

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
        int x;
        if(x != NULL)
        {
        printf("Hey! You should add something before showing results! \n");
        printf("Input a value");
        scanf("%d",x);
        
        }
        else 
        {
        /* whatever */
        }
        return (0);
    }
    Thanks for the response, just one question, why is with "!=" instead of "=", it's like if 'x' is different from NULL, if it's different, it means that the variable holds something. In that case, else should have the printf message. That's the way I understand it, I'm right or just speaking non-sense? D:

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Moody Barrawi View Post
    try this?

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
        int x;
        if(x != NULL)
    ...
    The result of this program is undefined, you must initialize x before testing for its value. Also to the OP, read the answers to the post first for ideas on how to accomplish your goal here. See post #2

  10. #10
    Registered User CatBrownie's Avatar
    Join Date
    Apr 2014
    Location
    Costa Rica
    Posts
    6
    Quote Originally Posted by c99tutorial View Post
    The result of this program is undefined, you must initialize x before testing for its value. Also to the OP, read the answers to the post first for ideas on how to accomplish your goal here. See post #2
    So...
    Code:
    int x = NULL;
    ?

    Right, now I understand, I'll test it and if goes wrong I'll post it.
    Thanks.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, NULL is a null pointer constant. It does not make sense to assign it to an int variable. You might as well just initialise x with 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CatBrownie View Post
    Integer, I need something like, if some user wants to print a null variable, print that "Hey! You should add something before showing results!" or something like that.
    Few words,
    Code:
    int x;
    if(x = NULL){
    printf("Hey! You should add something before showing results!");
    }
    else {
    /* whatever */
    }
    Thanks for your responses.
    Oh okay. Unfortunately an int type cannot be null. It can store the value of zero, but zero is a value just like any other number.
    Sometimes in that situation people will give the int an initial value which is not valid for the program, e.g. -1 for your height and then complain if the value is still -1 later on.
    Other times people use a separate Boolean flag variable to indicate if there is logically a useful value assigned to the int variable or not.
    You could also use a pointer to an integer but that will make the code messier, and dealing with pointers when you're that new to programming is generally going to confuse you more than it would help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User CatBrownie's Avatar
    Join Date
    Apr 2014
    Location
    Costa Rica
    Posts
    6
    I initialized the x in 0, and an "if", something like this.

    Code:
    int x = 0;
    if(x == 0){
    printf("You should add something.");
    }
    else {
    */ shows data stored in the other variables /*
    }
    If it's different than 0 it means that the variables got data when the program asked for it in the other "case" statement.
    Thanks for the idea.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CatBrownie View Post
    I initialized the x in 0, and an "if", something like this.

    Code:
    int x = 0;
    if(x == 0){
    printf("You should add something.");
    }
    else {
    */ shows data stored in the other variables /*
    }
    If it's different than 0 it means that the variables got data when the program asked for it in the other "case" statement.
    Thanks for the idea.
    Yes, but it would be more clear if you gave your variables meaningful names, like this:
    Code:
    int x_has_value = 0;  // initialize to 0/false
    int x;  // no initial value
    // do some stuff that may or may not give x a value
    if (!x_has_value)
        print "give x a value"
    else
        more things
    // do some other stuff that gives x a value
    scanf("%d", &x);
    x_has_value = 1;  // set to 1/true
    if (!x_has_value)
        print "give x a value"
    else
        even more things
    See, that makes it much easier to read and figure out what the code is doing/supposed to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign null value to a struct and check if it is null
    By ymc1g11 in forum C Programming
    Replies: 10
    Last Post: 11-01-2012, 03:58 PM
  2. Example from book - time(NULL) doesn't work
    By FernandoBasso in forum C Programming
    Replies: 6
    Last Post: 10-30-2011, 05:59 PM
  3. Check if string is null
    By doubty in forum C Programming
    Replies: 3
    Last Post: 07-04-2009, 03:10 PM
  4. How check a variable is null?
    By 6tr6tr in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2008, 07:36 AM
  5. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 08:16 AM

Tags for this Thread