mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
main() -> main(void) & fix spacing in c.html.markdown
This commit is contained in:
parent
85d80b9e5d
commit
7cb94b3b85
@ -18,9 +18,9 @@ memory management and C will take you as far as you need to go.
|
|||||||
```c
|
```c
|
||||||
// Single-line comments start with // - only available in C99 and later.
|
// Single-line comments start with // - only available in C99 and later.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Multi-line comments look like this. They work in C89 as well.
|
Multi-line comments look like this. They work in C89 as well.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Multi-line comments don't nest /* Be careful */ // comment ends on this line...
|
Multi-line comments don't nest /* Be careful */ // comment ends on this line...
|
||||||
@ -55,7 +55,7 @@ int add_two_ints(int x1, int x2); // function prototype
|
|||||||
|
|
||||||
// Your program's entry point is a function called
|
// Your program's entry point is a function called
|
||||||
// main with an integer return type.
|
// main with an integer return type.
|
||||||
int main() {
|
int main(void) {
|
||||||
// print output using printf, for "print formatted"
|
// print output using printf, for "print formatted"
|
||||||
// %d is an integer, \n is a newline
|
// %d is an integer, \n is a newline
|
||||||
printf("%d\n", 0); // => Prints 0
|
printf("%d\n", 0); // => Prints 0
|
||||||
@ -157,12 +157,12 @@ int main() {
|
|||||||
int cha = 'a'; // fine
|
int cha = 'a'; // fine
|
||||||
char chb = 'a'; // fine too (implicit conversion from int to char)
|
char chb = 'a'; // fine too (implicit conversion from int to char)
|
||||||
|
|
||||||
//Multi-dimensional arrays:
|
// Multi-dimensional arrays:
|
||||||
int multi_array[2][5] = {
|
int multi_array[2][5] = {
|
||||||
{1, 2, 3, 4, 5},
|
{1, 2, 3, 4, 5},
|
||||||
{6, 7, 8, 9, 0}
|
{6, 7, 8, 9, 0}
|
||||||
};
|
};
|
||||||
//access elements:
|
// access elements:
|
||||||
int array_int = multi_array[0][2]; // => 3
|
int array_int = multi_array[0][2]; // => 3
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
@ -219,13 +219,13 @@ int main() {
|
|||||||
0 || 1; // => 1 (Logical or)
|
0 || 1; // => 1 (Logical or)
|
||||||
0 || 0; // => 0
|
0 || 0; // => 0
|
||||||
|
|
||||||
//Conditional expression ( ? : )
|
// Conditional expression ( ? : )
|
||||||
int e = 5;
|
int e = 5;
|
||||||
int f = 10;
|
int f = 10;
|
||||||
int z;
|
int z;
|
||||||
z = (e > f) ? e : f; // => 10 "if e > f return e, else return f."
|
z = (e > f) ? e : f; // => 10 "if e > f return e, else return f."
|
||||||
|
|
||||||
//Increment and decrement operators:
|
// Increment and decrement operators:
|
||||||
char *s = "iLoveC";
|
char *s = "iLoveC";
|
||||||
int j = 0;
|
int j = 0;
|
||||||
s[j++]; // => "i". Returns the j-th item of s THEN increments value of j.
|
s[j++]; // => "i". Returns the j-th item of s THEN increments value of j.
|
||||||
@ -371,7 +371,7 @@ int main() {
|
|||||||
x_array[xx] = 20 - xx;
|
x_array[xx] = 20 - xx;
|
||||||
} // Initialize x_array to 20, 19, 18,... 2, 1
|
} // Initialize x_array to 20, 19, 18,... 2, 1
|
||||||
|
|
||||||
// Declare a pointer of type int and initialize it to point to x_array
|
// Declare a pointer of type int and initialize it to point to x_array
|
||||||
int* x_ptr = x_array;
|
int* x_ptr = x_array;
|
||||||
// x_ptr now points to the first element in the array (the integer 20).
|
// x_ptr now points to the first element in the array (the integer 20).
|
||||||
// This works because arrays often decay into pointers to their first element.
|
// This works because arrays often decay into pointers to their first element.
|
||||||
@ -404,8 +404,8 @@ int main() {
|
|||||||
*(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)
|
||||||
|
|
||||||
// Dereferencing memory that you haven't allocated gives
|
// Dereferencing memory that you haven't allocated gives
|
||||||
// "unpredictable results" - the program is said to invoke "undefined behavior"
|
// "unpredictable results" - the program is said to invoke "undefined behavior"
|
||||||
printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash.
|
printf("%d\n", *(my_ptr + 21)); // => Prints who-knows-what? It may even crash.
|
||||||
|
|
||||||
// When you're done with a malloc'd block of memory, you need to free it,
|
// When you're done with a malloc'd block of memory, you need to free it,
|
||||||
@ -471,13 +471,13 @@ str_reverse(c);
|
|||||||
printf("%s\n", c); // => ".tset a si sihT"
|
printf("%s\n", c); // => ".tset a si sihT"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//if referring to external variables outside function, must use extern keyword.
|
// if referring to external variables outside function, must use extern keyword.
|
||||||
int i = 0;
|
int i = 0;
|
||||||
void testFunc() {
|
void testFunc() {
|
||||||
extern int i; //i here is now using external variable i
|
extern int i; //i here is now using external variable i
|
||||||
}
|
}
|
||||||
|
|
||||||
//make external variables private to source file with static:
|
// make external variables private to source file with static:
|
||||||
static int j = 0; //other files using testFunc2() cannot access variable j
|
static int j = 0; //other files using testFunc2() cannot access variable j
|
||||||
void testFunc2() {
|
void testFunc2() {
|
||||||
extern int j;
|
extern int j;
|
||||||
|
Loading…
Reference in New Issue
Block a user