How is NULL different from 0 in C? [duplicate] - c

This question already has answers here:
Correct way of defining NULL and NULL_POINTER?
(2 answers)
What is the difference between NULL, '\0' and 0?
(11 answers)
Closed 4 years ago.
If these 3 lines are the only time NULL is mentioned in stdio.h, how is NULL different from 0 and most importantly what tells the compiler NULL is actually an invalid address?
Talking about C, maybe in other libraries, like in iostream, NULL is defined differently.
0085 #ifndef NULL
0086 #define NULL 0
0087 #endif

To the compiler, NULL is indistinguishable from 0 when used as a pointer. The standard actually defines 0 to be a null pointer value, and the standard library just introduces a convenient macro NULL defined to a null pointer value (usually 0 or ((void*)0)), so that you can use it in code for better readability and expressing intent. But there's nothing special about NULL itself; it's the 0 that is relevant.

To the compiler, all of 0, NULL and '\0' map to the same value. However, from the readability perspective, you may want to use the appropriate value depending on the context, to make it clear what you're trying to do and to improve the efficacy of code auditing and reviews.

Related

Is it essential to use a return 0 statement when we use int main() in c? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 5 years ago.
What will happen if we don't use it? will the compiler automatically add it like it does in C++?
It will return 0 automatically in C99-compliant compilers (and later). In pre-C99 compilers the return value is undefined in such cases.
In other words, in modern C you don't need an explicit return 0 at the end of main.

What is happening in this C code? (From interview) [duplicate]

This question already has answers here:
Square of a number being defined using #define
(11 answers)
Closed 5 years ago.
I was given a variation of this C code in an interview recently, and asked what would be returned by the function.
#define fun(a) a*a
int main() {
return(fun(4+5));
}
I've run it with a printf("%d"...) in place of the return and it prints 29. No other types than "%d" showed a result. Can someone explain what is going on here?
a is expanded, not evaluated as a. So you get:
4+5*4+5
and with operator priorities; you get 4 + 20 + 5 => 29
better way (with extra outside parenthesis for protecting against outside operators too thanks to Thomas comment):
#define fun(a) ((a)*(a))
but all parenthesis of the world still don't protect against i++, function calls (with side effects, preferably)... so argument reusing in macros is always a problem (there's no syntax to declare & assign a temp variable either). Prefer inlined functions in that case.

Difference between return values NULL and zero [duplicate]

This question already has answers here:
What is the difference between NULL, '\0' and 0?
(11 answers)
Closed 9 years ago.
What is the difference between return NULL and return 0 in C?
Yes, I know that 0 is a number and NULL is a void. But I don't understand the purpose of the two in context of programming.
Also I believe return 0 doesn't return the number 0 to calling function, but returns void. So what difference would it make if the statements return NULL and return 0 are interchanged?
In C
#define NULL ((void*) 0)
That means NULL is of type void* (a pointer without a type). And the value for it is 0.
There is no difference in assigning zero or NULL to a pointer variable. NULL may be just more readable.
See: http://c-faq.com/null/macro.html
When you are returning zero, you are really returning zero. If a function signature has void specified as a return value, it means that the function does not return a value. In some other languages this is known as a "procedure".

C if condition difference [duplicate]

This question already has answers here:
What is the difference between NULL, '\0' and 0?
(11 answers)
is NULL/false in C and C++ anything more than 0x0 / 0b0 / '\0' / 0 [duplicate]
(6 answers)
Closed 9 years ago.
While I was coding a program in C, I came up a question that I could not figure out. I was checking if a condition is met in an if statement but was wondering if there is any difference between the following:
if(ptr != NULL)
or
if(ptr)
To me, I feel like both of those are correct but in the C world, the second one would be used more and in the Java world, the first one is used more. Is one more correct then the other?
In C, anything that evaluates to 0 (zero) is "false", and anything non-zero is "true".
Thus when ptr is NULL, those two if conditions end up working the same way:
if (ptr != NULL) = if (0 != 0) = if (0)
and:
if (ptr) = if (0)
You'll get people debating which is better, but you'll see both in code. The first is more clear because it's more explicit. The second is shorter. Both are technically correct and equivalent.
Both are correct and equivalent.
A pointer alone evaluates to false if the pointer is NULL and to true otherwise.

Why out of bound array accessible in C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Array index out of bound in C
Can a local variable's memory be accessed outside its scope?
C no out of bounds error
I am trying out this code snippet,
#include <stdio.h>
main(){
int a[2],i;
a[5] = 12;
for(i=0;i<10;i++){
printf("%d\n", a[i]);
}
return 0;
}
It gives me output :
1053988144
32767
0
3
0
12
-1267323827
32716
0
0
Why a[5] is accessible ? Shouldn't it through RunTime Error?
C doesn't have bounds-checking on array access, so no it shouldn't. There is nothing in the language stopping you from attempting to read every (virtual) address you can imagine, although often the operating system and/or computer itself will protest, which might generate some kind of exception. Note that just because you "can" (it compiles and run, seemingly without any ill effects), that doesn't mean the program is valid or that it "works". The results of invalid memory accesses are undefined behavior.
int a[2]; means "allocate memory that is 2 * sizeof(int)"
a[5] is syntactic sugar for *(a + 5), which point to an area of memory a + (5 * sizeof(int)). So 3 * sizeof(int) past the end of your array. Where is that? Who knows?
Some languages do bound checking, and I have heard of some C compilers that can do it as well, but most do not. Why not do bound checking? Performance. And performance is a major reason for using C in the first place. But that's OK, because C programmers are good programmers and never go beyond the bounds of an array. (hopefully)
No control is performed on index bounds. This is simply the way C works.

Resources