Let's begin by writing our first C program that prints the message "Hello, world!" on the display console:
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].
- On IDE [such as CodeBlocks], push the "Build" button.
- On Text editor with the GNU GCC compiler, start a CMD Shell [Windows] or Terminal [Mac, Linux] and issue these commands:
// 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.c
where// 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.c
9 is the name of GCC C compiler;// 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!
0 option specifies the output filename ["// 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.c
7" for 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.c
8" for UNIX/Linux/Mac]; "// 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.c
4" is the input source file.
Step 3: Run the Executable Code: Execute [Run] the program.
- On IDE [such as CodeBlocks], push the "Run" button.
- On Text Editor with GNU GCC compiler, issue these command from CMD Shell [Windows] or Terminal [UNIX/Linux/Mac]:
// 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!
/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the compiler. But they provide useful explanation and documentation to your readers [and to yourself three days later]. There are two kinds of comments:
- 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. It may span more than one lines [as in Lines 1-3]. - 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 until the end of the current line [as in Lines 4, 6, 7, 8, and 9].
#include
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" 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[] { ...... }
defines the so-called
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"];
We invoke the function
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;
terminates the
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 Syntax
Statement: 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 Program
Step 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 Template
You 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 Numbers
Example: Adding Two Integers
Let's write a C program called "
int integer1, integer2, sum;1" to add two integers as follows:Add2Integers.c
1 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 Program
int integer1;
int integer2;
int sum;
We first declare three
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;
integer2 = 66;
sum = integer1 + integer2;
We assign values to variables
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];
We use the
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 Inputs
In 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.c
1 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 Program
int integer1, integer2, sum;
We first declare three
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: "];
We use the
1 2 3 4 5 6 7 8 9 105 function to put out a prompting message.
scanf["%d", &integer1];
We then use the
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 Integers
You 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
- Print each of the following patterns. Use one
1 2 3 4 5 6 7 8 9 10
5 statement for each line of outputs. End each line by printing a newline [1 2 3 4 5 6 7 8 9 10
6].1 2 3 4 5 6 7 8 9
0 - Print the above patterns using ONE
1 2 3 4 5 6 7 8 9 10
5 statement. - Write a program to prompt user for 5 integers and print their sum. Use five
1 2 3 4 5 6 7 8 9 10
4 variablesint integer1, integer2, sum;
3 to1 2 3 4 5 6 7 8 9
221 2 3 4 5 6 7 8 9
23 to store the five integers. - Write a program to prompt user for 5 integers and print their product. Use an
1 2 3 4 5 6 7 8 9 10
4 variable1 2 3 4 5 6 7 8 9
25 to store the product and operator1 2 3 4 5 6 7 8 9
26 for multiplication.
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 92Dissecting the Program
double radius, circumference, area;
double pi = 3.14159265;
We declare three
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: "];
scanf["%lf", &radius];
We use
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;
circumference = 2.0 * radius * pi;
perform the computation.
printf["The radius is %lf.\n", radius];
printf["The area is %lf.\n", area];
printf["The circumference is %lf.\n", circumference];
Again, we use
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
- Follow the above example, write a program to print the area and perimeter of a rectangle. Your program shall prompt the user for the length and width of the rectangle, in
1 2 3 4 5 6 7 8 9
28s. - Follow the above example, write a program to print the surface area and volume of a cylinder. Your program shall prompt the user for the radius and height of the cylinder, in
1 2 3 4 5 6 7 8 9
28s.
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.
- A variable has a name [or identifier], e.g.,
1 2 3 4 5 6 7 8 9
29,1 2 3 4 5 6 7 8 9
31,1 2 3 4 5 6 7 8 9
51,1 2 3 4 5 6 7 8 9
52. The name is needed to uniquely identify and reference a variable, so as to assign a value to the variable [e.g.,1 2 3 4 5 6 7 8 9
53], and retrieve the value stored [e.g.,1 2 3 4 5 6 7 8 9
54]. - A variable has a type. Examples of type are:
1 2 3 4 5 6 7 8 9 10
4: for integers [whole numbers] such as1 2 3 4 5 6 7 8 9
56 and1 2 3 4 5 6 7 8 9
57;1 2 3 4 5 6 7 8 9
28: for floating-point or real numbers, such as1 2 3 4 5 6 7 8 9
59,1 2 3 4 5 6 7 8 9
60,1 2 3 4 5 6 7 8 9
61,1 2 3 4 5 6 7 8 9
62,1 2 3 4 5 6 7 8 9
63 having a decimal point and fractional part, in fixed or scientific notations.
- A variable can store a value of the declared type. It is important to take note that a variable is associated with a type, and can only store value of that particular type. For example, a
1 2 3 4 5 6 7 8 9 10
4 variable can store an integer value such as1 2 3 4 5 6 7 8 9
56, but NOT real number such as1 2 3 4 5 6 7 8 9
66, nor texts such as1 2 3 4 5 6 7 8 9
67. The concept of type was introduced into the early programming languages to simplify interpretation of data.
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:
- Each declaration statement is terminated with a semi-colon [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1]. - In multiple-variable declaration, the names are separated by commas [
1 2 3 4 5 6 7 8 9
73]. - The symbol
1 2 3 4 5 6 7 8 9
74, known as the assignment operator, can be used to assign an initial value [of the declared type] to the variable.
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:
- Each variable can only be declared once.
- You can declare a variable anywhere inside the program, as long as it is declared before it is being used.
- Once the type of a variable is declared, it can only store a value belonging to this particular type. For example, an
1 2 3 4 5 6 7 8 9 10
4 variable can hold only integer such as1 2 3 4 5 6 7 8 9
56, and NOT floating-point number such as1 2 3 4 5 6 7 8 9
78 or text string such as1 2 3 4 5 6 7 8 9
67. - The type of a variable cannot be changed inside the program.
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 Operations
The 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.Example
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.c06] illustrates these arithmetic operations.
1 2 3 4 5 6 7 8 93
1 2 3 4 5 6 7 8 94Dissecting the Program
int number1, number2;
int sum, difference, product, quotient, remainder;
declare all the
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]: "];
scanf["%d%d", &number1, &number2];
prompt user for two integers and store into
// 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;
difference = number1 - number2;
product = number1 * number2;
quotient = number1 / number2;
remainder = number1 % number2;
carry out the arithmetic operations on
// 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, number2, sum, difference, product, quotient, remainder];
prints the results of the arithmetic operations, with the appropriate string descriptions in between.
++number1;
--number2;
illustrate the increment and decrement operations. Unlike
// 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
- Introduce one more
1 2 3 4 5 6 7 8 9 10
4 variable called// 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.c
34, and prompt user for its value. Print the sum and product of all the three integers. - In Mathematics, we could omit the multiplication sign in an arithmetic expression, 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.c
35. In programming, you need to explicitly provide all the operators, 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.c
36. Try printing the sum 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.c
37 times 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.c
08 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.c
39 times 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.c
09 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.c
41 time 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.c
34.
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 96Dissecting the Program
int sum = 0;
declares an
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: "];
scanf["%d", &upperbound];
prompt user for an upperbound to sum.
int number = 1;
while [number 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
- Modify the above program to sum all the number between a lowerbound and an upperbound provided by the user.
- Modify the above program to sum all the odd numbers 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.c
57 to an upperbound. [Hint: Use "// 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.c
58".] - Modify the above program to sum all the numbers 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.c
57 to an upperbound that are divisible 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.c
60. [Hint: Use "// 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.c
61"] - Modify the above program to find the sum of the square of all the numbers 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.c
57 to an upperbound, 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.c
63 - Modify the above program to compute the product of all the numbers 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.c
57 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.c
65. [Hint: Use a variable called1 2 3 4 5 6 7 8 9
25 instead ofint integer1, integer2, sum;
5 and initialize1 2 3 4 5 6 7 8 9
25 to 1. Ans:// 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.c
69.] Based on this code, write a program to display the factorial of n, where n is an integer 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.c
57 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.c
71.
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 99Dissecting the Program
int sumOdd = 0;
int sumEven = 0;
declare two
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] {
sumEven = sumEven + number;
} else {
sumOdd = sumOdd + number;
}
This is a conditional statement. The conditional statement can take one these forms: if-then or if-then-else.
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 Operators
There 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 Conditions
Suppose 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- Write a program to sum all the integers between 1 and 1000, that are divisible by 13, 15 or 17, but not by 30.
- Write a program to print all the leap years between AD1 and AD2010, and also print the number of leap years. [Hints: use a variable called
// 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!
15, which is initialized to zero. Increment 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!
15 whenever a leap year is found.]
Type double & Floating-Point Numbers
Recall 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 Casting
Although 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:
- Arithmetic operations [
// 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.c
22,// 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.c
23,// 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.c
24,// 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.c
25] of two1 2 3 4 5 6 7 8 9 10
4's produce an1 2 3 4 5 6 7 8 9 10
4; while arithmetic operations of two1 2 3 4 5 6 7 8 9
28's produce a1 2 3 4 5 6 7 8 9
28. Hence,// 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.c
21 [take note!] 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!
52. - Arithmetic operations of an
1 2 3 4 5 6 7 8 9 10
4 and a1 2 3 4 5 6 7 8 9
28 produce a1 2 3 4 5 6 7 8 9
28. Hence,// 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!
56 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!
57.
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.Example
Try 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
- Write a program called
// 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!
81 to compute the sum of a harmonic series// 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!
82, 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!
83. Your program shall prompt user for the value 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!
84. Keep the sum in a1 2 3 4 5 6 7 8 9
28 variable, and take note that// 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!
86 gives// 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.c
81 but// 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!
88 gives// 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!
89.
Try computing the sum for// 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!
90// 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!
91,// 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!
92,// 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!
93,// 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!
94,// 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!
95.
Hints: - Write a program called
// 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!
96 to compute the sum of a geometric series// 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!
97. You program shall prompt for the value 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!
84. [Hints: Use post-processing statement 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!
99.]
Summary
I 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.