mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
style changes, made many of the comments more precise, corrected incorrect facts about type sizes
This commit is contained in:
parent
86273dec2f
commit
aac6cb65c1
@ -38,36 +38,49 @@ printf("%d\n", 0); // => Prints 0
|
||||
// Types
|
||||
///////////////////////////////////////
|
||||
|
||||
// Variables must always be declared with a type.
|
||||
// You have to declare variables before using them. A variable declaration
|
||||
// requires you to specify its type; a variable's type determines its size
|
||||
// in bytes.
|
||||
|
||||
// 32-bit integer
|
||||
// ints are usually 4 bytes
|
||||
int x_int = 0;
|
||||
|
||||
// 16-bit integer
|
||||
// shorts are usually 2 bytes
|
||||
short x_short = 0;
|
||||
|
||||
// 8-bit integer, aka 1 byte
|
||||
// chars are guaranteed to be 1 byte
|
||||
char x_char = 0;
|
||||
char y_char = 'y'; // Char literals are quoted with ''
|
||||
|
||||
long x_long = 0; // Still 32 bytes for historical reasons
|
||||
long long x_long_long = 0; // Guaranteed to be at least 64 bytes
|
||||
// longs are often 4 to 8 bytes; long longs are guaranteed to be at least
|
||||
// 64 bits
|
||||
long x_long = 0;
|
||||
long long x_long_long = 0;
|
||||
|
||||
// 32-bit floating-point decimal
|
||||
// floats are usually 32-bit floating point numbers
|
||||
float x_float = 0.0;
|
||||
|
||||
// 64-bit floating-point decimal
|
||||
// doubles are usually 64-bit floating-point numbers
|
||||
double x_double = 0.0;
|
||||
|
||||
// Integer types may be unsigned
|
||||
// Integral types may be unsigned. This means they can't be negative, but
|
||||
// the maximum value of an unsigned variable is greater than the maximum
|
||||
// value of the same size.
|
||||
unsigned char ux_char;
|
||||
unsigned short ux_short;
|
||||
unsigned int ux_int;
|
||||
unsigned long long ux_long_long;
|
||||
|
||||
// Other than char, which is always 1 byte, these types vary in size depending
|
||||
// on your machine. sizeof(T) gives you the size of a variable with type T in
|
||||
// bytes so you can express the size of these types in a portable way.
|
||||
// For example,
|
||||
printf("%d\n", sizeof(int)); // => 4 (on machines with 4-byte words)
|
||||
|
||||
// Arrays must be initialized with a concrete size.
|
||||
char my_char_array[20]; // This array occupies 1 * 20 = 20 bytes
|
||||
int my_int_array[20]; // This array occupies 4 * 20 = 80 bytes
|
||||
// (assuming 4-byte words)
|
||||
|
||||
|
||||
// You can initialize an array to 0 thusly:
|
||||
@ -81,16 +94,20 @@ my_array[0]; // => 0
|
||||
my_array[1] = 2;
|
||||
printf("%d\n", my_array[1]); // => 2
|
||||
|
||||
// Strings are just lists of chars terminated by a null (0x00) byte.
|
||||
// Strings are just arrays of chars terminated by a NUL (0x00) byte,
|
||||
// represented in strings as the special character '\0'.
|
||||
// (We don't have to include the NUL byte in string literals; the compiler
|
||||
// inserts it at the end of the array for us.)
|
||||
char a_string[20] = "This is a string";
|
||||
printf("%s\n", a_string); // %s formats a string
|
||||
|
||||
/*
|
||||
You may have noticed that a_string is only 16 chars long.
|
||||
Char #17 is a null byte, 0x00 aka \0.
|
||||
Char #17 is the NUL byte.
|
||||
Chars #18, 19 and 20 have undefined values.
|
||||
*/
|
||||
|
||||
printf("%d\n", a_string[16]);
|
||||
printf("%d\n", a_string[16]); => 0
|
||||
|
||||
///////////////////////////////////////
|
||||
// Operators
|
||||
@ -112,7 +129,8 @@ f1 / f2; // => 0.5, plus or minus epsilon
|
||||
|
||||
// Comparison operators are probably familiar, but
|
||||
// there is no boolean type in c. We use ints instead.
|
||||
// 0 is false, anything else is true
|
||||
// 0 is false, anything else is true. (The comparison
|
||||
// operators always return 0 or 1.)
|
||||
3 == 2; // => 0 (false)
|
||||
3 != 2; // => 1 (true)
|
||||
3 > 2; // => 1
|
||||
@ -176,8 +194,8 @@ printf("\n");
|
||||
// Typecasting
|
||||
///////////////////////////////////////
|
||||
|
||||
// Everything in C is stored somewhere in memory. You can change
|
||||
// the type of a variable to choose how to read its data
|
||||
// Every value in C has a type, but you can cast one value into another type
|
||||
// if you want.
|
||||
|
||||
int x_hex = 0x01; // You can assign vars with hex literals
|
||||
|
||||
@ -188,21 +206,26 @@ printf("%d\n", (char) x_hex); // => Prints 1
|
||||
|
||||
// Types will overflow without warning
|
||||
printf("%d\n", (char) 257); // => 1 (Max char = 255)
|
||||
printf("%d\n", (short) 65537); // => 1 (Max short = 65535)
|
||||
|
||||
// Integral types can be cast to floating-point types, and vice-versa.
|
||||
printf("%f\n", (float)100); // %f formats a float
|
||||
printf("%lf\n", (double)100); // %lf formats a double
|
||||
printf("%d\n", (char)100.0);
|
||||
|
||||
///////////////////////////////////////
|
||||
// Pointers
|
||||
///////////////////////////////////////
|
||||
|
||||
// You can retrieve the memory address of your variables,
|
||||
// then mess with them.
|
||||
// You can retrieve the memory addresses of your variables and perform
|
||||
// operations on them.
|
||||
|
||||
int x = 0;
|
||||
printf("%p\n", &x); // Use & to retrieve the address of a variable
|
||||
// (%p formats a pointer)
|
||||
// => Prints some address in memory;
|
||||
|
||||
int x_array[20]; // Arrays are a good way to allocate a contiguous block of memory
|
||||
int x_array[20]; // Arrays are a good way to allocate a contiguous block
|
||||
// of memory
|
||||
int xx;
|
||||
for (xx=0; xx<20; xx++) {
|
||||
x_array[xx] = 20 - xx;
|
||||
@ -210,7 +233,8 @@ for(xx=0; xx<20; xx++){
|
||||
|
||||
// Pointer types end with *
|
||||
int* x_ptr = x_array;
|
||||
// This works because arrays are pointers to their first element.
|
||||
// x_ptr now points to the first element in the array (the integer 20).
|
||||
// This works because arrays are actually just pointers to their first element.
|
||||
|
||||
// Put a * in front to de-reference a pointer and retrieve the value,
|
||||
// of the same type as the pointer, that the pointer is pointing at.
|
||||
@ -221,33 +245,27 @@ printf("%d\n", x_array[0]); // => Prints 20
|
||||
printf("%d\n", *(x_ptr + 1)); // => Prints 19
|
||||
printf("%d\n", x_array[1]); // => Prints 19
|
||||
|
||||
// Array indexes are such a thin wrapper around pointer
|
||||
// arithmetic that the following works:
|
||||
printf("%d\n", 0[x_array]); // => Prints 20;
|
||||
printf("%d\n", 2[x_array]); // => Prints 18;
|
||||
|
||||
// The above is equivalent to:
|
||||
printf("%d\n", *(0 + x_ptr));
|
||||
printf("%d\n", *(2 + x_ptr));
|
||||
|
||||
// You can give a pointer a block of memory to use with malloc
|
||||
// You can also dynamically allocate contiguous blocks of memory with the
|
||||
// standard library function malloc, which takes one integer argument
|
||||
// representing the number of bytes to allocate from the heap.
|
||||
int* my_ptr = (int*) malloc(sizeof(int) * 20);
|
||||
for (xx=0; xx<20; xx++) {
|
||||
*(my_ptr + xx) = 20 - xx;
|
||||
*(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx would also work here
|
||||
} // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints)
|
||||
|
||||
// Dereferencing memory that you haven't allocated gives
|
||||
// unpredictable results
|
||||
printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what?
|
||||
|
||||
// When you're done with a malloc'd block, you need to free it
|
||||
// When you're done with a malloc'd block of memory, you need to free it,
|
||||
// or else no one else can use it until your program terminates
|
||||
free(my_ptr);
|
||||
|
||||
// Strings can be char arrays, but are usually represented as char
|
||||
// pointers:
|
||||
char* my_str = "This is my very own string";
|
||||
|
||||
printf("%d\n", *my_str); // 84 (The ascii value of 'T')
|
||||
printf("%c\n", *my_str); // => 'T'
|
||||
|
||||
function_1();
|
||||
} // end main function
|
||||
|
Loading…
Reference in New Issue
Block a user