From db82a53408b5c95ec565689d3a613279a3a0a8d7 Mon Sep 17 00:00:00 2001 From: Tzupy Date: Tue, 15 Jan 2019 23:03:52 +0200 Subject: [PATCH] Fixed a compilation error and some spelling mistakes (#30984) --- .../c/short-circuit-evaluation/index.md | 65 +++++++++---------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/guide/english/c/short-circuit-evaluation/index.md b/guide/english/c/short-circuit-evaluation/index.md index 1edc39c5c4..745fa5bb5e 100644 --- a/guide/english/c/short-circuit-evaluation/index.md +++ b/guide/english/c/short-circuit-evaluation/index.md @@ -1,15 +1,13 @@ --- title: Short-Circuit Evaluation --- + # Short-Circuit Evaluation +The Short-Circuit evaluation consists in checking or executing the second argument only if the first argument is not enough to determine the value of the expression. -The Short-Circuit evaluation consist in check or execute the second argument only if the first argument is not enough to determine the value of the expression. - -You can do a short-circuit evaluation with && and || operators. - - -## Example with && operator: +You can do a short-circuit evaluation with `&&` and `||` operators. +### Example with `&&` Operator ```c canOpenFile(filename) && openFile(filename); // If you can open the file then open it. ``` @@ -22,7 +20,7 @@ The example above is equivalent to: } ``` -## Example with || operator: +### Example with `||` Operator ```c isServerOn || startServer(); // If the server is not on then start it. @@ -35,7 +33,33 @@ The example above is equivalent to: } ``` - ## Example with nested if statements +### A Real-World Example with `||` Operator +```c +#include +#include + +char *getName(); + +int main(int argc, char *argv[]) { + // Get the first argument passed via terminal + char *name = argv[1]; + + // If the name is not passed via terminal, then print a message and then get the name + name || printf("Please give me your name:") && (name = getName()); + + printf("Hello %s\n", name); +} + +char *getName() { + // Allocate memory + char *name = (char*)malloc(30); + + scanf("%s", name); + return name; +} +``` + +### Example with Nested `if` Statements ```c int i, j; @@ -57,31 +81,6 @@ The example above is equivalent to: ``` Notice when `if ( i > 10 )` fails, the statement is false and the check `if ( j > 10 )` is never run. `if ( i > 10 && j > 10 )` behaves exactly the same way, because if `i > 10` is false then the entire statement is automatically false, and there is no need to run an additional check. - ## Keep it all together in real example - - ```c -#include -#include -char *getName(); - -int main(int argc, char *argv[]) { - // Get first argument passed via terminal - char *name = argv[1]; - - // If name is not passed via terminal then print message and then get the name - name || printf("Please give me your name:") && (name = getName()); - - printf("Hello %s\n", name); -} - -char *getName() { - // Allocate memory - char *name = malloc(30); - - scanf("%s", name); - return name; -} -```