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 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