mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 01:51:38 +00:00
fixed header, added switch statement
This commit is contained in:
parent
f2c41a8a99
commit
273c08d1fe
@ -1,10 +1,12 @@
|
|||||||
---
|
---
|
||||||
name: c
|
- name: c
|
||||||
category: language
|
- category: language
|
||||||
language: c
|
- language: c
|
||||||
filename: learnc.c
|
- filename: learnc.c
|
||||||
contributors:
|
- contributors:
|
||||||
- ["Adam Bard", "http://adambard.com/"]
|
- [Adam Bard](http://adambard.com/)
|
||||||
|
- [Árpád Goretity](http://twitter.com/H2CO3_iOS)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Ah, C. Still **the** language of modern high-performance computing.
|
Ah, C. Still **the** language of modern high-performance computing.
|
||||||
@ -152,7 +154,7 @@ int main() {
|
|||||||
|
|
||||||
// So string literals are strings enclosed within double quotes, but if we have characters
|
// So string literals are strings enclosed within double quotes, but if we have characters
|
||||||
// between single quotes, that's a character literal.
|
// between single quotes, that's a character literal.
|
||||||
// It's of type `int`, and *not* `char` (for hystorical reasons).
|
// It's of type `int`, and *not* `char` (for historical reasons).
|
||||||
int cha = 'a'; // fine
|
int cha = 'a'; // fine
|
||||||
char chb = 'a'; // fine too (implicit conversion from int to char - truncation)
|
char chb = 'a'; // fine too (implicit conversion from int to char - truncation)
|
||||||
|
|
||||||
@ -176,6 +178,7 @@ int main() {
|
|||||||
|
|
||||||
// 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.
|
||||||
|
// (Or _Bool or bool in C99.)
|
||||||
// 0 is false, anything else is true. (The comparison
|
// 0 is false, anything else is true. (The comparison
|
||||||
// operators always yield 0 or 1.)
|
// operators always yield 0 or 1.)
|
||||||
3 == 2; // => 0 (false)
|
3 == 2; // => 0 (false)
|
||||||
@ -185,6 +188,13 @@ int main() {
|
|||||||
2 <= 2; // => 1
|
2 <= 2; // => 1
|
||||||
2 >= 2; // => 1
|
2 >= 2; // => 1
|
||||||
|
|
||||||
|
// C is not Python - comparisons don't chain.
|
||||||
|
int a = 1;
|
||||||
|
// WRONG:
|
||||||
|
int between_0_and_2 = 0 < a < 2;
|
||||||
|
// Correct:
|
||||||
|
int between_0_and_2 = 0 < a && a < 2;
|
||||||
|
|
||||||
// Logic works on ints
|
// Logic works on ints
|
||||||
!3; // => 0 (Logical not)
|
!3; // => 0 (Logical not)
|
||||||
!0; // => 1
|
!0; // => 1
|
||||||
@ -201,6 +211,12 @@ int main() {
|
|||||||
0x01 << 1; // => 0x02 (bitwise left shift (by 1))
|
0x01 << 1; // => 0x02 (bitwise left shift (by 1))
|
||||||
0x02 >> 1; // => 0x01 (bitwise right shift (by 1))
|
0x02 >> 1; // => 0x01 (bitwise right shift (by 1))
|
||||||
|
|
||||||
|
// Be careful when shifting signed integers - the following are all undefined behavior:
|
||||||
|
// - shifting into the sign bit of a signed integer (int a = 1 << 32)
|
||||||
|
// - left-shifting a negative number (int a = -1 << 2)
|
||||||
|
// - shifting by an offset which is more than or equal to the width of the type of the LHS:
|
||||||
|
// int a = 1 << 32; // UB if int is 32 bits wide
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Control Structures
|
// Control Structures
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
@ -237,6 +253,22 @@ int main() {
|
|||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
// branching with multiple choices: switch()
|
||||||
|
switch (some_integral_expression) {
|
||||||
|
case 0: // labels need to be integral *constant* epxressions
|
||||||
|
do_stuff();
|
||||||
|
break; // if you don't break, control flow falls over labels - you usually don't want that.
|
||||||
|
case 1:
|
||||||
|
do_something_else();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// if `some_integral_expression` didn't match any of the labels
|
||||||
|
fputs("error!\n", stderr);
|
||||||
|
exit(-1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Typecasting
|
// Typecasting
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user