Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Created February 25, 2015 22:49
Show Gist options
  • Save odeke-em/a756f57d6b92c7df3d0d to your computer and use it in GitHub Desktop.
Save odeke-em/a756f57d6b92c7df3d0d to your computer and use it in GitHub Desktop.
const+spillover: A trivial reproduction of what tripped me out with using const within a for loop.
#include <stdio.h>
#include <string.h>
int main() {
char *lang = "c_language";
size_t len = strlen(lang);
size_t i;
for (i = 0; i < len; ++i) {
const char c = lang[i];
printf("%c", c);
}
return 0;
}
var lang = 'javascript';
for (var i = 0, len = lang.length; i < len; ++i) {
var c = lang[i];
console.log(c); // Should iterate through all the letters
}
// Spill over expected
console.log(c); // Expecting lang[i]
for (var i = 0, len = lang.length; i < len; ++i) {
const c = lang[i];
console.log(c); // Should always give 'j'
}
// Spill over still expected
console.log(c); // Should give 'j'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment