Defining Constants. Should I use CONST or

  Рет қаралды 58,565

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер: 153
@TheCocoaDaddy
@TheCocoaDaddy 3 жыл бұрын
"Is that a constant?", "No, it's a const int". :) Great video! Thanks for posting!
@JacobSorber
@JacobSorber 3 жыл бұрын
😂 Beautiful. Thanks.
@pierreabbat6157
@pierreabbat6157 3 жыл бұрын
const int anople=537; istanbul=!anople;
@trefwoordpunk2225
@trefwoordpunk2225 3 жыл бұрын
It's not quite redundant acronym syndrome, but its close!
@StefanoTrevisani
@StefanoTrevisani 2 жыл бұрын
@@JacobSorber you should make a new t-shirt: "contant != constint" or something along this line, would be quite funny!
@ramesh.programming
@ramesh.programming 3 жыл бұрын
Yes, please make videos on function style macros...
@someperson9895
@someperson9895 3 жыл бұрын
THIS
@mason6300
@mason6300 3 жыл бұрын
please do...
@orisphera
@orisphera 2 жыл бұрын
Also #ifdef and similar ones Fun fact: both the shortest (#if) and the longest (#elifndef) pp directives are in this family
@RajeshKulkarni26
@RajeshKulkarni26 3 жыл бұрын
In embedded systems where memory is precious , coders prefer macros to avoid loosing memory caused by using variables
@HansBezemer
@HansBezemer 3 жыл бұрын
If you wanna go fast, the preprocessor is your friend ;-)
@sledgex9
@sledgex9 3 жыл бұрын
Modern compilers will optimize away the (static) const variable.
@EdwinFairchild
@EdwinFairchild 3 жыл бұрын
A topic I've seen often is declaring something as "const volatile" or "volatile const" , usually used when using something like a status register in embedded and you don't want it to be modified because it's read only and at the same time it is volatile because it can change at anytime and should not be optimized out in any way. C is life man! Love your videos!!!!
@JacobSorber
@JacobSorber 3 жыл бұрын
Yeah, thanks for pointing that out. The first time I saw something that was both const and volatile, I was sure it was a joke.
@EdwinFairchild
@EdwinFairchild 3 жыл бұрын
@@JacobSorber it does sound like an oxymoron
@maxaafbackname5562
@maxaafbackname5562 2 жыл бұрын
@@EdwinFairchild const meen "I" wil not modify it. volatile means that something else other than me will change it. Me versus something else.
@pumpkinhead002
@pumpkinhead002 3 жыл бұрын
You covered a lot of great points. Now, I know that embedded isn't always the focus of your videos; but in memory restricted systems, it is good to know the difference. I am working a bare metal system with very limited and sparse memory. we have dedicated ram space that is really small and dedicated flash space that is super large. Those are also divided up among the different sections of `.econst`, `.text`, etc. We use `const` to move data out of ram and into flash space, and use #define to move data out of `econst` and into `.text`. Thus managing how we fit our program into the space.
@grenadier4702
@grenadier4702 Жыл бұрын
#define BUFFER_SIZE 32 cosnt int BUFFER_SIZE = 32 results in: mov eax, 32 mov eax, 0xAF (the address of const int BUFFER_SIZE = 32) No difference, both commands take 24 bits, for example. In the latter case, you consume more memory and hurt perforamnce a tiny bit (if run your code without optimizations ofc)
@pumpkinhead002
@pumpkinhead002 Жыл бұрын
@@grenadier4702 the latter case requires 24 bits in .text for the instruction and address, but Also requires space in .econst to hold the data "32" for which you address. So it takes more memory and actually consumes a "permanent" amount of it in the constant data section The former method does not use const section and instead relies totally on the program instructions to assign the 32. This gives us more data back in the const section as well as allows the 32 to be "unloaded" from the program during runtime. Granted it still exists in the flash somewhere, but in my particular example, the flash was not contiguous and our text section was significantly larger than the const sections as well as flash access being very slow.
@grenadier4702
@grenadier4702 Жыл бұрын
@@pumpkinhead002 So there's no real poinmt in using const int instead of define in embedded systems? I see only downsides in doing that
@SoulSukkur
@SoulSukkur 3 жыл бұрын
couple of comments from my experience: 1) When I try to compile a program in C, I can't initialize an array when its dimensions are defined using a variable. 2) When I place a constant in a header file, i can get dozens of linker errors. The solutions seems to be to declare it as an extern, and then define it in a .c So yeah, I use #define.
@ailijic
@ailijic 3 жыл бұрын
For arrays, use an enum. For constants declare them with static. Don’t forget to use header guards.
@mohammedjawahri5726
@mohammedjawahri5726 3 жыл бұрын
I thought variable length arrays were legal as of some C version some time ago?
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
Header files are not a good place for application data. Their purpose is to "give the aroma" of datatypes and function prototypes to related "compilation units". So, yeah, a const int wouldn't be a good implementation if two compile units need to use a particular value. However, if your array's dimensions are dynamic (involving malloc), the header file can 'reserve' the token name (shared by all source files including that .H), and the linker will eventually make everyone happy. "Variable" usually means "can/will change value during execution. The compiler can't help you once it's done it's job...
@marcossidoruk8033
@marcossidoruk8033 2 жыл бұрын
@@rustycherkas8229 thats not what he meant. He meant you can't initialize an array if the dimensions are variables and it doesn't matter if those variables are const, the compilers just doesn't let you.
@TheMR-777
@TheMR-777 3 жыл бұрын
Lemme answer some of the "Unknowing" about "Constant" attributes :) 1. Where constant get saved? When we declare a Constant, it gets saved in the *Binary file* (executable file) and every time it's used in the program, it gets loaded from the Executable File your Compiler has generated. You can test this by viewing the Binary file in *HEX* Editor. 2. Why/How the changed constant value doesn't get changed? - The answer actually depends on the *Type of the Memory* being used. Usually, when we change a variable, the Data is first loaded from the RAM and gets loaded in the particular Register. Then it gets modified according to our input and gets written on that RAM Location. - Now, in the case of constant, the Data from Binary (constant variable) gets loaded into the register, then updates as we did. But after that, the Binary file doesn't get written with updated data, as it's the Read-Only for the Execution Unit. That's the reason it doesn't get updated. - But if the particular compiler settings are enabled, Constants get saved on the RAM instead of the Binary file, and that's the reason sometimes you'll see them Changed. Hopefully, I have cleared the point. If there's any confusion, ask it in the reply.
@a1nelson
@a1nelson 3 жыл бұрын
A lot of good coverage of the topic here. I feel that some mention of the impact on the memory footprint would been a worthy addition to the video.
@elclippo4182
@elclippo4182 3 жыл бұрын
Yes, especially on embedded platforms with small memory and Harvard architecture (instead of von Neumann architecture).
@pablo_brianese
@pablo_brianese 3 жыл бұрын
I recently was having doubts about this. I wondered whether declaring an int, even if const had a cost at runtime.
@makermatrix9815
@makermatrix9815 3 жыл бұрын
I just made this comment too. Would love to know the answer.
@nunyobiznez875
@nunyobiznez875 3 жыл бұрын
Values in a #define actually do have a type, they're literals of type int, unless the value has U, L, D, F, etc. I often also hear the c++ community say macros are bad because they aren't type safe, which seems a bit absurd to me, because macros are no more or less type safe, than if the value is typed in by hand directly. It's only when macros are misused or abused, that there are problems with them, and as long as people remember they're a simple cut and paste operation, they work exactly as advertised and can be quite useful.
@mohammedjawahri5726
@mohammedjawahri5726 3 жыл бұрын
umm if you say this then you don't believe in type checking *in general*, typechecking is meant to prevent careless human error, u might as well say "hmm we never need types lol, every programmer should remember exactly what he put in what variable at what time, if it's corrupted it's his fault" I genuinely don't understand why anyone would ever want to opt for a macro instead of a constant with modern compilers, just look at the assembly generated by both on high optimization, even function style macros are obsolete with how much compilers inline (especially if u give hints that u want it inlined). edit: I think you're right about the point with typechecking constant literals. i misunderstood what you meant, but that's not really what people tend to mean when they talk about typechecking and defines, from what I've seen most people comment on function style macros where typechecking arguments would save hours of debugging
@johnheaney3349
@johnheaney3349 2 жыл бұрын
@@mohammedjawahri5726 A #define symbol can be tested when conditional compiling. Also, it is not seen by the compiler if not used in code.
@edgarbonet1
@edgarbonet1 2 жыл бұрын
Re the scope of a const: It is worth noting that, in C++, a const in file scope is only visible from the translation unit defining it. In other words, it has internal linkage (it is implicitly `static'). In plain C, however, a const in file scope is reachable from the whole program: it has external linkage.
@JonnyRobbie
@JonnyRobbie 3 жыл бұрын
I don't know. I still feel like you've "only" explained the difference, but haven't gotten to the meat of the question you teased in the title. Ok, now I now the difference, but WHEN should I use one and when the other?
@lycorisdev
@lycorisdev 3 жыл бұрын
Always use #define, unless you need the constant to be a variable. He explained cases where a variable might be useful, so it's up to you whether you care about those cases or not
@kanony5188
@kanony5188 3 жыл бұрын
In primitive types like int there is not any difference (in most cases), but in types like struct -s you can't write with #define
@lycorisdev
@lycorisdev 3 жыл бұрын
@@kanony5188 I don't know what you mean by "-s" structs, but of course a macro can't replace a struct
@simonfarre4907
@simonfarre4907 3 жыл бұрын
If you are using C++ *never* use define as some of these comments say - use constexpr.
@revealingfacts4all
@revealingfacts4all 3 жыл бұрын
You always want to use a const over #define. In fact, you really should avoid # anything because it's not handeled by the compiler. Compiler will give you type checking that #define won't. This guy is wrong, it does not depend. Always prefer the compiler over the preprocessor. You can't debug code that is generated by preprocessor macros either. And this just scratches the surface. There are a host of other issues preprocessor macros introduce too he doesn't mention. I've been doing C/C++ for over 25 years mostly in embedded space and I hardly ever find a need to #define anything except for the #include guards.
@henrykkaufman1488
@henrykkaufman1488 2 жыл бұрын
You should use const for everything that you can rather than #define. #define is a preprocessor directive, it's much harder to debug since it's making preprocessor generate some code only later seen by compiler. In order for programmer to see what's actually generated by preprocessor, he has to read through intermediates. Also, preprocessor is very powerful and quite easy to use incorrectly (think C++ templates on steroids), so it should be used (it really should be used!) in a controlled way.
@1495978707
@1495978707 Жыл бұрын
But in embedded systems where memory and speed are precious, it can be worth the cost of harder debugging. Especially since the programs are often simpler in the first place
@totaljustice1582
@totaljustice1582 3 жыл бұрын
Personally I use an enum. If the number is bigger than an int, then I’ll use a define, or if I won’t the value to be managed by build script, then I’ll use a define there as well. I use const if the var is local and it’s just 1 var. but if I want a local const for left and right for example, then I’d use an enum
@nashaut7635
@nashaut7635 3 жыл бұрын
Yet another great video :thumbsup: ! At #6:02 you might mention this is actually because changing a "const" is undefined behaviour. That's the reason why different compilers give different results. We do not want UB lurking in our code ;-) .
@siddharthpandey3204
@siddharthpandey3204 3 жыл бұрын
Great content man, always waiting for your uploads!
@JacobSorber
@JacobSorber 3 жыл бұрын
Thanks. Glad you like them!
@tordjarv3802
@tordjarv3802 3 жыл бұрын
Using #define might be your only option if you might compile your code with older c compilers. In my research group we have "work horse" machines all running older versions of Debian with gcc 6.x and you can't use const variables in expressions that should be constant, but on my private computer I have gcc 11.1.0 which has no problem with that.
@ScioFantasia
@ScioFantasia 3 жыл бұрын
Thanks for posting. @5:40 Note that this will not even compile in C++; pointers to const types must be const type *.
@johnheaney3349
@johnheaney3349 2 жыл бұрын
Why no mention of enum? They are constants and essential for indexing because of the auto-increment feature. The other important distinction with #define is that preprocessor symbols can be used for conditional compiling. Can't do that with const variables or enums. #if NUM_ITEMS > 256 //General purpose code goes here. #else //Optimization for singe byte values. #endif #ifndef MAX_BUFFER_SIZE #define MAX_BUFFER_SIZE 1024 #endif On the other hand, macros are text substitution, so expressions are not necessarily optimized away and code size could change with optimization level. //Never even seen by the compiler unless it is used somewhere in code. Compiler sees expression on each usage. // Integer type depends on context. Never any storage allocation. Essentially, always a literal in code. #define MAX_BYTES (ITEM_SIZE * NUM_ITEMS) //Expression always resolved by compiler. Never an expression in code. Integer type dependent on compiler and sometimes the // size of the enum values. Essentially, a literal in code. enum {MAX_BYTES = ITEM_SIZE * NUM_ITEMS}; //Expression always resolved by compiler. Never an expression in code. Explicit type. Compiler may optimize away storage. const int MAX_BYTES = ITEM_SIZE * NUM_ITEMS;
@johnheaney3349
@johnheaney3349 2 жыл бұрын
I forgot to add that you can take the address of a const variable, which of course forces storage allocation, regardless of optimization. Also, you can use sizeof().
@ronensuperexplainer
@ronensuperexplainer 2 жыл бұрын
C++17 has "inline constexpr" variables that completely replace #define
@dkkogmaw1311
@dkkogmaw1311 5 ай бұрын
Inline isn’t needed compiler decides anyways if he gonna inline it or not
@ronensuperexplainer
@ronensuperexplainer 5 ай бұрын
@@dkkogmaw1311 You're talking about functions. inline constexpr variables are the drop-in replacement for constant #defines
@8292-d6n
@8292-d6n 3 жыл бұрын
Another somewhat similar topic: what is your opinion of void* ? I really like them because they allow me to hide details and also act as generic types when combined with memcpy()
@alexwang007
@alexwang007 3 жыл бұрын
Me as an engineer who can somewhat program: I came being forced to do java, and stayed willingly for C
@junbird
@junbird 3 жыл бұрын
I don't know about C++, but in C const is basically a misnomer. A const variable is still a variable and its value can change, meaning that there's no guarantee that its assigned value will stay constant. Some suggest thinking of a const variable as a read-only variable, rather than a constant attribute. Also, declaring an array with a const counts as declaring a VLA (Variable Length Array), an array which length is determined at runtime (since a const is initialized only at runtime), which use is highly discouraged. An alternative to #define is using an enum such as "enum { ARRAY_LENGTH = 240 }", which doesn't have the problems that using a const brings, while also being typed, (so it will throw much more readable errors if used improperly). Also, I might be saying something wrong here, but if you need to declare a constant which is the result of a computation, #define TWO 1 + 1 will just copy 1 + 1 wherever TWO is found during code preprocessing, so the program will have to resolve the expression each time it reaches a part of code which used the TWO symbol (even though I'd bet compiler optimizations would take care of that), while enum { TWO = 1 + 1 } would be processed just once at compile time (or so I think, please feel free to correct me).
@totaljustice1582
@totaljustice1582 3 жыл бұрын
You are correct about the 1+1. This is why you will see people then use (1+1) so that there’s no errors if the macro is used in more complicated equations, such as mult and div, the order may be important. But yeah the compiler would just make that value a 2 in that case. Also I completely agree about the enum as well! That’s the best way imo. One thing to note is that enums are ints, so they have the max size of an int. usually that’s okay, but if you have a very large number that’s bigger than an int, the compiler should(gcc will) warn about it, though maybe only will warn with -pedantic flag
@anindyamitra5091
@anindyamitra5091 3 жыл бұрын
I just love the intro! 🔥🔥🔥 Thank you for making the video, I was really curious about this thing.
@tomaszstanislawski457
@tomaszstanislawski457 2 жыл бұрын
There is an alternative way. Use untagged enumerations. `enum { ARRAY_LENGTH = 240 };`. This produces a true named integer constant that is properly scoped (not like a macro).
@samaellovecraft
@samaellovecraft 8 ай бұрын
Thanks for the knowledge!
@simonmultiverse6349
@simonmultiverse6349 3 жыл бұрын
When I was working for a large company many years ago, I was programming in an ancient language, one of the ancestors of C. This large company was using an emulator to run this software, since the computer which originally could run it did not exist any more. The result was that the new, bigger computer ran the code more slowly than the original. I needed (for artistic reasons) to have variables which could be true or false, so I could check certain conditions, to see if a certain engineering calculation had converged to an answer. I defined two constants: NOWAY = 0 ; FERSURE = 1 ; I called these the California Booleans. There were snippets of code such as IF RESIDUALMAXITER THEN CONVERGED=NOWAY
@JacobSorber
@JacobSorber 3 жыл бұрын
Nice. 😂😂
@misana77
@misana77 3 жыл бұрын
1. Throwing away const and changing the value is undefined behavior that is am important concept both in C and C++ and had to be mentioned 2. The code with discarding const is not going to be compiled in C++ 3. Please don't say that it is for C++. C++ has solved the problem with the constexpr keyword. Using macros for constants in C++ is a sign of not a C++ programmer or bad taught one
@StefanoTrevisani
@StefanoTrevisani 2 жыл бұрын
This is one of the most important reasons I switched to C++: constexpr! Sure... You can use enums and pretend it's the same thing, but in the moment you need something other than an int, it still won't do.
@1stblazer781
@1stblazer781 Жыл бұрын
Helpful !!!! . keep it up Soldier !
@TheFootballPlaya
@TheFootballPlaya 3 жыл бұрын
this is a great topic. one thing about c++ is it kind of forces the programmer to zoom into the actuality of how each statement will be processed at run-time versus compile-time. I like this content focused on macros, because it can be easily overlooked, and is pretty important
@IamusTheFox
@IamusTheFox 3 жыл бұрын
macros don't happen at compile time, they happen before compile time.
@TheFootballPlaya
@TheFootballPlaya 3 жыл бұрын
@@IamusTheFox is it because they are processed by preprocessor? or how are they handled? are they treated as textual?
@IamusTheFox
@IamusTheFox 3 жыл бұрын
@@TheFootballPlaya yes, you're right. It's textual, at preprocessor time. It's why #includes work without effecting anything. Because it happens first.
@TheFootballPlaya
@TheFootballPlaya 3 жыл бұрын
@@IamusTheFox ah i see. that's even more interesting now. thank you.
@IamusTheFox
@IamusTheFox 3 жыл бұрын
@@TheFootballPlaya It's sad that all steps of compilation aren't thought well. Really glad to have helped!
@benjaminshinar9509
@benjaminshinar9509 3 жыл бұрын
there's also an older video that you made about debugging symbols, so if you use a variable, you can see the value in the debugger without any special modifications, but if you #define it, then you need some flags turned on. i prefer const, unless it's something that's controlled at the build level, in which case, that's the devops problem.
@JacobSorber
@JacobSorber 3 жыл бұрын
Definitely. Thanks!
@warrenbuckley3267
@warrenbuckley3267 3 жыл бұрын
For the style of videos you make Jacob (short and to the point), a video on "struct hack" might be an idea.
@LogicEu
@LogicEu 3 жыл бұрын
Great stuff! I was just wondering about this matter. Thank you
@JacobSorber
@JacobSorber 3 жыл бұрын
You're welcome. Glad the timing was helpful.
@MCLooyverse
@MCLooyverse 3 жыл бұрын
One nice thing about macros is that you can have `#define CSI "\x1B["`, and then, somewhere in your code, have `CSI "J"`, which the preprocessor will turn in to `"\x1B[" "J"`, which is exactly the same as `"\x1B[J"`. I recently #defined a lot of ANSI escape code macros, so I am extra aware of this feature.
@FaranAiki
@FaranAiki 2 жыл бұрын
Yup, this feature is yummy.
@preetishamballi6988
@preetishamballi6988 3 жыл бұрын
Further reading: Effective C++ by Scott Meyers, chapter 1, item 2.
@MahfuzurRahman-xl9pj
@MahfuzurRahman-xl9pj 2 жыл бұрын
What is a translation unit? I am new to c programming and hear this term everywhere. Recently, I was studying about storage class (auto, register, static and extern) and came across this term. However, I am finding hard to understand the translation unit definition and the class specifiers.
@michaelkotthaus7120
@michaelkotthaus7120 3 жыл бұрын
Like "BigCheese" already pointed out below, the enum has a certain advantage. Particularly, this does not work on file scope: const int ARRAY_LENGTH = 24; int array[ARRAY_LENGTH]; But this: enum { ARRAY_LENGTH = 24 }; int array[ARRAY_LENGTH];
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
As to your "no go" example, just swap where you define things: int array[ 24 ]; // limit is 2 dozen... const int ARRAY_LENGTH = sizeof(arrary)/sizeof(array[0]); This is much more flexible when adding or removing items from an initialised array: int array[] = { 1, 2, 3, 4, 5, }; const int nElem= sizeof(array)/sizeof(array[0]); When you add 6, 7 and 8 to the array, the compiler will count the elements accurately.
@jsjiang6120
@jsjiang6120 2 жыл бұрын
const int will cause larger code size since it needs a space to store the variable, which is not required for #define.
@homelikebrick42
@homelikebrick42 3 жыл бұрын
I like to use a anonymous enum instead because it can be scoped
@plumaligera8885
@plumaligera8885 3 жыл бұрын
The video about function style macros sounds great!
@SimGunther
@SimGunther 3 жыл бұрын
I'd "define" numbers/straight up values, but use const for expressions that are evaluated at compile time.
@SystemSigma_
@SystemSigma_ Жыл бұрын
In complex embedded projects with many submodules using #define for just constants is a mess. Pollutes everything and you end up with modules coupled by #define everywhere. Const and enums are your friend.
@osbaldotheVtenman
@osbaldotheVtenman 3 жыл бұрын
Please, could you make #define style function macros. Great video. I always love how short and sweet yet immensely informative videos are
@richardistvanthier5620
@richardistvanthier5620 3 жыл бұрын
use constexpr?
@chennebicken372
@chennebicken372 2 жыл бұрын
What about constexpr in C++?
@harshavardhanbose
@harshavardhanbose 7 ай бұрын
I use templated static constexpr
@mfrdbigolin
@mfrdbigolin 3 жыл бұрын
Nowadays I use mostly const, except for a constant string that needs to be concatenated with another string at compile-time, e.g.: puts ("Current program version: " VERSION);
@simonfarre4907
@simonfarre4907 3 жыл бұрын
And if you use C++ you should definitely use constexpr instead
@agustinranieri
@agustinranieri 3 жыл бұрын
I have an idea for a video: ¿how could I implement Promises or even Observables in C? Trying to understand how they work in behind
@makermatrix9815
@makermatrix9815 3 жыл бұрын
Helpful. Is there a difference in terms of RAM usage? On something like an AVR where you've got 2500 bytes, saving every byte you can is sometimes critical.
@JacobSorber
@JacobSorber 3 жыл бұрын
There often is a difference, but that difference depends on what the compiler is doing and what optimizations are turned on. Often you're also trading data memory for code memory, which can sometimes be useful.
@wirosk2916
@wirosk2916 11 ай бұрын
How about: #define ARRAY_LENGTH int(240) As I understand it the compiler would/could be aware if the type. And it should be optimized away in the asm.
@maxaafbackname5562
@maxaafbackname5562 9 ай бұрын
The exact type of the literal 240 is already an int.
@lorensims4846
@lorensims4846 3 жыл бұрын
I was always uncomfortable with the simple text replacement aspect of a #DEFINE, but I'm concerned the const int will add unneeded overhead within the executable. I guess it's just a few bytes so what's the diff? Who knows how the compiler might actually be using the result of that #DEFINE? I'll just have to do some test compiles and speed tests to figure out what I think about this.
@xCwieCHRISx
@xCwieCHRISx 2 жыл бұрын
on normal computers this overhead of few bytes doesn't matter, but on micro controllers it can matter.
@dynpallomah
@dynpallomah 2 жыл бұрын
For me, when 1) a variable is global and is a guaranteed compile constant (numbers, bools that aren't assigned using variables etc), I'd use #define 2) a variable is in scope, 99% of times I'd use const. That doesn't mean you can't #define, it's just that you have to #undef it at the end of scope and that's not elegant and is error-prone. As you can imagine const is way more common in my code 3) I'm defining a function inside a function (tho it's not a good practise imo) and I'm not feeling to deal with lambdas and std::function I'd use #define to 'declare' the function (terrible practise but gets the job done) tl;dr use you fucking const, always
@KishoreG2396
@KishoreG2396 3 жыл бұрын
I use a constexpr in most cases
@fabiovictorino350
@fabiovictorino350 3 жыл бұрын
I'm enjoy this canal
@Christobanistan
@Christobanistan 3 жыл бұрын
Does literally every developer use VS Code now??
@IamusTheFox
@IamusTheFox 3 жыл бұрын
No. I use CLion, and a lot I know use Visual Studios proper.
@pumpkinhead002
@pumpkinhead002 3 жыл бұрын
vim is life
@kermitdafrog8
@kermitdafrog8 3 жыл бұрын
Where can we purchase a shirt like that?
@JacobSorber
@JacobSorber 3 жыл бұрын
jacob-sorbers-store.creator-spring.com/
@raghavsrivastava2910
@raghavsrivastava2910 3 жыл бұрын
Great Video.
@IamusTheFox
@IamusTheFox 3 жыл бұрын
If you care about performance why would you turn off an optimizer!? That makes no sense. Here's the problem with treating c++ and c as the same language. #define, esp function macros have their place in C, never in C++. define < const < constexpr < consteval. You can run can assign a value to a const with immediate invoking lambdas.
@sledgex9
@sledgex9 3 жыл бұрын
Try to declare it as "static const int" instead of a global. Or, if in C++, put it inside an anonymous namespace.
@justcurious1940
@justcurious1940 Жыл бұрын
Nice !!
@XenoTravis
@XenoTravis 3 жыл бұрын
'we don't really know' Until you look it up...
@KarabauPlay
@KarabauPlay Жыл бұрын
for simple stuff, dont use #define. such as replacing a text with a number. also, in c++ instead of const int, which is global. use static constexpr int instead in fact, this video is mostly C convention, C++ has its own conventions regarding this topic.
2 жыл бұрын
What I hate about #define is that you can run into a #define that calls another #define that calls another... and you end up not knowing what it's doing.
@andrewbrownbill
@andrewbrownbill 3 жыл бұрын
If it's c++, constexpr.
@milo20060
@milo20060 2 жыл бұрын
Also "mutable" will disable the const ^^
@WistrelChianti
@WistrelChianti 2 жыл бұрын
I feel I could upset people if I had a tshirt made with that pointer example on it...
@mes2009
@mes2009 8 ай бұрын
Const better then define for type variable The all same you can not change
@gundam7463
@gundam7463 3 жыл бұрын
#define #define
@AbinMathewAbraham
@AbinMathewAbraham 3 жыл бұрын
Gotcha professor.... You typed a.out... so you are not using make files always... Just kidding... Love your videos ❤️
@glee21012
@glee21012 3 жыл бұрын
#define has scope in a namespace
@tk36_real
@tk36_real 2 жыл бұрын
Maybe mention #undef
@marbens
@marbens 7 ай бұрын
It's #undef, not #undefine.
@tk36_real
@tk36_real 7 ай бұрын
@@marbens yeah, obviously. silly mistake, thanks!
@OlliS71
@OlliS71 2 ай бұрын
Never use macros for constants except in C.
@nazarottto
@nazarottto 3 жыл бұрын
Another alternative is enum.
@scottspitlerII
@scottspitlerII 3 жыл бұрын
Hey Jacob! What do you think of the arguments that C is an old language not suited for modern systems programming and the folks at rust are trying to replace it? I personally disagree and think something not as drastic as Rust would be a better alternative. Thoughts?
@rafaelcoelho5226
@rafaelcoelho5226 3 жыл бұрын
nice
@blank-vw2sb
@blank-vw2sb 3 жыл бұрын
Whoa. prof, I really want to get in contact with you. How about creating a discord server? You aren't responding to me in instagram, prof!
@JacobSorber
@JacobSorber 3 жыл бұрын
Sorry. Not super responsive on IG. I'll think about the server idea. The best way to get project help is through Patreon.
@blank-vw2sb
@blank-vw2sb 3 жыл бұрын
@@JacobSorber I'm not actually looking for project help. Just a computer geek to befriend. I know no body which loves programming as I do. But I can sense that geekiness in you, professor!
@blank-vw2sb
@blank-vw2sb 3 жыл бұрын
@@JacobSorber please don't be sorry. if you make a discord server. Please let me know through your videos. Maybe a shout out?
@JacobSorber
@JacobSorber 3 жыл бұрын
@@blank-vw2sb Will do. Thanks.
@blank-vw2sb
@blank-vw2sb 3 жыл бұрын
@@JacobSorber /* input [s] is supposed to be a output stream like cout. So pass one. */ std::ostream& reply(std::ostream& s){ return reply(s
@arson5304
@arson5304 3 жыл бұрын
CONSTEXPR
@yen7569
@yen7569 3 жыл бұрын
los mejores videos no estan en espanol
@pizzarda
@pizzarda 2 жыл бұрын
genio
@ladyViviaen
@ladyViviaen 3 жыл бұрын
octalthorpe define > hash define / pound define
@stefanalecu9532
@stefanalecu9532 4 ай бұрын
Use constexpr. :)
@pierreabbat6157
@pierreabbat6157 3 жыл бұрын
An important use of #define is in the config.h file. You put a line like "test_big_endian(BIGENDIAN)" in CMakeLists.txt and "#cmakedefine BIGENDIAN" in config.h.in; this results in BIGENDIAN being defined or not in config.h, which you include in any source code that has to behave differently depending on endianness. The software version can be set similarly.
@Voltra_
@Voltra_ 3 жыл бұрын
constexpr
@n-steam
@n-steam 3 жыл бұрын
# = Hash £ = Pound
@user-sl6gn1ss8p
@user-sl6gn1ss8p 3 жыл бұрын
"This is really s... something you shouldn't do"
@williamwillis5729
@williamwillis5729 2 жыл бұрын
It's 'hash define' not 'pound define'. A pound sign is: '£'.
@apocryphite_2
@apocryphite_2 Жыл бұрын
I feel like you make mistakes on purpose to make us (the inexperienced) feel better.
@JacobSorber
@JacobSorber Жыл бұрын
If only that were true. I create plenty of genuine mistakes. But, if it makes you feel better, then I'm glad they served a valuable purpose.
@markmanning2921
@markmanning2921 3 жыл бұрын
a typedef enum ftw
@BlastinRope
@BlastinRope 3 жыл бұрын
local_constants.h
How to Write Function-Like Preprocessor Macros (C example)
13:59
Jacob Sorber
Рет қаралды 44 М.
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
A const int is not a constant.
9:16
Jacob Sorber
Рет қаралды 69 М.
How to make memory read-only in your C programs.
12:57
Jacob Sorber
Рет қаралды 21 М.
When do I use a union in C or C++, instead of a struct?
11:18
Jacob Sorber
Рет қаралды 71 М.
Why Your Brain Sabotages Your Goals (and How to Fix It)
11:56
Productive Peter
Рет қаралды 34 М.
9 Benefits of Living Alone as an Older Man
14:56
Wealthy Health
Рет қаралды 27
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 22 М.
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН