C programming Note
-
Return a value mean success.
#include <stdio.h> int main() { printf( "I am alive! Beware.\n" ); getchar(); return 0; }
Let’s look at the elements of the program. The #include is a “preprocessor” directive that tells the compiler to put code from the header called stdio.h into our program before actually creating the executable. By including header files, you can gain access to many different functions–both the printf and getchar functions are included in stdio.h. The next important line is int main(). This line tells the compiler that there is a function named main, and that the function returns an integer, hence int. The “curly braces” ({ and }) signal the beginning and end of functions and other code blocks. If you have programmed in Pascal, you will know them as BEGIN and END. Even if you haven’t programmed in Pascal, this is a good way to think about their meaning. The printf function is the standard C way of displaying output on the screen. The quotes tell the compiler that you want to output the literal string as-is (almost). The ‘\n’ sequence is actually treated as a single character that stands for a newline (we’ll talk about this later in more detail); for the time being, just remember that there are a few sequences that, when they appear in a string literal, are actually not displayed literally by printf and that ‘\n’ is one of them. The actual effect of ‘\n’ is to move the cursor on your screen to the next line. Notice the semicolon: it tells the compiler that you’re at the end of a command, such as a function call. You will see that the semicolon is used to end many lines in C. The next command is getchar(). This is another function call: it reads in a single character and waits for the user to hit enter before reading the character. This line is included because many compiler environments will open a new console window, run the program, and then close the window before you can see the output. This command keeps that window from closing because the program is not done yet because it waits for you to hit enter. Including that line gives you time to see the program run. Finally, at the end of the program, we return a value from main to the operating system by using the return statement. This return value is important as it can be used to tell the operating system whether our program succeeded or not. A return value of 0 means success. From http://www.cprogramming.com/tutorial/c/lesson2.html
Quiz question:囧
1. What is the correct value to return to the operating system upon the successful completion of a program? A. -1 B. 1 C. 0 D. Programs do not return a value.
-
All non-zero number means true, however, system automatically give 1 mean true.
Before discussing the actual structure of the if statement, let us examine the meaning of TRUE and FALSE in computer terminology. A true statement is one that evaluates to a nonzero number. A false statement evaluates to zero. When you perform comparison with the relational operators, the operator will return 1 if the comparison is true, or 0 if the comparison is false. For example, the check 0 == 2 evaluates to 0. The check 2 == 2 evaluates to a 1. If this confuses you, try to use a printf statement to output the result of those various comparisons (for example printf ( “%d”, 2 == 1 );) From:cprogramming.com (http://www.cprogramming.com/tutorial/c/lesson2.html)
Quiz question:
1. Which of the following is true? A. 1 B. 66 C. .1 D. -1 E. All of the above
-
Switch case in C: Break ,this keyword, prevents the program from falling through and executing the code in all the other case statements
switch ( <variable> ) { case this-value: Code to execute if <variable> == this-value break; case that-value: Code to execute if <variable> == that-value break; ... default: Code to execute if <variable> does not equal the value following any of the cases break; }
Example:we will get "ZeroHello world" from following codeint x=0; switch(x) { case 1: printf( "One" ); case 0: printf( "Zero" ); case 2: printf( "Hello World" ); }(http://www.cprogramming.com/tutorial/c/lesson5.html)