Standard macro to calculate the number of elements in an array [duplicate] - c

This question already has answers here:
Is there a standard function in C that would return the length of an array?
(7 answers)
Closed 5 years ago.
Is there something like this available in the C standard library:
#define sizeofarr(a) ( sizeof(a) / sizeof(a[0]) )

No there is not such a thing!
For better Macro definition check this link directly:
is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array
or common-array-length-macro-for-c

No. There is not something like that available in the C standard library.

Related

usage of !!, __warned and __ret_warn_once as int in WARN_ON_ONCE macro definition [duplicate]

This question already has answers here:
What is "!!" in C? [duplicate]
(7 answers)
!! c operator, is a two NOT?
(4 answers)
Closed 3 years ago.
I was going through the WARN_ON_ONCE macro definition. I have doubt regarding the following line, what is the use of !! before condition. If we remove !! then also same will be stored in __ret_warn_once.
int __ret_warn_once = !!(condition);
What will happen when compiler executing the following source line.
static bool __section(.data.unlikely) __warned;

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.

C Preprocessor Instructions (SQR-Funktion) [duplicate]

This question already has answers here:
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 6 years ago.
Want to know how "11" is the answer of this c preprocessor instruction:
#define SQR(x) x*x
int main()
{
SQR(2+3);
}
Try expanding the macro manually.
It will be 2+3*2+3 and this is evaluated as 11.

Is there any function in C similar to LIKE in SQL to compare 2 strings? [duplicate]

This question already has answers here:
Regular expressions in C: examples?
(5 answers)
Closed 7 years ago.
I meant, something that we can use this way:
char string1[] = "???, buddy*\0";
char string2[] = "Hey, buddy, hello!\0";
if (like(string1, string2)
puts("strings are similar!");
else
puts("string are different!");
You want to use a regular expression library. See this question for the ANSI library information: Regular expressions in C: examples?

what is the output of this according to the macro definition [duplicate]

This question already has answers here:
Strange behavior of Macro-expansion
(3 answers)
Why is the return value not what I expected in this C program with macro? [duplicate]
(2 answers)
Closed 7 years ago.
#define A 1+2
#define B 4+3
int main()
{
int val = A*B;
printf("%d",val);
}
Here A and B is defined, and val is (A*B). What is the correct output?
Let's trace the expansion and calculation manually.
A*B -> 1+2*4+3 -> 1+8+3 -> 12
As a result, the output will be 12
This is a common issue with Macros. When you define something even as x+y in a macro it is advisable to first rap them in () so as to "protect" the operation.
As such it would be better to define A as (1+2) etc. Otherwise you get the output 1+2*4+3=12 as people have stated above.

Resources