#include int main(){ int a = 5; int b = 10; if(a < b){ // Braces are optional for one-line printf("Logic works today!\n"); // If we had something to say here, it would make the braces mandatory } else if (b < a) { // Some people like this better (visual studio does this) printf("Logic is broke\n"); } else printf("That was weird"); // Some people will be upset // Here is generally prefered, but not universal /* * = gives a return value of the value assigned * Integers are ok for truth values! * 0 is false, anything else (even negatives) is true * Be careful! = doesn't generate an error, but == might be what you mean! */ if (a = 5) { // This is TRUE and changes a to 5 printf("a = %d\n", a); } char c; // Go ahead and use c like it's a boolean // Typical loop int i = 0; while(i < 10){ printf("i = %d\n", i); i++; } // int i = 0; // This is a "redefinition of i" i = 0; while(i < 10) printf("i = %d\n", i++); // i++: Take the value, THEN increase the value of i // ++i: Increase the value, THEN take the value // i is still around! Let's do better, and contain j // Starting a block like this, contains inside variables { int j = 0; // j is local to the block we just started! while(j < 10){ printf("j = %d\n", j); j++; } } // j is out of scope after this int j = 0; // We want this to work // For loops! C89 style (assuming you don't want k afterward) { int k; for(k = 0; k < 10; k++) printf("k = %d\n", k); } // C99 and later for(int k = 0; k < 10; k++) printf("k = %d\n", k); int add_aandb(){ return a+b; } printf("a + b = %d\n", add_aandb()); double g = a + b; printf("g = %lf\n", g); // This is a variable that holds the memory address of a function that // takes no parameters and returns an int int (*foo)() = add_aandb; // foo is the name I made up printf("a + b = %d\n", foo()); printf("8 / 3 = %d\n", 8/3); printf("8 / 3 = %lf\n", 8./3); int x,y; // Yes, you can make two on the same line x = (y = 10) + 5; printf("x = %d, y = %d\n", x, y); // No effect on a and b from outside this function double add_numbers(double a, int b){ return a + b; } // void: This doesn't return anything void print_message(char* message){ printf("The message was: %s\n", message); } print_message("There is a chicken in my kitchen\n"); return 0; }