How to make an intermediate printf()? [duplicate] - c

This question already has answers here:
How to pass variable number of arguments to printf/sprintf
(7 answers)
C: Passing variable number of arguments from one function to another
(5 answers)
Closed 2 years ago.
Suppose I have a function name my_print(int flag_1, /* what goes here? */).
Its objective is the same as printf(), but it should only print when flag_1 is "true".
How would I write the function's header and body?
Note: I know I can just write if (flag_1) printf(/*something*/);, but how would I put that into a function?

Related

Return string after changing it in C [duplicate]

This question already has answers here:
Function returning address of local variable error in C
(3 answers)
returning a local variable from function in C [duplicate]
(4 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 4 months ago.
I know, there are multiple questions about this on StackOverflow but I couldn't find a fitting solution for my problem.
I want to generate product codes (e.g. 12a, important: numbers and letters) and return them and printf. But when I do it in my main I get no result. When I printf("%s\n", pcode) in my generator function it works, but after return pcode I won't get the result.
char* generate(int num) {
char pcode[3];
... // generating code
printf("%s\n", pcode); // correct output
return pcode;
}
int main(void) {
printf("%s\n", generate(12)); // wrong output
}

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;

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

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.

C programming how to set fill to printf? [duplicate]

This question already has answers here:
Pad with asterisks in printf?
(3 answers)
Is there any setfill() alternative for C?
(5 answers)
Closed 5 years ago.
In the C programming language, I can specify spaces before the arguments to print out columns.
printf("%10i", 50);
How can I set the fill to be underscores or dashes instead?
________50

How to randomize numbers with no repeat in C? [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
Unique random number generation in an integer array [duplicate]
(9 answers)
Closed 8 years ago.
I want to randomize number in each element of array in a variabel. I currently use srand() function. But, with this function i could get a same number in two or more element of array.
the output of my program is
number[0]=6
number[1]=3
number[2]=8
number[3]=3
See, number[1] and number[3] has same value. How to prevent this thing happen?

Resources