mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-25 20:14:30 +03:00
Merge pull request #4097 from Naereen/patch-7
[c/en] Just a few explanations, a couple of tiny changes
This commit is contained in:
commit
cb434152b2
@ -224,10 +224,18 @@ int main (int argc, char** argv)
|
|||||||
(float)i1 / i2; // => 0.5f
|
(float)i1 / i2; // => 0.5f
|
||||||
i1 / (double)i2; // => 0.5 // Same with double
|
i1 / (double)i2; // => 0.5 // Same with double
|
||||||
f1 / f2; // => 0.5, plus or minus epsilon
|
f1 / f2; // => 0.5, plus or minus epsilon
|
||||||
// Floating-point numbers and calculations are not exact
|
|
||||||
|
|
||||||
// Modulo is there as well
|
// Floating-point numbers and calculations are not exact
|
||||||
11 % 3; // => 2
|
// for instance it is not giving mathematically correct results
|
||||||
|
(0.1 + 0.1 + 0.1) != 0.3; // => 1 (true)
|
||||||
|
// and it is NOT associative
|
||||||
|
1 + (1e123 - 1e123) != (1 + 1e123) - 1e123; // => 1 (true)
|
||||||
|
// this notation is scientific notations for numbers: 1e123 = 1*10^123
|
||||||
|
|
||||||
|
// Modulo is there as well, but be careful if arguments are negative
|
||||||
|
11 % 3; // => 2 as 11 = 2 + 3*x (x=3)
|
||||||
|
(-11) % 3; // => -2, as one would expect
|
||||||
|
11 % (-3); // => 2 and not -2, and it's quite counter intuitive
|
||||||
|
|
||||||
// Comparison operators are probably familiar, but
|
// Comparison operators are probably familiar, but
|
||||||
// there is no Boolean type in C. We use ints instead.
|
// there is no Boolean type in C. We use ints instead.
|
||||||
@ -236,12 +244,12 @@ int main (int argc, char** argv)
|
|||||||
// operators always yield 0 or 1.)
|
// operators always yield 0 or 1.)
|
||||||
3 == 2; // => 0 (false)
|
3 == 2; // => 0 (false)
|
||||||
3 != 2; // => 1 (true)
|
3 != 2; // => 1 (true)
|
||||||
3 > 2; // => 1
|
3 > 2; // => 1
|
||||||
3 < 2; // => 0
|
3 < 2; // => 0
|
||||||
2 <= 2; // => 1
|
2 <= 2; // => 1
|
||||||
2 >= 2; // => 1
|
2 >= 2; // => 1
|
||||||
|
|
||||||
// C is not Python - comparisons don't chain.
|
// C is not Python - comparisons do NOT chain.
|
||||||
// Warning: The line below will compile, but it means `(0 < a) < 2`.
|
// Warning: The line below will compile, but it means `(0 < a) < 2`.
|
||||||
// This expression is always true, because (0 < a) could be either 1 or 0.
|
// This expression is always true, because (0 < a) could be either 1 or 0.
|
||||||
// In this case it's 1, because (0 < 1).
|
// In this case it's 1, because (0 < 1).
|
||||||
@ -349,25 +357,30 @@ int main (int argc, char** argv)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
using "goto" in C
|
Using "goto" in C
|
||||||
*/
|
*/
|
||||||
typedef enum { false, true } bool;
|
typedef enum { false, true } bool;
|
||||||
// for C don't have bool as data type before C99 :(
|
// for C don't have bool as data type before C99 :(
|
||||||
bool disaster = false;
|
bool disaster = false;
|
||||||
int i, j;
|
int i, j;
|
||||||
for(i=0;i<100;++i)
|
for(i=0; i<100; ++i)
|
||||||
for(j=0;j<100;++j)
|
for(j=0; j<100; ++j)
|
||||||
{
|
{
|
||||||
if((i + j) >= 150)
|
if((i + j) >= 150)
|
||||||
disaster = true;
|
disaster = true;
|
||||||
if(disaster)
|
if(disaster)
|
||||||
goto error;
|
goto error; // exit both for loops
|
||||||
}
|
}
|
||||||
error :
|
error: // this is a label that you can "jump" to with "goto error;"
|
||||||
printf("Error occurred at i = %d & j = %d.\n", i, j);
|
printf("Error occurred at i = %d & j = %d.\n", i, j);
|
||||||
/*
|
/*
|
||||||
https://ideone.com/GuPhd6
|
https://ideone.com/GuPhd6
|
||||||
this will print out "Error occurred at i = 51 & j = 99."
|
this will print out "Error occurred at i = 51 & j = 99."
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
it is generally considered bad practice to do so, except if
|
||||||
|
you really know what you are doing. See
|
||||||
|
https://en.wikipedia.org/wiki/Spaghetti_code#Meaning
|
||||||
*/
|
*/
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
@ -741,11 +754,12 @@ typedef void (*my_fnp_type)(char *);
|
|||||||
// Order of Evaluation
|
// Order of Evaluation
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
|
||||||
|
// From top to bottom, top has higher precedence
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
// Operators | Associativity //
|
// Operators | Associativity //
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
// () [] -> . | left to right //
|
// () [] -> . | left to right //
|
||||||
// ! ~ ++ -- + = *(type)sizeof | right to left //
|
// ! ~ ++ -- + = *(type) sizeof | right to left //
|
||||||
// * / % | left to right //
|
// * / % | left to right //
|
||||||
// + - | left to right //
|
// + - | left to right //
|
||||||
// << >> | left to right //
|
// << >> | left to right //
|
||||||
@ -783,8 +797,8 @@ as the C file.
|
|||||||
/* included into files that include this header. */
|
/* included into files that include this header. */
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/* Like c source files macros can be defined in headers and used in files */
|
/* Like for c source files, macros can be defined in headers */
|
||||||
/* that include this header file. */
|
/* and used in files that include this header file. */
|
||||||
#define EXAMPLE_NAME "Dennis Ritchie"
|
#define EXAMPLE_NAME "Dennis Ritchie"
|
||||||
|
|
||||||
/* Function macros can also be defined. */
|
/* Function macros can also be defined. */
|
||||||
@ -823,7 +837,7 @@ Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://
|
|||||||
It is *the* book about C, written by Dennis Ritchie, the creator of C, and Brian Kernighan. Be careful, though - it's ancient and it contains some
|
It is *the* book about C, written by Dennis Ritchie, the creator of C, and Brian Kernighan. Be careful, though - it's ancient and it contains some
|
||||||
inaccuracies (well, ideas that are not considered good anymore) or now-changed practices.
|
inaccuracies (well, ideas that are not considered good anymore) or now-changed practices.
|
||||||
|
|
||||||
Another good resource is [Learn C The Hard Way](http://learncodethehardway.org/c/).
|
Another good resource is [Learn C The Hard Way](http://learncodethehardway.org/c/) (not free).
|
||||||
|
|
||||||
If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com).
|
If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com).
|
||||||
|
|
||||||
@ -833,4 +847,4 @@ Readable code is better than clever code and fast code. For a good, sane coding
|
|||||||
|
|
||||||
Other than that, Google is your friend.
|
Other than that, Google is your friend.
|
||||||
|
|
||||||
[1] [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member)
|
[1] [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member)
|
||||||
|
Loading…
Reference in New Issue
Block a user