From 3d0687027a450d1778e6f7336f96962fa385ec53 Mon Sep 17 00:00:00 2001 From: Joe Savage Date: Sat, 29 Aug 2015 11:44:41 +0100 Subject: add missing semicolons in c.html.markdown --- c.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index d3f20eda..e2e15620 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -183,8 +183,8 @@ int main() { i1 / i2; // => 0 (0.5, but truncated towards 0) // You need to cast at least one integer to float to get a floating-point result - (float)i1 / i2 // => 0.5f - i1 / (double)i2 // => 0.5 // Same with double + (float)i1 / i2; // => 0.5f + i1 / (double)i2; // => 0.5 // Same with double f1 / f2; // => 0.5, plus or minus epsilon // Floating-point numbers and calculations are not exact -- cgit v1.2.3 From 85d80b9e5d8a124d00322f5228e5be64cd97c8ea Mon Sep 17 00:00:00 2001 From: Joe Savage Date: Sat, 29 Aug 2015 11:47:09 +0100 Subject: fix resource capitalisation and add resource to c.html.markdown --- c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index e2e15620..09806d93 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -634,7 +634,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 inaccuracies (well, ideas that are not considered good anymore) or now-changed practices. -Another good resource is [Learn C the hard way](http://c.learncodethehardway.org/book/). +Other good resources include [Learn C The Hard Way](http://c.learncodethehardway.org/book/) and [SourceCrunch](https://www.sourcecrunch.com/courses/foundations-of-c). If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com). -- cgit v1.2.3 From 7cb94b3b85608a73a0200469a8fa897e68f7c991 Mon Sep 17 00:00:00 2001 From: Joe Savage Date: Sat, 29 Aug 2015 11:58:03 +0100 Subject: main() -> main(void) & fix spacing in c.html.markdown --- c.html.markdown | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 09806d93..2b087688 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -18,9 +18,9 @@ memory management and C will take you as far as you need to go. ```c // 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 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 // main with an integer return type. -int main() { +int main(void) { // print output using printf, for "print formatted" // %d is an integer, \n is a newline printf("%d\n", 0); // => Prints 0 @@ -157,12 +157,12 @@ int main() { int cha = 'a'; // fine char chb = 'a'; // fine too (implicit conversion from int to char) - //Multi-dimensional arrays: + // Multi-dimensional arrays: int multi_array[2][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 0} }; - //access elements: + // access elements: int array_int = multi_array[0][2]; // => 3 /////////////////////////////////////// @@ -219,13 +219,13 @@ int main() { 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 - //Conditional expression ( ? : ) + // Conditional expression ( ? : ) int e = 5; int f = 10; int z; 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"; int j = 0; 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; } // 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; // 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. @@ -404,8 +404,8 @@ int main() { *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) - // Dereferencing memory that you haven't allocated gives - // "unpredictable results" - the program is said to invoke "undefined behavior" + // Dereferencing memory that you haven't allocated gives + // "unpredictable results" - the program is said to invoke "undefined behavior" 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, @@ -471,13 +471,13 @@ str_reverse(c); 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; void testFunc() { 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 void testFunc2() { extern int j; -- cgit v1.2.3 From e0d0e9189bb6e0af14a21e0c315922bc16fc2e14 Mon Sep 17 00:00:00 2001 From: Joe Savage Date: Sun, 30 Aug 2015 20:51:23 +0100 Subject: revert additional resource suggestion in c.html.markdown --- c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 2b087688..8e631de4 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -634,7 +634,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 inaccuracies (well, ideas that are not considered good anymore) or now-changed practices. -Other good resources include [Learn C The Hard Way](http://c.learncodethehardway.org/book/) and [SourceCrunch](https://www.sourcecrunch.com/courses/foundations-of-c). +Another good resource is [Learn C The Hard Way](http://c.learncodethehardway.org/book/). If you have a question, read the [compl.lang.c Frequently Asked Questions](http://c-faq.com). -- cgit v1.2.3 From c7e552c448c5a562a3ff5f442a7ed834aec04d9e Mon Sep 17 00:00:00 2001 From: Pushkar Sharma Date: Sun, 4 Oct 2015 10:34:31 +0530 Subject: Variable size array, user size input added. #1170 Fixed Issue #1170 Variable size array, user size input added. --- c.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 8e631de4..36621a9e 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -130,7 +130,9 @@ int main(void) { // can be declared as well. The size of such an array need not be a compile // time constant: printf("Enter the array size: "); // ask the user for an array size - char buf[0x100]; + int size; + scanf("%d", &size); + char buf[size]; fgets(buf, sizeof buf, stdin); // strtoul parses a string to an unsigned integer -- cgit v1.2.3 From 2e987df42225e6bdf824584058467aaffc73fb49 Mon Sep 17 00:00:00 2001 From: Pushkar Sharma Date: Mon, 5 Oct 2015 00:52:47 +0530 Subject: replaced scanf with fscanf. --- c.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 36621a9e..db2ac930 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -131,7 +131,7 @@ int main(void) { // time constant: printf("Enter the array size: "); // ask the user for an array size int size; - scanf("%d", &size); + fscanf(stdin, "%d", &size); char buf[size]; fgets(buf, sizeof buf, stdin); -- cgit v1.2.3 From e1ac6209a8d3f43e7a018d79454fb1095b3314c0 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Wed, 7 Oct 2015 23:45:01 -0400 Subject: [c/en] Added a section for header files. Added a section for header files. Included a discussion of what belongs in a header file and what does not. --- c.html.markdown | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index db2ac930..f1201eac 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,7 +6,7 @@ contributors: - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] - ["Marco Scannadinari", "https://marcoms.github.io"] - + - ["Zachary Ferguson", "https://github.io/zfergus2"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -630,6 +630,54 @@ typedef void (*my_fnp_type)(char *); ``` +Header files are an important part of c as they allow for the connection of c +source files and can simplify code and definitions by seperating them into +seperate files. + +Header files are syntaxtically similar to c source files but reside in ".h" +files. They can be included in your c source file by using the precompiler +command #include "example.h", given that example.h exists in the same directory +as the c file. + +```c +/* A safe guard to prevent the header from being defined too many times. This */ +/* happens in the case of circle dependency, the contents of the header is */ +/* already defined. */ +#ifndef EXAMPLE_H /* if EXAMPLE_H is not yet defined. */ +#define EXAMPLE_H /* Define the macro EXAMPLE_H. */ + +/* Other headers can be included in headers and therefore transitively */ +/* included into files that include this header. */ +#include + +/* Like c source files macros can be defined in headers and used in files */ +/* that include this header file. */ +#define EXAMPLE_NAME "Dennis Ritchie" +/* Function macros can also be defined. */ +#define ADD(a, b) (a + b) + +/* Structs and typedefs can be used for consistency between files. */ +typedef struct node +{ + int val; + struct node *next; +} Node; + +/* So can enumerations. */ +enum traffic_light_state {GREEN, YELLOW, RED}; + +/* Function prototypes can also be defined here for use in multiple files, */ +/* but it is bad practice to define the function in the header. Definitions */ +/* should instead be put in a c file. */ +Node createLinkedList(int *vals, int len); + +/* Beyond the above elements, other definitions should be left to a c source */ +/* file. Excessive includeds or definitions should, also not be contained in */ +/* a header file but instead put into separate headers or a c file. */ + +#endif /* End of the if precompiler directive. */ + +``` ## Further Reading Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language) -- cgit v1.2.3 From 3c02fdb8e496816b0fd615e029fad4a8ed9f4585 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Wed, 7 Oct 2015 23:49:46 -0400 Subject: Revert "[c/en] Added a section for header files." This reverts commit e1ac6209a8d3f43e7a018d79454fb1095b3314c0. --- c.html.markdown | 50 +------------------------------------------------- 1 file changed, 1 insertion(+), 49 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index f1201eac..db2ac930 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,7 +6,7 @@ contributors: - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] - ["Marco Scannadinari", "https://marcoms.github.io"] - - ["Zachary Ferguson", "https://github.io/zfergus2"] + --- Ah, C. Still **the** language of modern high-performance computing. @@ -630,54 +630,6 @@ typedef void (*my_fnp_type)(char *); ``` -Header files are an important part of c as they allow for the connection of c -source files and can simplify code and definitions by seperating them into -seperate files. - -Header files are syntaxtically similar to c source files but reside in ".h" -files. They can be included in your c source file by using the precompiler -command #include "example.h", given that example.h exists in the same directory -as the c file. - -```c -/* A safe guard to prevent the header from being defined too many times. This */ -/* happens in the case of circle dependency, the contents of the header is */ -/* already defined. */ -#ifndef EXAMPLE_H /* if EXAMPLE_H is not yet defined. */ -#define EXAMPLE_H /* Define the macro EXAMPLE_H. */ - -/* Other headers can be included in headers and therefore transitively */ -/* included into files that include this header. */ -#include - -/* Like c source files macros can be defined in headers and used in files */ -/* that include this header file. */ -#define EXAMPLE_NAME "Dennis Ritchie" -/* Function macros can also be defined. */ -#define ADD(a, b) (a + b) - -/* Structs and typedefs can be used for consistency between files. */ -typedef struct node -{ - int val; - struct node *next; -} Node; - -/* So can enumerations. */ -enum traffic_light_state {GREEN, YELLOW, RED}; - -/* Function prototypes can also be defined here for use in multiple files, */ -/* but it is bad practice to define the function in the header. Definitions */ -/* should instead be put in a c file. */ -Node createLinkedList(int *vals, int len); - -/* Beyond the above elements, other definitions should be left to a c source */ -/* file. Excessive includeds or definitions should, also not be contained in */ -/* a header file but instead put into separate headers or a c file. */ - -#endif /* End of the if precompiler directive. */ - -``` ## Further Reading Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language) -- cgit v1.2.3 From 9796759379d77a848ef84f8c1019672b87b90822 Mon Sep 17 00:00:00 2001 From: himanshu81494 Date: Thu, 8 Oct 2015 14:44:10 +0530 Subject: Update c.html.markdown --- c.html.markdown | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index db2ac930..b99cfe84 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -472,7 +472,22 @@ char c[] = "This is a test."; str_reverse(c); printf("%s\n", c); // => ".tset a si sihT" */ - +//as we can return return only one variable +//to change values of more than one variables we use call by reference +void swapTwoNumbers(int *a, int *b) +{ +int temp = *a; +*a = *b; +*b = temp; +} +/* +int first = 10; +int second = 20; +printf("first: %d\nsecond: %d\n", first, second); +swapTwoNumbers(&first, &second); +printf("first: %d\nsecond: %d\n", first, second); +// values will be swapped +*/ // if referring to external variables outside function, must use extern keyword. int i = 0; void testFunc() { -- cgit v1.2.3 From 626ee03fc3ca27698044db118bd53f563d22ccd0 Mon Sep 17 00:00:00 2001 From: himanshu81494 Date: Thu, 8 Oct 2015 14:45:16 +0530 Subject: Update c.html.markdown --- c.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index b99cfe84..8e1675bb 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] - ["Marco Scannadinari", "https://marcoms.github.io"] + - ["himanshu", "https://github.com/himanshu81494"] --- -- cgit v1.2.3 From e8248af13431ca87786fff17a605189c69aacf15 Mon Sep 17 00:00:00 2001 From: himanshu81494 Date: Thu, 8 Oct 2015 15:31:40 +0530 Subject: Update c.html.markdown --- c.html.markdown | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 8e1675bb..29bc5a5b 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -307,7 +307,25 @@ int main(void) { exit(-1); break; } - + + // using "goto" + typedef enum { false, true } bool; + // for C don't have bool as data type :( + bool disaster = false; + int i, j; + for(i=0;i<100;++i) + for(j=0;j<100;++j) + { + if((i + j) >= 150) + disaster = true; + if(disaster) + goto error; + } + error : + printf("Error occured at i = %d & j = %d.\n", i, j); + // this will print out "Error occured at i = 52 & j = 99." + + /////////////////////////////////////// // Typecasting /////////////////////////////////////// -- cgit v1.2.3 From e8e8b9c76fb578d3f8e90b90b3c8a1c59cf0e901 Mon Sep 17 00:00:00 2001 From: Andy B Date: Fri, 9 Oct 2015 15:48:21 +0100 Subject: [C/en] Accessing command line arguments in main --- c.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index db2ac930..345dca7f 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -27,6 +27,7 @@ Multi-line comments don't nest /* Be careful */ // comment ends on this line... */ // ...not this one! // Constants: #define +// Constants are written in all-caps out of convention, not requirement #define DAYS_IN_YEAR 365 // Enumeration constants are also ways to declare constants. @@ -56,6 +57,15 @@ int add_two_ints(int x1, int x2); // function prototype // Your program's entry point is a function called // main with an integer return type. int main(void) { + // your program +} + +// The command line arguments used to run your program are also passed to main +// argc being the number of arguments - your program's name counts as 1 +// argv is an array of character arrays - containing the arguments themselves +// argv[0] = name of your program, argv[1] = first argument, etc. +int main (int argc, char** argv) +{ // print output using printf, for "print formatted" // %d is an integer, \n is a newline printf("%d\n", 0); // => Prints 0 -- cgit v1.2.3 From c899b6605ef9667cb214c2163e7182ad41783be4 Mon Sep 17 00:00:00 2001 From: himanshu81494 Date: Fri, 9 Oct 2015 20:59:05 +0530 Subject: Update c.html.markdown --- c.html.markdown | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 29bc5a5b..f35a2098 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -307,8 +307,9 @@ int main(void) { exit(-1); break; } - - // using "goto" + /* + using "goto" in C + */ typedef enum { false, true } bool; // for C don't have bool as data type :( bool disaster = false; @@ -323,7 +324,10 @@ int main(void) { } error : printf("Error occured at i = %d & j = %d.\n", i, j); - // this will print out "Error occured at i = 52 & j = 99." + /* + https://ideone.com/GuPhd6 + this will print out "Error occured at i = 52 & j = 99." + */ /////////////////////////////////////// @@ -491,13 +495,15 @@ char c[] = "This is a test."; str_reverse(c); printf("%s\n", c); // => ".tset a si sihT" */ -//as we can return return only one variable -//to change values of more than one variables we use call by reference +/* +as we can return only one variable +to change values of more than one variables we use call by reference +*/ void swapTwoNumbers(int *a, int *b) { -int temp = *a; -*a = *b; -*b = temp; + int temp = *a; + *a = *b; + *b = temp; } /* int first = 10; -- cgit v1.2.3 From e418849d7696bd91a144ccdff451025899ac10e0 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Fri, 9 Oct 2015 11:52:08 -0400 Subject: [c/en] Added a section for header files. Added a section for header files. Included a discussion of what belongs in a header file and what does not. --- c.html.markdown | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index db2ac930..f1201eac 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,7 +6,7 @@ contributors: - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] - ["Marco Scannadinari", "https://marcoms.github.io"] - + - ["Zachary Ferguson", "https://github.io/zfergus2"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -630,6 +630,54 @@ typedef void (*my_fnp_type)(char *); ``` +Header files are an important part of c as they allow for the connection of c +source files and can simplify code and definitions by seperating them into +seperate files. + +Header files are syntaxtically similar to c source files but reside in ".h" +files. They can be included in your c source file by using the precompiler +command #include "example.h", given that example.h exists in the same directory +as the c file. + +```c +/* A safe guard to prevent the header from being defined too many times. This */ +/* happens in the case of circle dependency, the contents of the header is */ +/* already defined. */ +#ifndef EXAMPLE_H /* if EXAMPLE_H is not yet defined. */ +#define EXAMPLE_H /* Define the macro EXAMPLE_H. */ + +/* Other headers can be included in headers and therefore transitively */ +/* included into files that include this header. */ +#include + +/* Like c source files macros can be defined in headers and used in files */ +/* that include this header file. */ +#define EXAMPLE_NAME "Dennis Ritchie" +/* Function macros can also be defined. */ +#define ADD(a, b) (a + b) + +/* Structs and typedefs can be used for consistency between files. */ +typedef struct node +{ + int val; + struct node *next; +} Node; + +/* So can enumerations. */ +enum traffic_light_state {GREEN, YELLOW, RED}; + +/* Function prototypes can also be defined here for use in multiple files, */ +/* but it is bad practice to define the function in the header. Definitions */ +/* should instead be put in a c file. */ +Node createLinkedList(int *vals, int len); + +/* Beyond the above elements, other definitions should be left to a c source */ +/* file. Excessive includeds or definitions should, also not be contained in */ +/* a header file but instead put into separate headers or a c file. */ + +#endif /* End of the if precompiler directive. */ + +``` ## Further Reading Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language) -- cgit v1.2.3 From 79ced08e094e488eb21d026c882babc50d0ca168 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Tue, 13 Oct 2015 11:05:20 -0400 Subject: Added Header title + Added a header file title * Changed ```c to ```h --- c.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index f1201eac..0c4916ac 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -630,6 +630,8 @@ typedef void (*my_fnp_type)(char *); ``` +### Header Files + Header files are an important part of c as they allow for the connection of c source files and can simplify code and definitions by seperating them into seperate files. @@ -639,7 +641,7 @@ files. They can be included in your c source file by using the precompiler command #include "example.h", given that example.h exists in the same directory as the c file. -```c +```h /* A safe guard to prevent the header from being defined too many times. This */ /* happens in the case of circle dependency, the contents of the header is */ /* already defined. */ -- cgit v1.2.3 From 223c8140a0392f3a0a33b93222597de7a68679e4 Mon Sep 17 00:00:00 2001 From: Awal Garg Date: Wed, 14 Oct 2015 20:41:36 +0530 Subject: clarify that args' names are not required in proto with obligatory conventional warning --- c.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 3339032f..7c64cffe 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -54,6 +54,8 @@ int function_2(void); // Must declare a 'function prototype' before main() when functions occur after // your main() function. int add_two_ints(int x1, int x2); // function prototype +// although `int add_two_ints(int, int);` is also valid (no need to name the args), +// it is recommended to name arguments in the prototype as well for easier inspection // Your program's entry point is a function called // main with an integer return type. -- cgit v1.2.3 From 4599cd319fd409ddc697feffb607697285b26368 Mon Sep 17 00:00:00 2001 From: Awal Garg Date: Wed, 14 Oct 2015 20:44:45 +0530 Subject: [c/en] clarify common tripping point of newbies int foo () { printf("bar\n"); int x; // this is not valid in C89+ } --- c.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 3339032f..8d4cfd93 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -74,6 +74,9 @@ int main (int argc, char** argv) /////////////////////////////////////// // Types /////////////////////////////////////// + + // All variables MUST be declared at the top of the current block scope + // we declare them dynamically along the code for the sake of the tutorial // ints are usually 4 bytes int x_int = 0; -- cgit v1.2.3 From 328ceb1a94f0fc6736e001ad0e7b6646173ca207 Mon Sep 17 00:00:00 2001 From: Elton Viana Date: Wed, 14 Oct 2015 21:44:42 -0300 Subject: Some extra information --- c.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index db2ac930..aaf176c1 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -221,7 +221,7 @@ int main(void) { 0 || 1; // => 1 (Logical or) 0 || 0; // => 0 - // Conditional expression ( ? : ) + // Conditional ternary expression ( ? : ) int e = 5; int f = 10; int z; @@ -291,6 +291,8 @@ int main(void) { for (i = 0; i <= 5; i++) { ; // use semicolon to act as the body (null statement) } + // Or + for (i = 0; i <= 5; i++); // branching with multiple choices: switch() switch (a) { -- cgit v1.2.3 From 27bb1a1e80b727b00b5ea8045d4bfa07eb1293bd Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Wed, 14 Oct 2015 21:59:42 -0400 Subject: Removed separate code section Removed the separate code section for header files --- c.html.markdown | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 0c4916ac..5e8e13c1 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -628,9 +628,7 @@ typedef void (*my_fnp_type)(char *); // , | left to right // //---------------------------------------------------// -``` - -### Header Files +/******************************* Header Files ********************************** Header files are an important part of c as they allow for the connection of c source files and can simplify code and definitions by seperating them into @@ -640,8 +638,8 @@ Header files are syntaxtically similar to c source files but reside in ".h" files. They can be included in your c source file by using the precompiler command #include "example.h", given that example.h exists in the same directory as the c file. +*/ -```h /* A safe guard to prevent the header from being defined too many times. This */ /* happens in the case of circle dependency, the contents of the header is */ /* already defined. */ -- cgit v1.2.3 From 8417366a1b2d2b7bce35e2e4a4fdccee2a60f457 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Thu, 15 Oct 2015 14:50:20 -0400 Subject: Cleaned up c file --- c.html.markdown | 4 ---- 1 file changed, 4 deletions(-) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index 7bb363b3..a8f71057 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,12 +6,8 @@ contributors: - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] - ["Marco Scannadinari", "https://marcoms.github.io"] -<<<<<<< HEAD - ["Zachary Ferguson", "https://github.io/zfergus2"] -======= - ["himanshu", "https://github.com/himanshu81494"] - ->>>>>>> refs/remotes/adambard/master --- Ah, C. Still **the** language of modern high-performance computing. -- cgit v1.2.3 From 2acf7822bdf45909d6c92fe30515b2a3b40e6c22 Mon Sep 17 00:00:00 2001 From: Brendan Batliner Date: Thu, 15 Oct 2015 16:34:53 -0500 Subject: Update c.html.markdown Added additional info on dynamically allocated arrays in C. --- c.html.markdown | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index a8f71057..b4129f7a 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -445,6 +445,17 @@ int main (int argc, char** argv) for (xx = 0; xx < 20; xx++) { *(my_ptr + xx) = 20 - xx; // my_ptr[xx] = 20-xx } // Initialize memory to 20, 19, 18, 17... 2, 1 (as ints) + + // Note that there is no standard way to get the length of a + // dynamically allocated array in C. Because of this, if your arrays are + // going to be passed around your program a lot, you need another variable + // to keep track of the number of elements (size) of an array. See the + // functions section for more info. + int size = 10; + int *my_arr = malloc(sizeof(int) * size); + // Add an element to the array + my_arr = realloc(my_arr, ++size); + my_arr[10] = 5; // Dereferencing memory that you haven't allocated gives // "unpredictable results" - the program is said to invoke "undefined behavior" @@ -530,6 +541,23 @@ swapTwoNumbers(&first, &second); printf("first: %d\nsecond: %d\n", first, second); // values will be swapped */ + +/* +With regards to arrays, they will always be passed to functions +as pointers. Even if you statically allocate an array like `arr[10]`, +it still gets passed as a pointer to the first element in any function calls. +Again, there is no standard way to get the size of a dynamically allocated +array in C. +*/ +// Size must be passed! +// Otherwise, this function has no way of knowing how big the array is. +void printIntArray(int *arr, int size) { + int i; + for (i = 0; i < size; i++) { + printf("arr[%d] is: %d\n", i, arr[i]); + } +} + // if referring to external variables outside function, must use extern keyword. int i = 0; void testFunc() { -- cgit v1.2.3 From 36fe7e1ccec6be05a98b9a8bee633073dca0d24d Mon Sep 17 00:00:00 2001 From: Brendan Batliner Date: Thu, 15 Oct 2015 16:38:42 -0500 Subject: Added example of printIntArray in C --- c.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'c.html.markdown') diff --git a/c.html.markdown b/c.html.markdown index b4129f7a..3d632eab 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -557,6 +557,12 @@ void printIntArray(int *arr, int size) { printf("arr[%d] is: %d\n", i, arr[i]); } } +/* +int my_arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; +int size = 10; +printIntArray(my_arr, size); +// will print "arr[0] is: 1" etc +*/ // if referring to external variables outside function, must use extern keyword. int i = 0; -- cgit v1.2.3