SWAP(A, B) where A and B are same variable always return A==B==0, so it can destroy some sorting algoritms, but there is no need to swap the thing with itself so one extra if statment fixes the problem. i lost an hour debbuging bc i was sure that one liner works, i hope u guys wont have to.
Hi... The SWAP method was really amazing... I'd love to learn about the math behind it... But let's first discuss about macro.. I'm a beginner in C so I don't have much knowledge... But as we know macros are preprocessor directives, and it simply copy paste the value in the code .. so for normal code I can understand it can replace...but will it work with dynamically allocated data? How will it replace the values that we haven't assigned yet and which will only be inputted by user during run time?
The values are still going to be evaluated at runtime. The preprocessor simply changes the code itself which then gets compiled and executed like any other
recently i came across a weird function declaration/definition in a linux kernel drive stub. something like : int __init functionName(parameters); Read somewhere that __init thing is also a macro. however i dont understand it atm.
It's a bit more complex. But the basic gist of it is those functions are marked as "initialization" functions and will be removed after the kernel is initialized. More info here: stackoverflow.com/questions/8832114/what-does-init-mean-in-the-linux-kernel-code
Mister CodeVault, let me first say that i admire your work. Your videos are very informative! On 7:38 you suggest that the swap macro can be used for any type as long as the two inputs are of the same type but when i try this on floats, for example, it fails.
I gotta question. thank you for explaining so clearly. Ho to you think NeuraLink devices use electrodes to sense brain neurin activities? Can think of how the code would look like?
It's probably just some embedded device and coded in a C-like language. I'm not sure what the exact code for it is, it might be super specific, but it's like any other program... takes input, processes it and returns an output.
Given a macro like #define FOO(x) (2*x-x) and then replacing x with a variable, lets call it var and make it equal to 10, plus a number, like FOO(var+5) instead of getting 15 out of it the program returns 20. I don't know if that is suposed to happen but i found it weird and couldnt find any explanation to it.
The explanation is simple. Preprocessor operations are done before the compilation step so, when the preprocessor sees: FOO(var+5) It replaces it (literally inside your code) with: 2*var+5-var+5 And then compiler evaluates this as a normal operation so, since var is 10 you get: 2*10+5-10+5 which is 20 To fix this issue we usually surround the parameter of a macro with paranthesis, like so: #define FOO(x) (2*(x)-(x)) So then this: FOO(var+5) Would be evaluated as: 2*(var+5)-(var+5) and since var is 10 you get: 2*(10+5)-(10+5) which is 15 So the main takeaway is that the preprocessor has NO IDEA what operation you're doing with what variables. It just literally copies and pastes characters so you have to make sure that, after the macro expansion happened, your code is what you expect.
Smh I cannot find a single documentation of the ^= operator, I have the latest gnu c manual and it's not even documented there either, nvm it's the "Bitwise exclusive disjunction" operator. I wish there was a video about all niche operators in gnu c / c99
Guess Im 2 years late but I was wondering if it is possible to create macros that are polymorphic. I have a weird case where I want to call a macro and have it behave differently based on how many args are passed in. For example: #define print_add(x, y) printf("%d", x + y); #define print_add(x,y,z) printf("%d, x+y+z); This is obviously pseudocode and not my actual use case but it is the same general idea. Is there any way to achieve this polymorphic behaviour using defines in C? If not what would be the best way to approach this problem?
Figured this one out. Next up is how to achieve polymorphic behavior based on what type is passed into the macro. Unfortunatrly I dont think its even possible given that this is a precompile time.
Oh interesting. I actually don't know how you achieved polymorphism using macros. Would be nice if you'd share the solution here. Otherwise, yeah, as I mentioned in other videos, macros have no idea about types so it would be tricky. Maybe... if you could check at runtime the type of your variables, that could work
Can I use macros to "rename" how you index an array element ; i.e you have an array that instead of being addressed: array[1],array[2],array[3],array[4] you want to be adressed like so: array[x1],array[x2],array[y1],array[y2] but you can't define "[x" to "[2*" and "[y" to "[1+2*" Multi-Dimensional arrays are a solution, so this is not necessary but it could be a very neat feature that would make code much more readable. Plus in my case it is already a 2d matrix and I would prefer to just use numbers over another dimension because it is used in a linear system. Thanks in advance, your videos are the best!
Interesting idea... Hmm, while you cannot parameterized the square brackets, you could try defining a simple macro like so: #define X 2 * #define Y 1 + 2 * Then you could use them like so (the extra space is needed): int arr[100]; arr[X 1] = 5; arr[Y 2] = 15;
Can you please tell me how to do a loop inside a Macro, For example I have x number of slaves to be connected to the master, For all these x devices i need to update Dev0_rr0, dev1_rr0, dev2_rr0,...devx_rro
Macros _are_ wonderful things that allow some degrees of code simplification/clarification but _buyer beware_ that you you are working with the compiler _preprocessor_ and that can lead to some unintended code being bound into the resulting object file. Recommend a new guy get into the habit of inspecting the _output_ of the preprocessor (available as a compiler option) to see what happens with your macros until you are comfortable with the process. But be warned! It is NOT a pretty sight.
You do amazing code , can you help me to be professional in c programming I need path to learn c language book or videos if assembly language is required to professional c lang can you help me to resources for assembly
And I would respectfully recommend that you NOT begin with the K&R "white book" as it is a little bit dated. Almost any decent tutorial available on TH-cam is fine and as you progress past the basics revert to this young gentleman's videos to understand the concepts he teaches. He is quite clear and his examples are not loaded with a lot of bullshit! Have patience, understand the principles, and have fun, and you will pick it up in short order!
a = 3 = 0b0011 ≡ 0011; b = 8 = 0b1000 ≡ 1000; a ^= b ^= a ^= b; this needs to be read from right to left: a ^= b; b ^= a; a ^= b; or more explicitly: a = a ^ b; b = b ^ a; a = a ^ b; recall that: 0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 at first: a = 0011; b = 1000; first xor: a ^ b = 1011; a = 1011; so now: a = 1011; b = 1000; second xor: b ^ a = 0011; b = 0011; so now: a = 1011; b = 0011; third xor: a ^ b = 1000; a = 1000; so now: a = 1000; b = 0011; but: a = 1000 ≡ 0b1000 = 8; b = 0011 ≡ 0b0011 = 3; the values of a and b have been swapped.
Almost 2 years later and I still visit your channel and watch some of your videos, and always learn something! Thank you !
I love how you explain things...I'll forever be grateful to you for my success in Software Engineering
I really liked the swap trick!
I was just struggling with this reading some else's code and this really helped me get up to speed. Thanks, great video.
This is the first video am watching to learn macros and it help alot.
Great explanation of macros with parameters. Thank you.
Thank you very much, your channel deserves more views!
I love your clear explanations!
This channel is great, keep it up.
You have a gift of teaching
from every video i learn at least one new thing
SWAP(A, B) where A and B are same variable always return A==B==0, so it can destroy some sorting algoritms, but there is no need to swap the thing with itself so one extra if statment fixes the problem. i lost an hour debbuging bc i was sure that one liner works, i hope u guys wont have to.
Hi bro ,is it possible to do a video explanation about ellipsis in C?
Thanks for share your knowledge
Can you also explain how to create macro with same name but with multiple parameters ... thanks :)
Hi... The SWAP method was really amazing... I'd love to learn about the math behind it...
But let's first discuss about macro.. I'm a beginner in C so I don't have much knowledge... But as we know macros are preprocessor directives, and it simply copy paste the value in the code .. so for normal code I can understand it can replace...but will it work with dynamically allocated data? How will it replace the values that we haven't assigned yet and which will only be inputted by user during run time?
The values are still going to be evaluated at runtime. The preprocessor simply changes the code itself which then gets compiled and executed like any other
recently i came across a weird function declaration/definition in a linux kernel drive stub. something like : int __init functionName(parameters); Read somewhere that __init thing is also a macro. however i dont understand it atm.
It's a bit more complex. But the basic gist of it is those functions are marked as "initialization" functions and will be removed after the kernel is initialized. More info here: stackoverflow.com/questions/8832114/what-does-init-mean-in-the-linux-kernel-code
thank you , its a good and helpful videos , Thank you
Nice video, can you do one also in Variadic Macros?
Yes, will look into it
Mister CodeVault, let me first say that i admire your work. Your videos are very informative! On 7:38 you suggest that the swap macro can be used for any type as long as the two inputs are of the same type but when i try this on floats, for example, it fails.
I think I meant to say any variables of an integer type
Very informative video.
I gotta question. thank you for explaining so clearly.
Ho to you think NeuraLink devices use electrodes to sense brain neurin activities?
Can think of how the code would look like?
It's probably just some embedded device and coded in a C-like language. I'm not sure what the exact code for it is, it might be super specific, but it's like any other program... takes input, processes it and returns an output.
Great video! Thank u so much!
Given a macro like #define FOO(x) (2*x-x) and then replacing x with a variable, lets call it var and make it equal to 10, plus a number, like FOO(var+5) instead of getting 15 out of it the program returns 20. I don't know if that is suposed to happen but i found it weird and couldnt find any explanation to it.
The explanation is simple. Preprocessor operations are done before the compilation step so, when the preprocessor sees:
FOO(var+5)
It replaces it (literally inside your code) with:
2*var+5-var+5
And then compiler evaluates this as a normal operation so, since var is 10 you get:
2*10+5-10+5 which is 20
To fix this issue we usually surround the parameter of a macro with paranthesis, like so:
#define FOO(x) (2*(x)-(x))
So then this:
FOO(var+5)
Would be evaluated as:
2*(var+5)-(var+5) and since var is 10 you get:
2*(10+5)-(10+5) which is 15
So the main takeaway is that the preprocessor has NO IDEA what operation you're doing with what variables. It just literally copies and pastes characters so you have to make sure that, after the macro expansion happened, your code is what you expect.
Smh I cannot find a single documentation of the ^= operator,
I have the latest gnu c manual and it's not even documented there either,
nvm it's the "Bitwise exclusive disjunction" operator.
I wish there was a video about all niche operators in gnu c / c99
Hmm, I never thought about such a video. I noted it down, will look into it!
Guess Im 2 years late but I was wondering if it is possible to create macros that are polymorphic.
I have a weird case where I want to call a macro and have it behave differently based on how many args are passed in.
For example:
#define print_add(x, y) printf("%d", x + y);
#define print_add(x,y,z) printf("%d, x+y+z);
This is obviously pseudocode and not my actual use case but it is the same general idea.
Is there any way to achieve this polymorphic behaviour using defines in C? If not what would be the best way to approach this problem?
In essence, i really just want to call a macro and have it return different functions depending on how many args are passed in.
Figured this one out. Next up is how to achieve polymorphic behavior based on what type is passed into the macro. Unfortunatrly I dont think its even possible given that this is a precompile time.
Oh interesting. I actually don't know how you achieved polymorphism using macros. Would be nice if you'd share the solution here. Otherwise, yeah, as I mentioned in other videos, macros have no idea about types so it would be tricky. Maybe... if you could check at runtime the type of your variables, that could work
Can I use macros to "rename" how you index an array element ; i.e you have an array that instead of being addressed:
array[1],array[2],array[3],array[4] you want to be adressed like so:
array[x1],array[x2],array[y1],array[y2]
but you can't define "[x" to "[2*" and "[y" to "[1+2*"
Multi-Dimensional arrays are a solution, so this is not necessary but it could be a very neat feature that would make code much more readable. Plus in my case it is already a 2d matrix and I would prefer to just use numbers over another dimension because it is used in a linear system. Thanks in advance, your videos are the best!
Interesting idea... Hmm, while you cannot parameterized the square brackets, you could try defining a simple macro like so:
#define X 2 *
#define Y 1 + 2 *
Then you could use them like so (the extra space is needed):
int arr[100];
arr[X 1] = 5;
arr[Y 2] = 15;
@@CodeVault that does makes more sense thank you very much
had my mind blown
thenks
Can you please tell me how to do a loop inside a Macro, For example I have x number of slaves to be connected to the master, For all these x devices i need to update Dev0_rr0, dev1_rr0, dev2_rr0,...devx_rro
There are some ways but I'm not sure exactly what you need it for. Here's some help: stackoverflow.com/questions/17866644/putting-loop-inside-c-macro
How to pass a value for macro during run time
You can't during runtime. Macros are evaluated and executed at compile time (preprocessor time to be exact).
Macros _are_ wonderful things that allow some degrees of code simplification/clarification but _buyer beware_ that you you are working with the compiler _preprocessor_ and that can lead to some unintended code being bound into the resulting object file. Recommend a new guy get into the habit of inspecting the _output_ of the preprocessor (available as a compiler option) to see what happens with your macros until you are comfortable with the process.
But be warned! It is NOT a pretty sight.
You have a video every year
You missed the opportunity to define macros that take other macros as parameters and define a macro that refers to another macro
I might do a video on that later on. I just tried to give a simple explanation for understanding macro parameters first
You do amazing code , can you help me to be professional in c programming
I need path to learn c language book or videos
if assembly language is required to professional c lang can you help me to resources for assembly
I can recommend you the book "The C programming language" created by the actual creators of C. You can start from there!
And I would respectfully recommend that you NOT begin with the K&R "white book" as it is a little bit dated. Almost any decent tutorial available on TH-cam is fine and as you progress past the basics revert to this young gentleman's videos to understand the concepts he teaches. He is quite clear and his examples are not loaded with a lot of bullshit!
Have patience, understand the principles, and have fun, and you will pick it up in short order!
a = 3 = 0b0011 ≡ 0011;
b = 8 = 0b1000 ≡ 1000;
a ^= b ^= a ^= b;
this needs to be read from right to left:
a ^= b;
b ^= a;
a ^= b;
or more explicitly:
a = a ^ b;
b = b ^ a;
a = a ^ b;
recall that:
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
at first:
a = 0011;
b = 1000;
first xor:
a ^ b = 1011;
a = 1011;
so now:
a = 1011;
b = 1000;
second xor:
b ^ a = 0011;
b = 0011;
so now:
a = 1011;
b = 0011;
third xor:
a ^ b = 1000;
a = 1000;
so now:
a = 1000;
b = 0011;
but:
a = 1000 ≡ 0b1000 = 8;
b = 0011 ≡ 0b0011 = 3;
the values of a and b have been swapped.
Thaaaaaank youuuuuuuu
#define foo(x, y) x / y + x
int main() {
int i = -6, j = 3;
printf("%d
",foo(i + j, 3));
return 0; }
pls explain this pls.
foo(i + j, 3) gets replaced by i + j / 3 + i + j and then gets printed on the screen. Note the order of operations and the missing parenthesis
@@CodeVault thank you very much. Love from India🇮🇳🇮🇳
🦾🦾🦾🦾🦾🦾🦾
#define PRODUCT(X) (X*X)
int main() {
int i = 3;
int j = PRODUCT(i++) ;
int k = PRODUCT(++i) ;
printf("%d %d", j, k) ;
}
Plz explain sir
Multiple increments on the same line are undefined behaviour
Husay
Dude Google how to pronounce integer
I know I know :D