Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))
Let's begin by writing our first C program that prints the message "Hello, world!" on the display console: Show
Hello, world! Step 1: Write the Source Code: Enter the following source codes using a programming text editor (such as NotePad++ for Windows or gEdit for UNIX/Linux/Mac) or an Interactive Development Environment (IDE) (such as CodeBlocks, Eclipse, NetBeans or MS Visual Studio - Read the respective "How-To" article on how to install and get started with these IDEs). Do not enter the line numbers (on the left panel), which were added to help in the explanation. Save the source file as " // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c4". A C source file should be saved with a file extension of " // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c5". You should choose a filename which reflects the purpose of the program. 1 2 3 4 5 6 7 8 9 Step 2: Build the Executable Code: Compile and Link (aka Build) the source code " // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c4" into executable code (" // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c7" in Windows or " // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c8" in UNIX/Linux/Mac).
Step 3: Run the Executable Code: Execute (Run) the program.
Brief Explanation of the Program/* ...... */
#include // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!7" is called a preprocessor directive. A preprocessor directive begins with a // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!8 sign, and is processed before compilation. The directive " // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!7" tells the preprocessor to include the " 1 2 3 4 5 6 7 8 9 100" header file to support input/output operations. This line shall be present in all our programs. I will explain its meaning later. int main() { ...... } 1 2 3 4 5 6 7 8 9 101 function. The 1 2 3 4 5 6 7 8 9 101 function is the entry point of program execution. 1 2 3 4 5 6 7 8 9 101 is required to return an 1 2 3 4 5 6 7 8 9 104 (integer). printf("Hello, world!\n"); 1 2 3 4 5 6 7 8 9 105 to print the string "Hello, world!" followed by a newline ( 1 2 3 4 5 6 7 8 9 106) to the console. The newline ( 1 2 3 4 5 6 7 8 9 106) brings the cursor to the beginning of the next line. return 0; 1 2 3 4 5 6 7 8 9 101 function and returns a value of 0 to the operating system. Typically, return value of 0 signals normal termination; whereas value of non-zero (usually 1) signals abnormal termination. This line is optional. C compiler will implicitly insert a " 1 2 3 4 5 6 7 8 9 109" to the end of the 1 2 3 4 5 6 7 8 9 101 function. C Terminology and SyntaxStatement: A programming statement performs a piece of programming action. It must be terminated by a semi-colon ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 191) (just like an English sentence is ended with a period) as in Lines 7 and 8. Preprocessor Directive: The // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!7 (Line 4) is a preprocessor directive and NOT a programming statement. A preprocessor directive begins with hash sign ( // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!8). It is processed before compiling the program. A preprocessor directive is NOT terminated by a semicolon - Take note of this rule. Block: A block is a group of programming statements enclosed by braces 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 194. This group of statements is treated as one single unit. There is one block in this program, which contains the body of the 1 2 3 4 5 6 7 8 9 101 function. There is no need to put a semi-colon after the closing brace. Comments: A multi-line comment begins with // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!4 and ends with // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!5. An end-of-line comment begins with // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!6 and lasts till the end of the line. Comments are NOT executable statements and are ignored by the compiler. But they provide useful explanation and documentation. Use comments liberally. Whitespaces: Blank, tab, and newline are collectively called whitespaces. Extra whitespaces are ignored, i.e., only one whitespace is needed to separate the tokens. But they could help you and your readers better understand your program. Use extra whitespaces liberally. Case Sensitivity: C is case sensitive - a ROSE is NOT a Rose, and is NOT a rose. The Process of Writing a C ProgramStep 1: Write the source codes ( // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c5) and header files ( The sum of 55 and 66 is 121.0). Step 2: Pre-process the source codes according to the preprocessor directives. The preprocessor directives begin with a hash sign ( // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!8), such as // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!7 and The sum of 55 and 66 is 121.3. They indicate that certain manipulations (such as including another file or replacement of symbols) are to be performed BEFORE compilation. Step 3: Compile the pre-processed source codes into object codes ( The sum of 55 and 66 is 121.4, The sum of 55 and 66 is 121.5). Step 4: Link the compiled object codes with other object codes and the library object codes ( The sum of 55 and 66 is 121.6, The sum of 55 and 66 is 121.7)to produce the executable code ( The sum of 55 and 66 is 121.8). Step 5: Load the executable code into computer memory. Step 6: Run the executable code. C Program TemplateYou can use the following template to write your C programs. Choose a meaningful filename for you source file that reflects the purpose of your program with file extension of " // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c5". Write your programming statements inside the body of the 1 2 3 4 5 6 7 8 9 101 function. Don't worry about the other terms for the time being. I will explain them later. 1 2 3 4 5 6 7 8 9 10 Let's Write a Program to Add a Few NumbersExample: Adding Two IntegersLet's write a C program called " int integer1, integer2, sum;1" to add two integers as follows: Add2Integers.c1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 The sum of 55 and 66 is 121. Dissecting the Programint integer1; 1 2 3 4 5 6 7 8 9 104 (integer) variables: int integer1, integer2, sum;3, int integer1, integer2, sum;4, and int integer1, integer2, sum;5. A variable is a named storage location that can store a value of a particular data type, in this case, 1 2 3 4 5 6 7 8 9 104 (integer). You can declare one variable in one statement. You could also declare many variables in one statement, separating with commas, e.g., int integer1, integer2, sum; integer1 = 55; int integer1, integer2, sum;3 and int integer1, integer2, sum;4; compute their sum and store in variable int integer1, integer2, sum;5. printf("The sum of %d and %d is %d.\n", integer1, integer2, sum); 1 2 3 4 5 6 7 8 9 105 function to print the result. The first argument in 1 2 3 4 5 6 7 8 9 105 is known as the formatting string, which consists of normal texts and so-called conversion specifiers. Normal texts will be printed as they are. A conversion specifier begins with a percent sign ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 202), followed by a code to indicate the data type, such as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 203 for decimal integer. You can treat the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204 as placeholders, which will be replaced by the value of variables given after the formatting string in sequential order. That is, the first 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204 will be replaced by the value of int integer1, integer2, sum;3, second 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204 by int integer1, integer2, sum;4, and third 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204 by int integer1, integer2, sum;5. The 1 2 3 4 5 6 7 8 9 106 denotes a newline character. Printing a 1 2 3 4 5 6 7 8 9 106 bring the cursor to the beginning of the next line. Example: Prompting User for InputsIn the previous example, we assigned fixed values into variables int integer1, integer2, sum;3 and int integer1, integer2, sum;4. Instead of using fixed values, we shall prompt the user to enter two integers. PromptAdd2Integers.c1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Enter first integer: 55 Enter second integer: 66 The sum of 55 and 66 is 121. Disecting the Programint integer1, integer2, sum; 1 2 3 4 5 6 7 8 9 104 (integer) variables: int integer1, integer2, sum;3, int integer1, integer2, sum;4, and int integer1, integer2, sum;5 in one statement. printf("Enter first integer: "); 1 2 3 4 5 6 7 8 9 105 function to put out a prompting message. scanf("%d", &integer1); 1 2 3 4 5 6 7 8 900 function to read the user input from the keyboard and store the value into variable int integer1, integer2, sum;3.The first argument of 1 2 3 4 5 6 7 8 900 is the formatting string (similar to 1 2 3 4 5 6 7 8 9 105).The 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204 conversion specifier provides a placeholder for an integer, which will be substituted by variable int integer1, integer2, sum;3. Take note that we have to place an ampersand sign ( 1 2 3 4 5 6 7 8 906), which stands for address-of operator, before the variable, I shall explain its significance later. It is important to stress that missing ampersand ( 1 2 3 4 5 6 7 8 906) in 1 2 3 4 5 6 7 8 900 is a common error, which leads to abnormal termination of the program. Reading Multiple IntegersYou can read multiple items in one 1 2 3 4 5 6 7 8 900 statement as follows: In the 1 2 3 4 5 6 7 8 900, the first 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204 puts the first integer entered into variable int integer1, integer2, sum;3, and the second 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 204 puts into int integer1, integer2, sum;4. Again, remember to place an ampersand ( 1 2 3 4 5 6 7 8 906) before the variables in 1 2 3 4 5 6 7 8 900. Exercises
What is a Program?A program is a sequence of instructions (called programming statements), executing one after another - usually in a sequential manner, as illustrated in the previous example and the following flow chart. Example (Sequential): The following program ( 1 2 3 4 5 6 7 8 927) prompts user for the radius of a circle, and prints its area and circumference. Take note that the programming statements are executed sequentially - one after another in the order that they are written. 1 2 3 4 5 6 7 8 91 1 2 3 4 5 6 7 8 92 Dissecting the Programdouble radius, circumference, area; 1 2 3 4 5 6 7 8 928 variables called 1 2 3 4 5 6 7 8 929, 1 2 3 4 5 6 7 8 930 and 1 2 3 4 5 6 7 8 931. A 1 2 3 4 5 6 7 8 928 variable, unlike 1 2 3 4 5 6 7 8 9 104, can hold real number (or floating-point number) such as 1.23 or 4.5e6. We also declare a 1 2 3 4 5 6 7 8 928 variable called 1 2 3 4 5 6 7 8 935 and initialize its value to 3.1416. printf("Enter the radius: "); 1 2 3 4 5 6 7 8 936 to put up a prompt message, and 1 2 3 4 5 6 7 8 900 to read the user input into variable 1 2 3 4 5 6 7 8 929. Take note that the 1 2 3 4 5 6 7 8 939 conversion specifier for 1 2 3 4 5 6 7 8 928 ( 1 2 3 4 5 6 7 8 941 stands for long float). Also remember to place an ampersand ( 1 2 3 4 5 6 7 8 906) before 1 2 3 4 5 6 7 8 929. area = radius * radius * pi; printf("The radius is %lf.\n", radius); 1 2 3 4 5 6 7 8 939 conversion specifier to print a 1 2 3 4 5 6 7 8 928. Take note that the programming statements inside the 1 2 3 4 5 6 7 8 9 101 are executed one after another, sequentially. Exercises
What is a Variable?Computer programs manipulate (or process) data. A variable is used to store a piece of data for processing. It is called variable because you can change the value stored. More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value of that type.
The above diagram illustrates 2 types of variables: 1 2 3 4 5 6 7 8 9 104 and 1 2 3 4 5 6 7 8 928. An 1 2 3 4 5 6 7 8 9 104 variable stores an integer (whole number). A 1 2 3 4 5 6 7 8 928 variable stores a real number. To use a variable, you need to first declare its name and type, in one of the following syntaxes: Take note that:
For example, Once a variable is declared, you can assign and re-assign a value to a variable, via the assignment operator " 1 2 3 4 5 6 7 8 974". For example, Take note that:
x=x+1?Assignment ( 1 2 3 4 5 6 7 8 974) in programming is different from equality in Mathematics. e.g., " 1 2 3 4 5 6 7 8 981" is invalid in Mathematics. However, in programming, it means compute the value of 1 2 3 4 5 6 7 8 982 plus 1, and assign the result back to variable 1 2 3 4 5 6 7 8 982. "x+y=1" is valid in Mathematics, but is invalid in programming. In programming, the RHS of " 1 2 3 4 5 6 7 8 974" has to be evaluated to a value; while the LHS shall be a variable. That is, evaluate the RHS first, then assign to LHS. Some languages uses 1 2 3 4 5 6 7 8 985 as the assignment operator to avoid confusion with equality. Basic Arithmetic OperationsThe basic arithmetic operators are: OperatorMeaningExample1 2 3 4 5 6 7 8 986Addition 1 2 3 4 5 6 7 8 987 1 2 3 4 5 6 7 8 988Subtraction 1 2 3 4 5 6 7 8 989 1 2 3 4 5 6 7 8 926Multiplication 1 2 3 4 5 6 7 8 991 1 2 3 4 5 6 7 8 992Division 1 2 3 4 5 6 7 8 993 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 202Modulus (Remainder) 1 2 3 4 5 6 7 8 995 1 2 3 4 5 6 7 8 996Increment by 1 (Unary) 1 2 3 4 5 6 7 8 997 or 1 2 3 4 5 6 7 8 998 1 2 3 4 5 6 7 8 999Decrement by 1 (Unary) // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c00 or // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c01 Addition, subtraction, multiplication, division and remainder are binary operators that take two operands (e.g., 1 2 3 4 5 6 7 8 987); while negation (e.g., // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c03), increment and decrement (e.g., 1 2 3 4 5 6 7 8 998, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c00) are unary operators that take only one operand. ExampleThe following program ( // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c06) illustrates these arithmetic operations. 1 2 3 4 5 6 7 8 93 1 2 3 4 5 6 7 8 94 Dissecting the Programint number1, number2; 1 2 3 4 5 6 7 8 9 104 (integer) variables // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c08, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c09, int integer1, integer2, sum;5, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c11, 1 2 3 4 5 6 7 8 925, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c13, and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c14, needed in this program. printf("Enter two integers (separated by space): "); // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c08 and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c09, respectively. sum = number1 + number2; // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c08 and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c09. Take note that division of two integers produces a truncated integer, e.g., // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c19, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c20, and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c21. printf("The sum, difference, product, quotient and remainder of %d and %d are %d, %d, %d, %d, %d.\n", ++number1; // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c22, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c23, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c24, // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c25 and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c26, which work on two operands (binary operators), // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c27 and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c28 operate on only one operand (unary operators). 1 2 3 4 5 6 7 8 997 is equivalent to // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c30, i.e., increment x by 1. You may place the increment operator before or after the operand, i.e., 1 2 3 4 5 6 7 8 997 (pre-increment) or 1 2 3 4 5 6 7 8 998 (post-increment). In this example, the effects of pre-increment and post-increment are the same. I shall point out the differences in later section. Exercises
What If Your Need To Add a Thousand Numbers? Use a Loop!Suppose that you want to add all the integers from 1 to 1000. If you follow the previous examples, you would require a thousand-line program! Instead, you could use a loop in your program to perform a repetitive task, that is what the dumb computers are good at. ExampleTry the following program // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c43, which sums all the integers from 1 to an upperbound provided by the user, using a so-called while-loop. 1 2 3 4 5 6 7 8 95 1 2 3 4 5 6 7 8 96 Dissecting the Programint sum = 0; 1 2 3 4 5 6 7 8 9 104 variable named int integer1, integer2, sum;5 and initializes it to 0. This variable will be used to accumulate numbers over the steps in the repetitive loop. printf("Enter the upperbound: "); int number = 1; 1 2 3 4 5 6 7 8 97 As illustrated in the flow chart, the initialization statement is first executed. The test is then checked. If the test is true, the body is executed. The test is checked again and the process repeats until the test is false. When the test is false, the loop completes and program execution continues to the next statement after the loop. In our program, the initialization statement declares an 1 2 3 4 5 6 7 8 9 104 variable named // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c47 and initializes it to 1. The test checks if // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c47 is equal to or less than the // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c49. If it is true, the current value of // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c47 is added into the int integer1, integer2, sum;5, and the statement // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c52 increases the value of // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c47 by 1. The test is then checked again and the process repeats until the test is false (i.e., // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c47 increases to // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c55), which causes the loop to terminate. Execution then continues to the next statement (in Line 22). In this example, the loop repeats // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c49 times. After the loop is completed, Line 22 prints the result with a proper description. Exercises
Conditional (or Decision)What if you want to sum all the odd numbers and also all the even numbers between 1 and 1000? There are many way to do this. You could declare two variables: // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c72 and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c73. You can then use a conditional statement to check whether the number is odd or even, and accumulate the number into the respective sum. The program // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c74 is as follows: 1 2 3 4 5 6 7 8 98 1 2 3 4 5 6 7 8 99 Dissecting the Programint sumOdd = 0; 1 2 3 4 5 6 7 8 9 104 variables named // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c72 and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c73 and initialize them to 0, for accumulating the odd and even numbers, respectively. if (number % 2 == 0) { For a if-then statement, the true-body is executed if the test is true. Otherwise, nothing is done and the execution continues to the next statement. For a if-then-else statement, the true-body is executed if the test is true; otherwise, the false-body is executed. Execution is then continued to the next statement. In our program, we use the remainder operator // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c78 to compute the remainder of // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c47 divides by // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c80. We then compare the remainder with // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c81 to test for even number. Comparison OperatorsThere are six comparison (or relational) operators: OperatorMeaningExample// Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c82Equal to // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c83 // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c84Not equal to // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c85 // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c86Greater than // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c87 // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c88Greater than or equal to // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c89 // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c90Less than // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c91 // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c92Less than or equal to // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c93 Take note that the comparison operator for equality is a double-equal sign // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c94; whereas a single-equal sign // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c95 is the assignment operator. Combining Simple ConditionsSuppose that you want to check whether a number 1 2 3 4 5 6 7 8 982 is between // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c57 and // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c98 (inclusive), i.e., // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c99. There are two simple conditions here, // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!00. In programming, you cannot write // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c99, but need to write // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!02, where " // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!03" denotes the " // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!04" operator. Similarly, suppose that you want to check whether a number 1 2 3 4 5 6 7 8 982 is divisible by 2 OR by 3, you have to write // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!06 where " // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!07" denotes the " // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!08" operator. There are three so-called logical operators that operate on the boolean conditions: OperatorMeaningExample// Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!03Logical AND // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!02 // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!07Logical OR // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!12 // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!13Logical NOT // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!14 For examples: Exercises
Type double & Floating-Point NumbersRecall that a variable in C has a name and a type, and can hold a value of only that particular type. We have so far used a type called 1 2 3 4 5 6 7 8 9 104. A 1 2 3 4 5 6 7 8 9 104 variable holds only integers (whole numbers), such as 1 2 3 4 5 6 7 8 956 and 1 2 3 4 5 6 7 8 957. In programming, real numbers such as 1 2 3 4 5 6 7 8 959 and 1 2 3 4 5 6 7 8 960 are called floating-point numbers, and belong to a type called 1 2 3 4 5 6 7 8 928. You can express floating-point numbers in fixed notation (e.g., // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!24, // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!25) or scientific notation (e.g., 1 2 3 4 5 6 7 8 962, // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!27) where // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!28 or // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!29 denote the exponent of base 10. Example// Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c0 // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c1 Mixing int and double, and Type CastingAlthough you can use a 1 2 3 4 5 6 7 8 928 to keep an integer value (e.g., // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!31), you should use an 1 2 3 4 5 6 7 8 9 104 for integer. This is because 1 2 3 4 5 6 7 8 9 104 is far more efficient than 1 2 3 4 5 6 7 8 928, in terms of running times and memory requirement. At times, you may need both 1 2 3 4 5 6 7 8 9 104 and 1 2 3 4 5 6 7 8 928 in your program. For example, keeping the sum from // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c57 to // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c98 ( // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!39) as an 1 2 3 4 5 6 7 8 9 104, and their average // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!41 as a 1 2 3 4 5 6 7 8 928. You need to be extremely careful when different types are mixed. It is important to note that:
You can assign an integer value to a 1 2 3 4 5 6 7 8 928 variable. The integer value will be converted to a double value automatically, e.g., // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!59. For example, However, if you assign a 1 2 3 4 5 6 7 8 928 value to an 1 2 3 4 5 6 7 8 9 104 variable, the fractional part will be lost. For example, Some C compilers signal a warning for truncation, while others do not. You should study the "warning messages" (if any) carefully - which signals a potential problem in your program, and rewrite the program if necessary. C allows you to ignore the warning and run the program. But, the fractional part will be lost during the execution. Type Casting OperatorsIf you are certain that you wish to carry out the type conversion, you could use the so-called type cast operator. The type cast operation returns an equivalent value in the new-type specified. // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c2 For example, Similarly, you can explicitly convert an 1 2 3 4 5 6 7 8 9 104 value to 1 2 3 4 5 6 7 8 928 by invoking type-casting operation too. ExampleTry the following program and explain the outputs produced: // Windows (CMD shell) - Build "Hello.c" into "Hello.exe" > gcc -o Hello.exe Hello.c // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello" $ gcc -o Hello Hello.c3 The first average is incorrect, as // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!64 produces an 1 2 3 4 5 6 7 8 9 104 (of // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!66). For the second average, the value of int integer1, integer2, sum;5 (of 1 2 3 4 5 6 7 8 9 104) is first converted to 1 2 3 4 5 6 7 8 928. Subsequently, // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!70 produces 1 2 3 4 5 6 7 8 928. For the third average, // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!72 produces 1 2 3 4 5 6 7 8 928. For the fourth average, // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!64 produces an 1 2 3 4 5 6 7 8 9 104 (of // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!66), which is then casted to 1 2 3 4 5 6 7 8 928 (of // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!78) and assigned to // Windows (CMD shell) - Run "Hello.exe" (.exe is optional) > Hello Hello, world! // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory) $ ./Hello Hello, world!79 (of 1 2 3 4 5 6 7 8 928). Exercises
SummaryI have presented the basics for you to get start in programming. To learn programming, you need to understand the syntaxes and features involved in the programming language that you chosen, and you have to practice, practice and practice, on as many problems as you could. Which of the following is not a function from 1 2 3 4 to 1 2 3?Answer: {(1,2), (1,4), (2,5), (3,8)} is not a function. Step-by-step explanation: A function is a relation in which every x value is associated with exactly one y value.
What is a function f from A to B?A function f : A −→ B consists of: (1) the set A, which is called the domain of f, (2) the set B, which is called the range of f, (3) a rule which assigns to every element a ∈ A an element b ∈ B, which we denote by f(a).
What is the formula for function of a function?Functions are generally represented as y = f(x) and it states the dependence of y on x, or we say that y is a function of x. Functions formulas define the mathematical rules to connect one set of elements to another set of elements.
What are the 4 types of function?There are 4 types of functions:. Functions with arguments and return values. This function has arguments and returns a value: ... . Functions with arguments and without return values. ... . Functions without arguments and with return values. ... . Functions without arguments and without return values.. |