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.
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.
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.
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.
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"
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,
Thanks for your responses.Code:int x; if(x = NULL){ printf("Hey! You should add something before showing results!"); } else { /* whatever */ }
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:
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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"
I initialized the x in 0, and an "if", something like this.
If it's different than 0 it means that the variables got data when the program asked for it in the other "case" statement.Code:int x = 0; if(x == 0){ printf("You should add something."); } else { */ shows data stored in the other variables /* }
Thanks for the idea.
Yes, but it would be more clear if you gave your variables meaningful names, like this:
See, that makes it much easier to read and figure out what the code is doing/supposed to do.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