Issues with the pre- and postincrement operator in C, C++, etc.

preview_player
Показать описание
---

Issues with the pre- and postincrement operator in C, C++, etc. // The pre- and post-increment operators cause a lot of new programmers some headaches, because they forget that they are both a statement and an expression. This video helps break things down.

Related Videos:

***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Рекомендации по теме
Комментарии
Автор

This is fine for situations where you're dealing with simple types, and in C. But in C++ with iterator objects (or other instances of other classes which overload operator++()) the post increment/decrement operators might cause a copy to be constructed, which could be costly.

stephenaustin
Автор

Lovely video. Perhaps you could cover order of evaluation. For example, var[i] = var[++i] is not the same as var[i] = var[i+1]

RAINE____
Автор

Wish someone taught me this in my early days in university... I never understood this during any of my intro coding classes. Great explanation. Will definitely refer people who I see with this problem to this video.

cameronball
Автор

definitely one of the problems when you start with pointer artimetics is this pre and post increment of values, which can get quite confusing. Excellent topic!

fuemedicalcenter
Автор

Some truly beautiful and simple examples of programs using this to the max in K&R

JustinCromer
Автор

Hello Jacob. You could cover order of function's arguments evaluation. This is improved with C++17 standard.

rafalmichalski
Автор

What this video does NOT mention is incrementing iterators. Using integers, the "standard" for loop is for (i = 0; i < n; i++) but I've read where iterators should use the preincrement, for (i = 0; i < n; ++i) but I forget exactly why. I vaguely recall it has something to do with it being more efficient or some such, BUT how could that be? The effect is the same - the compiler should recognize that the thing is incremented by one, and the value is not used in any immediate expression, so preincrement vs. postincrement doesn't matter.

This is just one of many areas where C++ is MUCH more complicated than C.

TranscendentBen
Автор

Indeed, I even made a post regarding this. Dori did something like "return n++;" expecting that a returned value will be n+1. No, the returned value will be still n. She should do "return ++n;" as you explained. Or just return n+1 in case of a hesitation.

kamertonaudiophileplayer
Автор

Good video for beginners, but it is not just about how people like to write it, it is much deeper than this, the increment operator is actually a CPU inc instruction for both x86 and x64 and it could be atomic operation when using atomic increment/decrement, X++ is not the same as X = X + 1 when talking about atomic operations, X = X + 1 contains 3 different operations (read-modify-write) that could be intercepted by some other thread in the program where atomic increment/decrement can not, X++ is generally faster than X = X + 1 because of the way inc instruction works, CPU optimizes this commonly used instruction to be faster than read from memory and add 1, like below for x64:
Increment proc
mov rax, dword ptr[rsp]
add rax, 1
ret
Increment endp

; With faster inc instruction:

IncrementWithInc proc
inc rax

ret
IncrementWithInc endp

Another example where ++X is faster than X++ is the loops, take this example
for (int x = 0; x < 5; x++) {} // Slower
for (int x = 0; x < 5; ++x) {} // Faster
The first one will increment the value of X save it in temporary variable read X, while the second one says increment the value of X and directly move on without any intermediate stages, today compilers are smart enough to optimize the slower version and replace it with the faster version.
Keep up the good work

ahmadalastal
Автор

I never knew the ++ had a name. I always called it the ++ operator.
I tend to use ++i, because my programming teacher said in the far back it used to be slightly faster then i++. I doubt that there is any performance difference nowadays.
I try to switch to i+=1 like it is in python and swift just to make ne not remember two different ways.

IamTheHolypumpkin
Автор

I guess it's supposed to be "the output is the same but it is still a 1 up on c" since it adds in higher level features like classes while also promising low level control like c does.

leonm
Автор

I really enjoy your videos. Even as an advanced programmer I stil learn new interesting facts through your videos. Keep going

patrykmielczarek
Автор

A horrible way to ship even or odd numbers is to use ++x++ :P

mrcrackerist
Автор

Happy new year sir and please continue C server series🙏

saeedmahmoodi
Автор

Int X=1, y=2, c;
C=x+++y;
What will be the value of c and x when they are printed?? And why?

cezzar
Автор

[Post/pre]increments are nice to write more cleaner code, like for example
instead of

if ((x = x+1) > LENGHT(array)) break;

i can type only this

if (++x > LENGHT(array)) break;

There is a lot of use for this syntax sugar. I also like `?:` conditional operator because i can just place what could be a several lines of `if-else` statement to one line, if it short enough and i still wanna do it in one line of course :)

rogo
Автор

And back in the day before great optimizations on compilers ++i was faster than i++ if you just used it for incrementing a counter. Beacause of that i still use post increment when using something just for counters, even in other languages :D

airjuri
Автор

Honestly, I think super small details like this are more trouble than they're worth.
I've read about the pre/post-fix thing 3 or 4 times now. And every time I'm just like "...but why though??"

Stomachbuzz
Автор

Hey, uhh I didn’t go to college so I don’t have the full-spectrum of CS. I’ve been programming for about 5 years for client-side and building micro services/server programs. Do you have any book recommendations for what you are teaching? The lowest level I’ve worked on was Java for android devices/servers.

basicduck
Автор

Thank you so much for this video! You rock!

RileyCourtier