[C/en] Added tidbit about fall-though in switch statements.

Another pitfall, as not all languages have fall-through in switches.
This commit is contained in:
Alex Luehm 2015-10-28 17:45:31 -05:00
parent 927ac9c3e8
commit 3b1940b9cc

View File

@ -76,7 +76,7 @@ int main (int argc, char** argv)
/////////////////////////////////////// ///////////////////////////////////////
// Types // Types
/////////////////////////////////////// ///////////////////////////////////////
// All variables MUST be declared at the top of the current block scope // All variables MUST be declared at the top of the current block scope
// we declare them dynamically along the code for the sake of the tutorial // we declare them dynamically along the code for the sake of the tutorial
@ -318,6 +318,12 @@ int main (int argc, char** argv)
case 1: case 1:
printf("Huh, 'a' equals 1!\n"); printf("Huh, 'a' equals 1!\n");
break; break;
// Be careful - without a "break", execution continues until the
// next "break" is reached.
case 3:
case 4:
printf("Look at that.. 'a' is either 3, or 4\n");
break;
default: default:
// if `some_integral_expression` didn't match any of the labels // if `some_integral_expression` didn't match any of the labels
fputs("error!\n", stderr); fputs("error!\n", stderr);
@ -345,8 +351,8 @@ int main (int argc, char** argv)
https://ideone.com/GuPhd6 https://ideone.com/GuPhd6
this will print out "Error occured at i = 52 & j = 99." this will print out "Error occured at i = 52 & j = 99."
*/ */
/////////////////////////////////////// ///////////////////////////////////////
// Typecasting // Typecasting
/////////////////////////////////////// ///////////////////////////////////////
@ -445,7 +451,7 @@ int main (int argc, char** argv)
for (xx = 0; xx < 20; xx++) { for (xx = 0; xx < 20; xx++) {
*(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx
} // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints)
// Note that there is no standard way to get the length of a // Note that there is no standard way to get the length of a
// dynamically allocated array in C. Because of this, if your arrays are // dynamically allocated array in C. Because of this, if your arrays are
// going to be passed around your program a lot, you need another variable // going to be passed around your program a lot, you need another variable
@ -721,13 +727,13 @@ typedef void (*my_fnp_type)(char *);
/******************************* Header Files ********************************** /******************************* Header Files **********************************
Header files are an important part of c as they allow for the connection of c Header files are an important part of c as they allow for the connection of c
source files and can simplify code and definitions by seperating them into source files and can simplify code and definitions by seperating them into
seperate files. seperate files.
Header files are syntaxtically similar to c source files but reside in ".h" Header files are syntaxtically similar to c source files but reside in ".h"
files. They can be included in your c source file by using the precompiler files. They can be included in your c source file by using the precompiler
command #include "example.h", given that example.h exists in the same directory command #include "example.h", given that example.h exists in the same directory
as the c file. as the c file.
*/ */