Basics of C
Basics of C
Basics of C
html
1 /*
1 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
2 * Sum the odd and even numbers, respectively, from 1 to a given upperbound.
3 * Also compute the absolute difference.
4 * (SumOddEven.c)
5 */
6 #include <stdio.h> // Needed to use IO functions
7
8 int main() {
9 int sumOdd = 0; // For accumulating odd numbers, init to 0
10 int sumEven = 0; // For accumulating even numbers, init to 0
11 int upperbound; // Sum from 1 to this upperbound
12 int absDiff; // The absolute difference between the two sums
13
14 // Prompt user for an upperbound
15 printf("Enter the upperbound: ");
16 scanf("%d", &upperbound); // Use %d to read an int
17
18 // Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
19 int number = 1;
20 while (number <= upperbound) {
21 if (number % 2 == 0) { // Even number
22 sumEven += number; // Add number into sumEven
23 } else { // Odd number
24 sumOdd += number; // Add number into sumOdd
25 }
26 ++number; // increment number by 1
27 }
28
29 // Compute the absolute difference between the two sums
30 if (sumOdd > sumEven) {
31 absDiff = sumOdd - sumEven;
32 } else {
33 absDiff = sumEven - sumOdd;
34 }
35
36 // Print the results
37 printf("The sum of odd numbers is %d.\n", sumOdd);
38 printf("The sum of even numbers is %d.\n", sumEven);
39 printf("The absolute difference is %d.\n", absDiff);
40
41 return 0;
42 }
2.2 Comments
Comments are used to document and explain your codes and program logic. Comments are not programming statements and are ignored by the compiler, but they VERY
IMPORTANT for providing documenta�on and explana�on for others to understand your program (and also for yourself three days later).
You should use comments liberally to explain and document your codes. During program development, instead of dele�ng a chunk of statements permanently, you could
comment-out these statements so that you could get them back later, if needed.
For examples,
// Each of the following lines is a programming statement, which ends with a semi-colon (;)
int number1 = 10;
int number2, number3 = 99;
int product;
product = number1 * number2 * number3;
printf("Hello\n");
Block : A block (or a compound statement) is a group of statements surrounded by braces { }. All the statements inside the block is treated as one unit. Blocks are used as the
body in constructs like func�on, if-else and loop, which may contain mul�ple statements but are treated as one unit. There is no need to put a semi-colon a�er the closing brace to
end a complex statement. Empty block (without any statement) is permi�ed. For examples,
// Each of the followings is a "complex" statement comprising one or more blocks of statements.
// No terminating semi-colon needed after the closing brace to end the "complex" statement.
// Take note that a "complex" statement is usually written over a few lines for readability.
if (mark >= 50) {
printf("PASS\n");
printf("Well Done!\n");
printf("Keep it Up!\n");
2 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
if (number == 88) {
printf("Got it\n");
} else {
printf("Try Again\n");
}
i = 1;
while (i < 8) {
printf("%d\n", i);
++i;
}
int main() {
...statements...
}
You need to use a white space to separate two keywords or tokens, e.g.,
Addi�onal white spaces and extra lines are, however, ignored, e.g.,
// same as above
int sum
= 0 ;
double average ;
average = sum / 100.0;
Formatting Source Codes : As men�oned, extra white spaces are ignored and have no computa�onal significance. However, proper indenta�on (with tabs and blanks) and
extra empty lines greatly improves the readability of the program, which is extremely important for others (and yourself three days later) to understand your programs. For
example, the following hello-world works, but can you understand the program?
#include <stdio.h>
int main(){printf("Hello, world!\n");return 0;}
Braces : Place the beginning brace at the end of the line, and align the ending brace with the start of the statement.
Indentation : Indent the body of a block by an extra 3 (or 4 spaces), according to its level.
For example,
/*
* Recommended Programming style.
*/
#include <stdio.h>
// blank line to separate sections of codes
int main() { // Place the beginning brace at the end of the current line
// Indent the body by an extra 3 or 4 spaces for each level
return 0;
} // ending brace aligned with the start of the statement
Most IDEs (such as CodeBlocks, Eclipse and NetBeans) have a command to re-format your source code automa�cally.
Note: Tradi�onal C-style forma�ng places the beginning and ending braces on the same column. For example,
/*
* Traditional C-style.
*/
#include <stdio.h>
int main()
{
int mark = 70;
if (mark >= 50) // in level-1 block, indent once
{
printf("You Pass!\n"); // in level-2 block, indent twice
}
else
{
printf("You Fail!\n");
}
return 0;
3 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
A preprocessor direc�ve, which begins with a # sign (such as #include, #define), tells the preprocessor to perform a certain ac�on (such as including a header file, or
performing text replacement), before compiling the source code into object code. Preprocessor direc�ves are not programming statements, and therefore should NOT be
terminated with a semi-colon. For example,
In almost all of the C programs, we use #include <stdio.h> to include the input/output stream library header into our program, so as to use the IO library func�on to carry
out input/output opera�ons (such as printf() and scanf()).
3.1 Variables
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 loca�on, that stores a value of a par�cular data type. In other words, a variable has a name, a type and stores a value.
A variable has a name (or iden�fier), e.g., radius, area, age, height. The name is needed to uniquely iden�fy each variable, so as to assign a value to the variable (e.g.,
radius=1.2), and retrieve the value stored (e.g., area = radius*radius*3.1416).
A variable has a type. Examples of type are,
int: for integers (whole numbers) such as 123 and -456;
double: for floa�ng-point or real numbers such as 3.1416, -55.66, having a decimal point and frac�onal part.
A variable can store a value of that par�cular type. It is important to take note that a variable in most programming languages is associated with a type, and can only store
value of the par�cular type. For example, a int variable can store an integer value such as 123, but NOT real number such as 12.34, nor texts such as "Hello".
The concept of type was introduced into the early programming languages to simplify interpreta�on of data made up of 0s and 1s. The type determines the size and layout of
the data, the range of its values, and the set of opera�ons that can be applied.
The following diagram illustrates two types of variables: int and double. An int variable stores an integer (whole number). A double variable stores a real number.
3.2 Identifiers
An iden�fier is needed to name a variable (or any other en�ty such as a func�on or a class). C imposes the following rules on iden�fiers:
An iden�fier is a sequence of characters, of up to a certain length (compiler-dependent, typically 255 characters), comprising uppercase and lowercase le�ers (a-z, A-Z),
4 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Caution : Programmers don't use blank character in names. It is either not supported, or will pose you more challenges.
Recommendations
1. It is important to choose a name that is self-descrip�ve and closely reflects the meaning of the variable, e.g., numberOfStudents or numStudents.
2. Do not use meaningless names like a, b, c, d, i, j, k, i1, j99.
3. Avoid single-alphabet names, which is easier to type but o�en meaningless, unless they are common names like x, y, z for coordinates, i for index.
4. It is perfectly okay to use long names of says 30 characters to make sure that the name accurately reflects its meaning!
5. Use singular and plural nouns prudently to differen�ate between singular and plural variables. For example, you may use the variable row to refer to a single row number
and the variable rows to refer to many rows (such as an array of rows - to be discussed later).
Syntax Example
Example,
1 #include <stdio.h>
2
3 int main() {
4 int number; // Declared but not initialized
5 printf("%d\n", number); // Used before initialized
6 // No warning/error, BUT unexpected result
7 return 0;
8 }
Constant Naming Convention: Use uppercase words, joined with underscore. For example, MIN_VALUE, MAX_SIZE.
5 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
3.5 Expressions
An expression is a combina�on of operators (such as addi�on '+', subtrac�on '-', mul�plica�on '*', division '/') and operands (variables or literal values), that can be
evaluated to yield a single value of a certain type. For example,
1 + 2 * 3 // give int 7
The RHS shall be a value; and the LHS shall be a variable (or memory address).
Syntax Example
// Assign the literal value (of the RHS) to the variable (of the LHS)
variable = literal-value; number = 88;
// Evaluate the expression (RHS) and assign the result to the variable (LHS)
variable = expression; sum = sum + number;
The assignment statement should be interpreted this way: The expression on the right-hand-side (RHS) is first evaluated to produce a resultant value (called rvalue or right-value).
The rvalue is then assigned to the variable on the le�-hand-side (LHS) (or lvalue, which is a loca�on that can hold a rvalue). Take note that you have to first evaluate the RHS, before
assigning the resultant value to the LHS. For examples,
The symbol "=" is known as the assignment operator. The meaning of "=" in programming is different from Mathema�cs. It denotes assignment instead of equality. The RHS is a
literal value; or an expression that evaluates to a value; while the LHS must be a variable. Note that x = x + 1 is valid (and o�en used) in programming. It evaluates x + 1 and
assign the resultant value to the variable x. x = x + 1 illegal in Mathema�cs. While x + y = 1 is allowed in Mathema�cs, it is invalid in programming (because the LHS of an
assignment statement must be a variable). Some programming languages use symbol ":=", "←", "->", or "→" as the assignment operator to avoid confusion with equality.
Characters: Characters (e.g., 'a', 'Z', '0', '9') are encoded in ASCII into integers, and kept in type char. For example, character '0' is 48 (decimal) or 30H (hexadecimal);
character 'A' is 65 (decimal) or 41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal). Take note that the type char can be interpreted as character in ASCII
code, or an 8-bit integer. Unlike int or long, which is signed, char could be signed or unsigned, depending on the implementa�on. You can use signed char or
unsigned char to explicitly declare signed or unsigned char.
Floating-point Numbers: There are 3 floa�ng point types: float, double and long double, for single, double and long double precision floa�ng point numbers.
float and double are represented as specified by IEEE 754 standard. A float can represent a number between ±1.40239846×10^-45 and ±3.40282347×10^38,
approximated. A double can represented a number between ±4.94065645841246544×10^-324 and ±1.79769313486231570×10^308, approximated. Take note
that not all real numbers can be represented by float and double, because there are infinite real numbers. Most of the values are approximated.
The table below shows the typical size, minimum, maximum for the primi�ve types. Again, take note that the sizes are implementa�on dependent.
6 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
In addi�on, many C library func�ons use a type called size_t, which is equivalent (typedef) to a unsigned int, meant for coun�ng, size or length, with 0 and posi�ve
integers.
1 /*
2 * Print Size of Fundamental Types (SizeofTypes.cpp).
3 */
4 #include <stdio.h>
5
6 int main() {
7 printf("sizeof(char) is %d bytes.\n", sizeof(char));
8 printf("sizeof(short) is %d bytes.\n", sizeof(short));
9 printf("sizeof(int) is %d bytes.\n", sizeof(int));
10 printf("sizeof(long) is %d bytes.\n", sizeof(long));
11 printf("sizeof(long long) is %d bytes.\n", sizeof(long long));
12 printf("sizeof(float) is %d bytes.\n", sizeof(float));
13 printf("sizeof(double) is %d bytes.\n", sizeof(double));
14 printf("sizeof(long double) is %d bytes.\n", sizeof(long double));
15 return 0;
16 }
sizeof(char) is 1 bytes.
sizeof(short) is 2 bytes.
sizeof(int) is 4 bytes.
sizeof(long) is 4 bytes.
sizeof(long long) is 8 bytes.
sizeof(float) is 4 bytes.
sizeof(double) is 8 bytes.
sizeof(long double) is 12 bytes.
*Header <limits.h>
The limits.h header contains informa�on about limits of integer type. For example,
7 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
17
18 printf("Bits in char = %d\n", CHAR_BIT);
19 printf("char max = %d\n", CHAR_MAX);
20 printf("char min = %d\n", CHAR_MIN);
21 printf("signed char max = %d\n", SCHAR_MAX);
22 printf("signed char min = %d\n", SCHAR_MIN);
23 printf("unsigned char max = %u\n", UCHAR_MAX);
24 return 0;
25 }
The minimum of unsigned integer is always 0. The other constants are SHRT_MAX, SHRT_MIN, USHRT_MAX, LONG_MIN, LONG_MAX, ULONG_MAX. Try inspec�ng this header
(search for limits.h under your compiler).
*Header <float.h>
Similarly, the float.h header contain informa�on on limits for floa�ng point numbers, such as minimum number of significant digits (FLT_DIG, DBL_DIG, LDBL_DIG for
float, double and long double), number of bits for man�ssa (FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG), maximum and minimum exponent values, etc. Try
inspec�ng this header (search for cfloat under your compiler).
Choosing Types
As a programmer, you need to choose variables and decide on the type of the variables to be used in your programs. Most of the �mes, the decision is intui�ve. For example, use
an integer type for coun�ng and whole number; a floa�ng-point type for number with frac�onal part, char for a single character, and boolean for binary outcome.
Rule of Thumb
Use int for integer and double for floa�ng point numbers. Use byte, short, long and float only if you have a good reason to choose that specific precision.
Use int (or unsigned int) for coun�ng and indexing, NOT floa�ng-point type (float or double). This is because integer type are precise and more efficient in
opera�ons.
Use an integer type if possible. Use a floa�ng-point type only if the number contains a frac�onal part.
Read my ar�cle on "Data Representa�on" if you wish to understand how the numbers and characters are represented inside the computer memory. In brief, It is important to take
note that char '1' is different from int 1, short 1, float 1.0, double 1.0, and String "1". They are represented differently in the computer memory, with
different precision and interpreta�on. For example, short 1 is "00000000 00000001", int 1 is "00000000 00000000 00000000 00000001", long long 1 is
"00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001", float 1.0 is "0 01111111 0000000 00000000
00000000", double 1.0 is "0 01111111111 0000 00000000 00000000 00000000 00000000 00000000 00000000", char '1' is "00110001".
Furthermore, you MUST know the type of a value before you can interpret a value. For example, this value "00000000 00000000 00000000 00000001" cannot be
interpreted unless you know the type.
Many C compilers define a type called size_t, which is a typedef of unsigned int.
To print a string literal such as "Hello, world", simply place it inside the parentheses, as follow:
printf(aStringLiteral);
For example,
printf("Hello, world\n");
Hello, world
_
8 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
The \n represents the newline character. Prin�ng a newline advances the cursor (denoted by _ in the above example) to the beginning of next line. printf(), by default, places
the cursor a�er the printed string, and does not advance the cursor to the next line. For example,
printf("Hello");
printf(", ");
printf("world!");
printf("\n");
printf("Hello\nworld\nagain\n");
Hello, world!
Hello
world
again
_
The forma�ngString is a string composing of normal texts and conversion specifiers. Normal texts will be printed as they are. A conversion specifier begins with a percent sign (%),
followed by a code to specify the type of variable and format of the output (such as the field width and number of decimal places). For example, %d denotes an int; %3d for an
int with field-width of 3. The conversion specifiers are used as placeholders, which will be subs�tuted by the variables given a�er the forma�ng string in a sequen�al manner. For
example,
1 /*
2 * Test formatted printing for int (TestPrintfInt.c)
3 */
4 #include <stdio.h>
5
6 int main() {
7 int number1 = 12345, number2 = 678;
8 printf("Hello, number1 is %d.\n", number1); // 1 format specifier
9 printf("number1=%d, number2=%d.\n", number1, number2); // 2 format specifiers
10 printf("number1=%8d, number2=%5d.\n", number1, number2); // Set field-widths
11 printf("number1=%08d, number2=%05d.\n", number1, number2); // Pad with zero
12 printf("number1=%-8d, number2=%-5d.\n", number1, number2); // Left-align
13 return 0;
14 }
Notes:
For double, you must use %lf (for long float) in scanf() (or %le, %lE, %lg, %lG), but you can use either %f or %lf in printf() (or %e, %E, %g, %G, %le, %lE, %lg,
%lG).
Use %% to print a % in the forma�ng string.
For example,
9 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Field Width
You can op�onally specify a field-width before the type conversion code, e.g., %3d, %6f, %20s. If the value to be forma�ed is shorter than the field width, it will be padded with
spaces (by default). Otherwise, the field-width will be ignored. For example,
Alignment
The output are right-aligned by default. You could include a "-" flag (before the field width) to ask for le�-aligned. For example,
Others
+ (plus sign): display plus or minus sign preceding the number.
# or 0: Pad with leading # or 0.
C11's printf_s()/scanf_s()
C11 introduces more secure version of printf()/scanf() called printf_s()/scanf_s() to deal with mismatched conversion specifiers. Microso� Visual C
implemented its own versions of printf_s()/scanf_s() before C11, and issues a deprecated warning for using printf()/scanf().
1 /*
2 * TestScanf.c
3 */
4 #include <stdio.h>
5
6 int main() {
7 int anInt;
8 float aFloat;
9 double aDouble;
10
10 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Notes:
To place the input into a variable in scanf(), you need to prefix the variable name by an ampersand sign (&). The ampersand (&) is called address-of operator, which will be
explained later. However, it is important to stress that missing ampersand (&) is a common error.
For double, you must use type conversion code %lf for scanf(). You could use %f or %lf for printf().
For example,
The scanf() returns 1 if user enters an integer which is read into the variable number. It returns 0 if user enters a non-integer (such as "hello"), and variable number is not
assigned.
The scanf() returns 2 if user enters two integers that are read into number1 and number2. It returns 1 if user enters an integer followed by a non-integer, and number2 will
not be affected. It returns 0 if user enters a non-integer, and both number1 and number2 will not be affected.
Integer Literals
A whole number, such as 123 and -456, is treated as an int, by default. For example,
An int literal may precede with a plus (+) or minus (-) sign, followed by digits. No commas or special symbols (e.g., $ or space) is allowed (e.g., 1,234 and $123 are invalid). No
preceding 0 is allowed too (e.g., 007 is invalid).
Besides the default base 10 integers, you can use a prefix '0' (zero) to denote a value in octal, prefix '0x' for a value in hexadecimal, and prefix '0b' for binary value (in some
compilers), e.g.,
A long literal is iden�fied by a suffix 'L' or 'l' (avoid lowercase, which can be confused with the number one). A long long int is iden�fied by a suffix 'LL'. You can also
use suffix 'U' for unsigned int, 'UL' for unsigned long, and 'ULL' for unsigned long long int. For example,
No suffix is needed for short literals. But you can only use integer values in the permi�ed range. For example,
short smallNumber = 1234567890; // ERROR: this value is outside the range of short.
short midSizeNumber = -12345;
Floating-point Literals
A number with a decimal point, such as 55.66 and -33.44, is treated as a double, by default. You can also express them in scien�fic nota�on, e.g., 1.2e3, -5.5E-6, where
e or E denotes the exponent in power of 10. You could precede the frac�onal part or exponent with a plus (+) or minus (-) sign. Exponent shall be an integer. There should be no
space or other characters (e.g., space) in the number.
11 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
You MUST use a suffix of 'f' or 'F' for float literals, e.g., -1.2345F. For example,
float average = 55.66; // Error! RHS is a double. Need suffix 'f' for float.
float average = 55.66f;
For example,
Non-printable and control characters can be represented by so-called escape sequences, which begins with a back-slash (\) followed by a code. The commonly-used escape
sequences are:
Escape Hex
Description
Sequence (Decimal)
\n New-line (or Line-feed) 0AH (10D)
\r Carriage-return 0DH (13D)
\t Tab 09H (9D)
\" Double-quote (needed to include " in double-quoted 22H (34D)
string)
\' Single-quote 27H (39D)
\\ Back-slash (to resolve ambiguity) 5CH (92D)
Notes:
New-line (0AH) and carriage return (0dH), represented by \n, and \r respec�vely, are used as line delimiter (or end-of-line, or EOL). However, take note that UNIX/Linux/Mac
use \n as EOL, Windows use \r\n.
Horizontal Tab (09H) is represented as \t.
To resolve ambiguity, characters back-slash (\), single-quote (') and double-quote (") are represented using escape sequences \\, \' and \", respec�vely. This is because a
single back-slash begins an escape sequence, while single-quotes and double-quotes are used to enclose character and string.
Other less commonly-used escape sequences are: \? or ?, \a for alert or bell, \b for backspace, \f for form-feed, \v for ver�cal tab. These may not be supported in some
consoles.
String Literals
A String literal is composed of zero of more characters surrounded by a pair of double quotes, e.g., "Hello, world!", "The sum is ", "".
String literals may contains escape sequences. Inside a String, you need to use \" for double-quote to dis�nguish it from the ending double-quote, e.g. "\"quoted\"". Single
quote inside a String does not require escape sequence. For example,
TRY: Write a program to print the following picture. Take note that you need to use escape sequences to print special characters.
'__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" ""
Example (Literals)
1 /* Testing Primitive Types (TestLiteral.c) */
2 #include <stdio.h>
3
4 int main() {
5 char gender = 'm'; // char is single-quoted
6 unsigned short numChildren = 8; // [0, 255]
7 short yearOfBirth = 1945; // [-32767, 32768]
8 unsigned int salary = 88000; // [0, 4294967295]
9 double weight = 88.88; // With fractional part
12 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Gender is m.
Number of children is 8.
Year of birth is 1945.
Salary is 88000.
Weight is 88.88.
GPA is 3.88.
4. Operations
All the above operators are binary operators, i.e., they take two operands. The mul�plica�on, division and remainder take precedence over addi�on and subtrac�on. Within the
same precedence level (e.g., addi�on and subtrac�on), the expression is evaluated from le� to right. For example, 1+2+3-4 is evaluated as ((1+2)+3)-4.
It is important to take note that int/int produces an int, with the result truncated, e.g., 1/2 → 0 (instead of 0.5).
Take note that C does not have an exponent (power) operator ('^' is exclusive-or, not exponent).
must be wri�en as (1+2*a)/3 + (4*(b+c)*(5-d-e))/f - 6*(7/g+h). You cannot omit the mul�plica�on symbol '*' (as in Mathema�cs).
Like Mathema�cs, the mul�plica�on '*' and division '/' take precedence over addi�on '+' and subtrac�on '-'. Parentheses () have higher precedence. The operators '+',
'-', '*', and '/' are le�-associa�ve. That is, 1 + 2 + 3 + 4 is treated as (((1+2) + 3) + 4).
However, if the two operands belong to different types, the compiler promotes the value of the smaller type to the larger type (known as implicit type-cas�ng). The opera�on is
then carried out in the larger type. For example, int/double → double/double → double. Hence, 1/2 → 0, 1.0/2.0 → 0.5, 1.0/2 → 0.5, 1/2.0 →
0.5.
For example,
Example
1 /* Testing mix-type arithmetic operations (TestMixTypeOp.c) */
2 #include <stdio.h>
3
4 int main() {
5 int i1 = 2, i2 = 4;
6 double d1 = 2.5, d2 = 5.2;
7
13 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
4.4 Overflow/UnderFlow
Study the output of the following program:
In arithme�c opera�ons, the resultant value wraps around if it exceeds its range (i.e., overflow or underflow). C run�me does not issue an error/warning message but produces
incorrect result.
It is important to take note that checking of overflow/underflow is the programmer's responsibility, i.e., your job!
This feature is an legacy design, where processors were slow. Checking for overflow/underflow consumes computa�on power and reduces performance.
To check for arithme�c overflow (known as secure coding) is tedious. Google for "INT32-C. Ensure that opera�ons on signed integers do not result in overflow" @
www.securecoding.cert.org.
Example
1 /* Test on increment (++) and decrement (--) Operator (TestIncDec.cpp) */
2 #include <stdio.h>
3
4 int main() {
5 int mark = 76; // declare & assign
6 printf("%d\n", mark); // 76
7
8 mark++; // increase by 1 (post-increment)
9 printf("%d\n", mark); // 77
10
11 ++mark; // increase by 1 (pre-increment)
12 printf("%d\n", mark); // 78
13
14 mark = mark + 1; // also increase by 1 (or mark += 1)
15 printf("%d\n", mark); // 79
16
17 mark--; // decrease by 1 (post-decrement)
18 printf("%d\n", mark); // 78
19
14 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
The increment/decrement unary operator can be placed before the operand (prefix operator), or a�er the operands (pos�ix operator). They take on different meaning in
opera�ons.
If '++' or '--' involves another opera�on, then pre- or post-order is important to specify the order of the two opera�ons. For examples,
x = 5;
printf("%d\n", x++); // Save x (5); Increment x (=6); Print old x (5).
x = 5;
printf("%d\n", ++x); // Increment x (=6); Print x (6).
// This is confusing! Try to avoid! What is i=++i? What is i=i++?
Prefix operator (e.g, ++i) could be more efficient than pos�ix operator (e.g., i++) in some situa�ons.
1 /*
2 * Test implicit type casting (TestImplicitTypeCast.c)
3 */
4 #include <stdio.h>
5
6 int main() {
7 int i;
8 double d;
9
10 i = 3;
11 d = i; // Assign an int value to double
12 printf("d = %lf\n", d); // d = 3.0
13
14 d = 5.5;
15 i = d; // Assign a double value to int
16 printf("i = %d\n", i); // i = 5 (truncated, no warning!)
17
18 i = 6.6; // Assign a double literal to int
19 printf("i = %d\n", i); // i = 6 (truncated, no warning!)
20 }
C will not perform automa�c type conversion, if the two types are not compa�ble.
Explicit Type-Casting
You can explicitly perform type-cas�ng via the so-called unary type-cas�ng operator in the form of (new-type)operand. The type-cas�ng operator takes one operand in the
par�cular type, and returns an equivalent value in the new type. Take note that it is an opera�on that yields a resultant value, similar to an addi�on opera�on although addi�on
involves two operands. For example,
15 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Example: Suppose that you want to find the average (in double) of the integers between 1 and 100. Study the following codes:
1 /*
2 * Testing Explicit Type Cast (Average1to100.c).
3 */
4 #include <stdio.h>
5
6 int main() {
7 int sum = 0;
8 double average;
9 int number = 1;
10 while (number <= 100) {
11 sum += number; // Final sum is int 5050
12 ++number;
13 }
14 average = sum / 100; // Won't work (average = 50.0 instead of 50.5)
15 printf("Average is %lf\n", average); // Average is 50.0
16 return 0;
17 }
You don't get the frac�onal part although the average is a double. This is because both the sum and 100 are int. The result of division is an int, which is then implicitly
casted to double and assign to the double variable average. To get the correct answer, you can do either:
average = (double)sum / 100; // Cast sum from int to double before division
average = sum / (double)100; // Cast 100 from int to double before division
average = sum / 100.0;
average = (double)(sum / 100); // Won't work. why?
Example:
1 /*
2 * Converting between Celsius and Fahrenheit (ConvertTemperature.c)
3 * Celsius = (5/9)(FahrenheitɃ32)
4 * Fahrenheit = (9/5)Celsius+32
5 */
6 #include <stdio.h>
7
8 int main() {
9 double celsius, fahrenheit;
10
11 printf("Enter the temperature in celsius: ");
12 scanf("%lf", &celsius);
13 fahrenheit = celsius * 9 / 5 + 32;
14 // 9/5*celsius + 32 gives wrong answer! Why?
15 printf("%.2lf degree C is %.2lf degree F\n", celsius, fahrenheit);
16
17 printf("Enter the temperature in fahrenheit: ");
18 scanf("%lf", &fahrenheit);
19 celsius = (fahrenheit - 32) * 5 / 9;
20 // 5/9*(fahrenheit - 32) gives wrong answer! Why?
21 printf("%.2lf degree F is %.2lf degree C\n", fahrenheit, celsius);
22 return 0;
23 }
Each comparison opera�on involves two operands, e.g., x <= 100. It is invalid to write 1 < x < 100 in programming. Instead, you need to break out the two comparison
opera�ons x > 1, x < 100, and join with with a logical AND operator, i.e., (x > 1) && (x < 100), where && denotes AND operator.
16 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
false true
Example:
Exercise: Given the year, month (1-12), and day (1-31), write a boolean expression which returns true for dates before October 15, 1582 (Gregorian calendar cut over date).
Ans: (year < 1582) || (year == 1582 && month < 10) || (year == 1582 && month == 10 && day < 15)
5. Flow Control
There are three basic flow control constructs - sequen�al, condi�onal (or decision), and loop (or itera�on), as illustrated below.
17 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
"switch-case" is an alterna�ve to the "nested-if". In a switch-case statement, a break statement is needed for each of the cases. If break is missing, execu�on will flow through
the following case. You can use either an int or char variable as the case-selector.
Conditional Operator: A condi�onal operator is a ternary (3-operand) operator, in the form of booleanExpr ? trueExpr : falseExpr. Depending on the
booleanExpr, it evaluates and returns the value of trueExpr or falseExpr.
Syntax Example
Braces: You could omit the braces { }, if there is only one statement inside the block. For example,
However, I recommend that you keep the braces, even though there is only one statement in the block, to improve the readability of your program.
Exercises
[TODO]
18 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
} sum += number;
}
The difference between while-do and do-while lies in the order of the body and condi�on. In while-do, the condi�on is tested first. The body will be executed if the condi�on is true
and the process repeats. In do-while, the body is executed and then the condi�on is tested. Take note that the body of do-while will be executed at least once (vs. possibly zero for
while-do).
Suppose that your program prompts user for a number between 1 to 10, and checks for valid input, do-while with a boolean flag could be more appropriate.
// Game loop
bool gameOver = false;
while (!gameOver) {
// play the game
......
// Update the game state
// Set gameOver to true if appropriate to exit the game loop
......
}
Example (Counter-Controlled Loop): Prompt user for an upperbound. Sum the integers from 1 to a given upperbound and compute its average.
1 /*
2 * Sum from 1 to a given upperbound and compute their average (SumNumbers.c)
3 */
4 #include <stdio.h>
5
6 int main() {
7 int sum = 0; // Store the accumulated sum
8 int upperbound;
9
10 printf("Enter the upperbound: ");
11 scanf("%d", &upperbound);
12
13 // Sum from 1 to the upperbound
14 int number;
15 for (number = 1; number <= upperbound; ++number) {
16 sum += number;
17 }
18 printf("Sum is %d\n", sum);
19 printf("Average is %.2lf\n", (double)sum / upperbound);
20
21 // Sum only the odd numbers
19 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Example (Sentinel-Controlled Loop): Prompt user for posi�ve integers, and display the count, maximum, minimum and average. Terminate when user enters -1.
1 /* Prompt user for positive integers and display the count, maximum,
2 minimum and average. Terminate the input with -1 (StatNumbers.c) */
3 #include <stdio.h>
4 #include <limits.h> // for INT_MAX
5
6 int main() {
7 int numberIn = 0; // input number (positive integer)
8 int count = 0; // count of inputs, init to 0
9 int sum = 0; // sum of inputs, init to 0
10 int max = 0; // max of inputs, init to minimum
11 int min = INT_MAX; // min of inputs, init to maximum (need <climits>)
12 int sentinel = -1; // Input terminating value
13
14 // Read Inputs until sentinel encountered
15 printf("Enter a positive integer or %d to exit: ", sentinel);
16 scanf("%d", &numberIn);
17 while (numberIn != sentinel) {
18 // Check input for positive integer
19 if (numberIn > 0) {
20 ++count;
21 sum += numberIn;
22 if (max < numberIn) max = numberIn;
23 if (min > numberIn) min = numberIn;
24 } else {
25 printf("error: input must be positive! try again...\n");
26 }
27 printf("Enter a positive integer or %d to exit: ", sentinel);
28 scanf("%d", &numberIn);
29 }
30
31 // Print result
32 printf("\n");
33 printf("Count is %d\n", count);
34 if (count > 0) {
35 printf("Maximum is %d\n", max);
36 printf("Minimum is %d\n", min);
37 printf("Average is %.2lf\n", (double)sum / count);
38 }
39 }
Program Notes
In compu�ng, a sen�nel value is a special value that indicates the end of data (e.g., a nega�ve value to end a sequence of posi�ve value, end-of-file, null character in the null-
terminated string). In this example, we use -1 as the sen�nel value to indicate the end of inputs, which is a sequence of posi�ve integers. Instead of hardcoding the value of -1,
we use a variable called sentinel for flexibility and ease-of-maintenance.
Take note of the while-loop pa�ern in reading the inputs. In this pa�ern, you need to repeat the promp�ng and input statement.
Exercises
[TODO]
The continue statement aborts the current itera�on and con�nue to the next itera�on of the current (innermost) loop.
break and continue are poor structures as they are hard to read and hard to follow. Use them only if absolutely necessary. You can always write the same program without
using break and continue.
Example (break): The following program lists the non-prime numbers between 2 and an upperbound.
1 /*
2 * List non-prime from 1 to an upperbound (NonPrimeList.c).
3 */
4 #include <stdio.h>
5 #include <math.h>
6
7 int main() {
8 int upperbound, number, maxFactor, factor;
9 printf("Enter the upperbound: ");
10 scanf("%d", &upperbound);
11 for (number = 2; number <= upperbound; ++number) {
12 // Not a prime, if there is a factor between 2 and sqrt(number)
20 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
13 maxFactor = (int)sqrt(number);
14 for (factor = 2; factor <= maxFactor; ++factor) {
15 if (number % factor == 0) { // Factor?
16 printf("%d ", number);
17 break; // A factor found, no need to search for more factors
18 }
19 }
20 }
21 printf("\n");
22 return 0;
23 }
Let's rewrite the above program without using break statement. A while loop is used (which is controlled by the boolean flag) instead of for loop with break.
1 /*
2 * List primes from 1 to an upperbound (PrimeList.c).
3 */
4 #include <stdio.h>
5 #include <math.h>
6
7 int main() {
8 int upperbound, number, maxFactor, isPrime, factor;
9 printf("Enter the upperbound: ");
10 scanf("%d", &upperbound);
11
12 for (number = 2; number <= upperbound; ++number) {
13 // Not prime, if there is a factor between 2 and sqrt of number
14 maxFactor = (int)sqrt(number);
15 isPrime = 1;
16 factor = 2;
17 while (isPrime && factor <= maxFactor) {
18 if (number % factor == 0) { // Factor of number?
19 isPrime = 0;
20 }
21 ++factor;
22 }
23 if (isPrime) printf("%d ", number);
24 }
25 printf("\n");
26 return 0;
27 }
Example (continue):
exit(): You could invoke the func�on exit(int exitCode), in <stdlib.h>, to terminate the program and return the control to the Opera�ng System. By conven�on,
return code of zero indicates normal termina�on; while a non-zero exitCode (-1) indicates abnormal termina�on. For example,
21 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
abort(): The header <stdlib.h> also provide a func�on called abort(), which can be used to terminate the program abnormally.
The "return" Statement: You could also use a "return returnValue" statement in the main() func�on to terminate the program and return control back to the
Opera�ng System. For example,
int main() {
...
if (errorCount > 10) {
printf("too many errors\n");
return -1; // Terminate and return control to OS from main()
}
...
}
Try out the following program, which prints a 8-by-8 checker box pa�ern using nested loops, as follows:
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
1 /*
2 * Print square pattern (PrintSquarePattern.c).
3 */
4 #include <stdio.h>
5
6 int main() {
7 int size = 8, row, col;
8 for (row = 1; row <= size; ++row) { // Outer loop to print all the rows
9 for (col = 1; col <= size; ++col) { // Inner loop to print all the columns of each row
10 printf("# ");
11 }
12 printf("\n"); // A row ended, bring the cursor to the next line
13 }
14
15 return 0;
16 }
This program contains two nested for-loops. The inner loop is used to print a row of eight "# ", which is followed by prin�ng a newline. The outer loop repeats the inner loop to
print all the rows.
Suppose that you want to print this pa�ern instead (in program called PrintCheckerPattern.cpp):
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
22 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
You need to print an addi�onal space for even-number rows. You could do so by adding the following statement before Line 8.
Exercises
1. Print these pa�erns using nested loop (in a program called PrintPattern1x). Use a variable called size for the size of the pa�ern and try out various sizes. You should
use as few printf() statements as possible.
# * # * # * # * # # # # # # # # # # # # # # # # 1 1
# * # * # * # * # # # # # # # # # # # # # # 2 1 1 2
# * # * # * # * # # # # # # # # # # # # 3 2 1 1 2 3
# * # * # * # * # # # # # # # # # # 4 3 2 1 1 2 3 4
# * # * # * # * # # # # # # # # 5 4 3 2 1 1 2 3 4 5
# * # * # * # * # # # # # # 6 5 4 3 2 1 1 2 3 4 5 6
# * # * # * # * # # # # 7 6 5 4 3 2 1 1 2 3 4 5 6 7
# * # * # * # * # # 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8
(a) (b) (c) (d) (e)
Hints:
The equa�ons for major and opposite diagonals are row = col and row + col = size + 1. Decide on what to print above and below the diagonal.
2. Print the �metable of 1 to 9, as follows, using nested loop. (Hints: you need to use an if-else statement to check whether the product is single-digit or double-digit, and print
an addi�onal space if needed.)
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
......
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
(a) (b) (c) (d) (e)
if (i == 0)
if (j == 0)
printf("i and j are zero\n");
else printf("i is not zero\n"); // intend for the outer-if
The else clause in the above codes is syntac�cally applicable to both the outer-if and the inner-if. The C compiler always associate the else clause with the innermost if (i.e., the
nearest if). Dangling else can be resolved by applying explicit parentheses. The above codes are logically incorrect and require explicit parentheses as shown below.
if ( i == 0) {
if (j == 0) printf("i and j are zero\n");
} else {
printf("i is not zero\n"); // non-ambiguous for outer-if
}
is commonly used. It seems to be an endless loop (or infinite loop), but it is usually terminated via a break or return statement inside the loop body. This kind of code is hard to
read - avoid if possible by re-wri�ng the condi�on.
5.8 Exercises
[TODO]
23 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Use "mono-space" fonts (such as Consola, Courier New, Courier) for wri�ng/displaying your program.
Programming Errors
There are generally three classes of programming errors:
1. Compila�on Error (or Syntax Error): can be fixed easily.
2. Run�me Error: program halts pre-maturely without producing the results - can also be fixed easily.
3. Logical Error: program completes but produces incorrect results. It is easy to detect if the program always produces wrong result. It is extremely hard to fix if the program
produces the correct result most of the �mes, but incorrect result some�mes. For example,
This kind of errors is very serious if it is not caught before produc�on. Wri�ng good programs helps in minimizing and detec�ng these errors. A good tes�ng strategy is
needed to ascertain the correctness of the program. So�ware tes�ng is an advanced topics which is beyond our current scope.
Debugging Programs
Here are the common debugging techniques:
1. Stare at the screen! Unfortunately, errors usually won't pop-up even if you stare at it extremely hard.
2. Study the error messages! Do not close the console when error occurs and pretending that everything is fine. This helps most of the �mes.
3. Insert print statements at appropriate loca�ons to display the intermediate results. It works for simple toy program, but it is neither effec�ve nor efficient for complex
program.
4. Use a graphic debugger. This is the most effec�ve means. Trace program execu�on step-by-step and watch the value of variables and outputs.
5. Advanced tools such as profiler (needed for checking memory leak and func�on usage).
6. Proper program tes�ng to wipe out the logical errors.
7. Arrays
An array is a list of elements of the same type, iden�fied by a pair of square brackets [ ]. To use an array, you need to declare the array with 3 things: a name, a type and a
dimension (or size, or length). The syntax is:
type arrayName[arraylength];
I recommend using a plural name for array, e.g., marks, rows, numbers. For example,
int size;
printf("Enter the length of the array: ");
scanf("%d", size);
float values[size];
Take note that, in C, the value of the elements are undefined a�er declara�on.
You can also ini�alize the array during declara�on with a comma-separated list of values, as follows:
24 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
You can refer to an element of an array via an index (or subscript) enclosed within the square bracket [ ]. C's array index begins with zero. For example, suppose that marks is an
int array of 5 elements, then the 5 elements are: marks[0], marks[1], marks[2], marks[3], and marks[4].
To create an array, you need to known the length (or size) of the array in advance, and allocate accordingly. Once an array is created, its length is fixed and cannot be changed. At
�mes, it is hard to ascertain the length of an array (e.g., how many students in a class?). Nonetheless, you need to es�mate the length and allocate an upper bound. This is
probably the major drawback of using an array.
You can find the array length using expression sizeof(arrayName)/sizeof(arrayName[0]), where sizeof(arrayName) returns the total bytes of the array and
sizeof(arrayName[0]) returns the bytes of first element.
C does not perform array index-bound check. In other words, if the index is beyond the array's bounds, it does not issue a warning/error. For example,
This is another pi�all of C. Checking the index bound consumes computa�on power and depicts the performance. However, it is be�er to be safe than fast. Newer programming
languages such as Java/C# performs array index bound check.
25 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
1 /*
2 * Find the mean and standard deviation of numbers kept in an array (MeanStdArray.c).
3 */
4 #include <stdio.h>
5 #include <math.h>
6 #define SIZE 7
7
8 int main() {
9 int marks[] = {74, 43, 58, 60, 90, 64, 70};
10 int sum = 0;
11 int sumSq = 0;
12 double mean, stdDev;
13 int i;
14 for (i = 0; i < SIZE; ++i) {
15 sum += marks[i];
16 sumSq += marks[i] * marks[i];
17 }
18 mean = (double)sum/SIZE;
19 printf("Mean is %.2lf\n", mean);
20
21 stdDev = sqrt((double)sumSq/SIZE - mean*mean);
22 printf("Std dev is %.2lf\n", stdDev);
23
24 return 0;
25 }
Exercises
[TODO]
For 2D array (table), the first index is the row number, second index is the column number. The elements are stored in a so-called row-major manner, where the column index runs
out first.
Example
1 /* Test Multi-dimensional Array (Test2DArray.c) */
2 #include <stdio.h>
3 void printArray(const int[][3], int);
4
5 int main() {
6 int myArray[][3] = {{8, 2, 4}, {7, 5, 2}}; // 2x3 initialized
7 // Only the first index can be omitted and implied
8 printArray(myArray, 2);
9 return 0;
10 }
11
12 // Print the contents of rows-by-3 array (columns is fixed)
13 void printArray(const int array[][3], int rows) {
14 int i, j;
15 for (i = 0; i < rows; ++i) {
16 for (j = 0; j < 3; ++j) {
17 printf("%d ", array[i][j]);
18 }
19 printf("\n");
20 }
21 }
8. Functions
26 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Two par�es are involved in using a func�on: a caller who calls the func�on, and the func�on called. The caller passes argument(s) to the func�on. The func�on receives these
argument(s), performs the programmed opera�ons within the func�on's body, and returns a piece of result back to the caller.
area 1 is 3.63
area 2 is 14.52
area 3 is 32.67
In the above example, a reusable func�on called getArea() is defined, which receives a parameter (in double) from the caller, performs the calcula�on, and return a piece of
result (in double) to the caller. In the main(), we invoke getArea() func�ons thrice, each �me with a different parameter.
In C, you need to declare a func�on prototype (before the func�on is used), and provide a func�on defini�on, with a body containing the programmed opera�ons.
Function Definition
The syntax for func�on defini�on is as follows:
The parameterList consists of comma-separated parameter-type and parameter-name, i.e., param-1-type param-1-name, param-2-type param-2-name,...
The returnValueType specifies the type of the return value, such as int or double. An special return type called void can be used to denote that the func�on returns no
value. In C, a func�on is allowed to return one value or no value (void). It cannot return mul�ple values. [C does not allow you to return an array!]
27 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Take note that invoking a func�on (by the caller) transfers the control to the func�on. The return statement in the func�on transfers the control back to the caller.
Function Prototype
In C, a func�on must be declared before it can be called. It can be achieved by either placing the func�on defini�on before it is being used, or declare a so-called func�on
prototype.
A func�on prototype tells the compiler the func�on's interface, i.e., the return-type, func�on name, and the parameter type list (the number and type of parameters). The func�on
can now be defined anywhere in the file. For example,
You could op�onally include the parameter names in the func�on prototype. The names will be ignored by the compiler, but serve as documenta�on. For example,
// Function Prototype
double getArea(double radius); // parameter names are ignored, but serve as documentation
int max(int number1, int number2);
Func�on prototypes are usually grouped together and placed in a so-called header file. The header file can be included in many programs. We will discuss header file later.
Another Example
We have a func�on called max(int, int), which takes two int and return their maximum. We invoke the max() func�on from the main().
In the above example, the variable (double radius) declared in the signature of getArea(double radius) is known as formal parameter. Its scope is within the
func�on's body. When the func�on is invoked by a caller, the caller must supply so-called actual parameters (or arguments), whose value is then used for the actual computa�on.
For example, when the func�on is invoked via "area1 = getArea(radius1)", radius1 is the actual parameter, with a value of 1.1.
Boolean Functions
A boolean func�on returns a int value of either 0 or not 0 to the caller.
Suppose that we wish to write a func�on called isOdd() to check if a given number is odd.
1 /*
2 * Test Boolean function (BooleanfunctionTest.c).
3 */
4 #include <stdio.h>
5
6 // Function Prototype
7 int isOdd(int);
28 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
8
9 int main() {
10 printf("%d\n", isOdd(5)); // 1 (true)
11 printf("%d\n", isOdd(6)); // 0 (false)
12 printf("%d\n", isOdd(-5)); // 0 (false)
13 }
14
15 int isOdd(int number) {
16 if (number % 2 == 1) {
17 return 1;
18 } else {
19 return 0;
20 }
21 }
This seemingly correct codes produces false for -5, because -5%2 is -1 instead of 1. You may rewrite the condi�on:
The above code produces the correct answer, but is poor. For boolean func�on, you should simply return the resultant value of the comparison, instead of using a condi�onal
statement, as follow:
int main() {
int number = -9;
if (isEven(number)) { // Don't write (isEven(number) != 0)
printf("Even\n");
}
if (isOdd(number)) { // Don't write (isOdd(number) != 0)
printf("Odd\n");
}
}
For example,
29 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
33 printf(",");
34 }
35 }
36 printf("}\n");
37 }
Pass-by-Value
In pass-by-value, a "copy" of argument is created and passed into the func�on. The invoked func�on works on the "clone", and cannot modify the original copy. In C, fundamental
types (such as int and double) are passed by value. That is, you cannot modify caller's value inside the func�on - there is no side effect.
Pass-by-Reference
On the other hand, in pass-by-reference, a reference of the caller's variable is passed into the func�on. In other words, the invoked func�on works on the same data. If the invoked
func�on modifies the parameter, the same caller's copy will be modified as well.
In C, arrays are passed by reference. That is, you can modify the contents of the caller's array inside the invoked func�on - there could be side effect in passing arrays into func�on.
C does not allow func�ons to return an array. Hence, if you wish to write a func�on that modifies the contents of an array (e.g., sor�ng the elements of an array), you need to rely
on pass-by-reference to work on the same copy inside and outside the func�on. Recall that in pass-by-value, the invoked func�on works on a clone copy and has no way to modify
the original copy.
30 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
35 printf("%d", array[i]);
36 if (i < size - 1) {
37 printf(",");
38 }
39 }
40 printf("}\n");
41 }
Array is passed into func�on by reference. That is, the invoked func�on works on the same copy of the array as the caller. Hence, changes of array inside the func�on is reflected
outside the func�on (i.e., side effect).
Use const whenever possible for passing references as it prevents you from inadvertently modifying the parameters and protects you against many programming errors.
1 /* Search an array for the given key using Linear Search (LinearSearch.c) */
2 #include <stdio.h>
3
4 int linearSearch(const int a[], int size, int key);
5
6 int main() {
7 const int SIZE = 8;
8 int a1[] = {8, 4, 5, 3, 2, 9, 4, 1};
9
10 printf("%d\n", linearSearch(a1, SIZE, 8)); // 0
11 printf("%d\n", linearSearch(a1, SIZE, 4)); // 1
12 printf("%d\n", linearSearch(a1, SIZE, 99)); // 8 (not found)
13 }
14
15 // Search the array for the given key
16 // If found, return array index [0, size-1]; otherwise, return size
17 int linearSearch(const int a[], int size, int key) {
18 int i;
19 for (i = 0; i < size; ++i) {
20 if (a[i] == key) return i;
21 }
22 return size;
23 }
Program Notes:
[TODO]
{8,4,5,3,2,9,4,1}
PASS 1 ...
{8,4,5,3,2,9,4,1} => {4,8,5,3,2,9,4,1}
{4,8,5,3,2,9,4,1} => {4,5,8,3,2,9,4,1}
{4,5,8,3,2,9,4,1} => {4,5,3,8,2,9,4,1}
{4,5,3,8,2,9,4,1} => {4,5,3,2,8,9,4,1}
{4,5,3,2,8,9,4,1} => {4,5,3,2,8,4,9,1}
{4,5,3,2,8,4,9,1} => {4,5,3,2,8,4,1,9}
PASS 2 ...
{4,5,3,2,8,4,1,9} => {4,3,5,2,8,4,1,9}
{4,3,5,2,8,4,1,9} => {4,3,2,5,8,4,1,9}
{4,3,2,5,8,4,1,9} => {4,3,2,5,4,8,1,9}
{4,3,2,5,4,8,1,9} => {4,3,2,5,4,1,8,9}
PASS 3 ...
{4,3,2,5,4,1,8,9} => {3,4,2,5,4,1,8,9}
{3,4,2,5,4,1,8,9} => {3,2,4,5,4,1,8,9}
{3,2,4,5,4,1,8,9} => {3,2,4,4,5,1,8,9}
{3,2,4,4,5,1,8,9} => {3,2,4,4,1,5,8,9}
PASS 4 ...
{3,2,4,4,1,5,8,9} => {2,3,4,4,1,5,8,9}
{2,3,4,4,1,5,8,9} => {2,3,4,1,4,5,8,9}
PASS 5 ...
{2,3,4,1,4,5,8,9} => {2,3,1,4,4,5,8,9}
PASS 6 ...
{2,3,1,4,4,5,8,9} => {2,1,3,4,4,5,8,9}
PASS 7 ...
{2,1,3,4,4,5,8,9} => {1,2,3,4,4,5,8,9}
PASS 8 ...
31 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
{1,2,3,4,4,5,8,9}
Program Notes:
[TODO]
{8,4,5,3,2,9,4,1}
{8} {4,5,3,2,9,4,1}
{4,8} {5,3,2,9,4,1}
{4,5,8} {3,2,9,4,1}
{3,4,5,8} {2,9,4,1}
{2,3,4,5,8} {9,4,1}
{2,3,4,5,8,9} {4,1}
{2,3,4,4,5,8,9} {1}
{1,2,3,4,4,5,8,9}
32 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Program Notes:
[TODO]
{8,4,5,3,2,9,4,1}
{} {8,4,5,3,2,9,4,1} => {} {1,4,5,3,2,9,4,8}
{1} {4,5,3,2,9,4,8} => {1} {2,5,3,4,9,4,8}
{1,2} {5,3,4,9,4,8} => {1,2} {3,5,4,9,4,8}
{1,2,3} {5,4,9,4,8} => {1,2,3} {4,5,9,4,8}
{1,2,3,4} {5,9,4,8} => {1,2,3,4} {4,9,5,8}
{1,2,3,4,4} {9,5,8} => {1,2,3,4,4} {5,9,8}
{1,2,3,4,4,5} {9,8} => {1,2,3,4,4,5} {8,9}
{1,2,3,4,4,5,8,9}
33 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Program Notes:
[TODO]
atan2(y, x):
Return arc-tan of y/x. Better than atan(x) for handling 90 degree.
ceil(x), floor(x):
returns the ceiling and floor integer of floating point number.
rand() generates the same squence of pseudo-random numbers on different invoca�ons. The stblib.h also provides a srand() func�on to seed or ini�alize the random
number generator. We typically seed it with the current �me obtained via time(0) func�on (in <time.h> header), which returns the number of seconds since January 1st,
1970.
34 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
2 #include <stdio.h>
3 #include <stdlib.h> // for rand(), srand()
4 #include <time.h> // for time()
5
6 int main() {
7 // rand() generate a random number in [0, RAND_MAX]
8 printf("RAND_MAX is %d\n", RAND_MAX); // 32767
9
10 // Generate 10 pseudo-random numbers between 0 and 99
11 // without seeding the generator.
12 // You will get the same sequence, every time you run this program
13 int i;
14 for (i = 0; i < 10; ++i) {
15 printf("%d ", rand() % 100); // need <stdlib.h> header
16 }
17 printf("\n");
18
19 // Seed the random number generator with current time
20 srand(time(0)); // need <cstdlib> and <ctime> header
21 // Generate 10 pseudo-random numbers
22 // You will get different sequence on different run,
23 // because the current time is different
24 for (i = 0; i < 10; ++i) {
25 printf("%d ", rand() % 100); // need <stdlib.h> header
26 }
27 printf("\n");
28 }
1: 333109 (16.66%)
2: 333113 (16.66%)
3: 333181 (16.66%)
4: 333562 (16.68%)
5: 333601 (16.68%)
6: 333434 (16.67%)
As seen from the output, rand() is fairly uniformly-distributed over [0, RAND_MAX].
8.8 Exercises
[TODO]
Clearly, the length of array is the length of string plus 1, to account for the termina�ng null character '\0'.
You can use scanf() to input a string, and printf() to print a string, with %s conversion specifier. For example,
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char message[256];
35 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Take note that you need to allocate a char array that is big enough to hold the input string including the termina�ng null character '\0'.
int isupper(int c); [A-Z] Check if uppercase/lowercase and return true (non-zero) or false (0)
int islower(int c); [a-z]
int toupper(int c); To Uppercase Return the uppercase/lowercase character, if c is a lowercase/uppercase character; otherwise, return c.
int tolower(int c); To Lowercase
Example: [TODO]
Function Description
int atoi(const char * str); String to int Convert the str to
double atof(const char * str); String to double int/double/long/long long.
long atol(const char * str); String to long
long long atoll(const char * str); String to long long
double strtod(const char* str, char** endptr); String to double Convert the str to double/float.
float strtof(const char* str, char** endptr); String to float If endptr is not a null pointer,
it will be set to point to the first character a�er the
number.
long strtol(const char* str, char** endptr, int base); String to long Convert the str to long/unsigned long.
unsigned long strtoul(const char* str, char** endptr, int String to unsigned
base); long
Example: [TODO]
36 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
int strlen(const char* str); String Length Return the length of str
(excluding termina�ng null char)
char* strchr(const char* str, int c); Search string for char Return a pointer to the first/last occurrence
char* strrchr(const char* str, int c); Search string for char reverse of c in str
if present. Otherwise, return NULL.
char* strpbrk(const char* str, const char* pattern); Search string for char in pa�ern Locate the first occurrence in str
of any character in pattern.
char* strstr(const char* str, const char* substr); Search string for sub-string Return a pointer to the first occurrence
of substr in str
if present. Otherwise, return NULL.
char* strspn(const char* str, const char* substr); Search string for span of substr
char* strcspn(const char* str, const char* substr); Search string for complement span of substr
void* memcpy(void *dest, const void *src, size_t n); Memory block copy
void* memmove(void *dest, const void *src, size_t n); Memory block move
int memcmp(const void *p1, const void *p2, size_t n); Memory block compare
void* memchr(void *ptr, int value, size_t n); Search memory block for char
void* memset(void *ptr, int value, size_t n); Memory block set (fill)
Example: [TODO]
int getc(FILE *stream); Get character (from FILE stream) Input/Output a character
int putc(int c, FILE *stream); Put character (to FILE stream) from FILE stream.
int ungetc(int c, FILE *stream); Un-get character (to FILE stream)
int sprintf(char *str, const char *format, ...); Forma�ed print (to string) Forma�ed string input/output.
int sscanf(char *str, const char *format, ....); Forma�ed scan (from string) Similar to printf() and scanf(),
except that the output/input comes from the str.
Example: [TODO]
size_t fread(void *ptr size_t size, size_t count, FILE *stream) File read Direct Access
size_t fwrite(const void *ptr, size_t size, size_t count, FILE File write
*stream); Get file posi�on
int fgetpos(FILE *stream, fpos_t *pos); Set file posi�on
int fsetpos(FILE *stream, const fpos_t *pos); File seek
Tell file
int fseek(FILE *stream, long offset, int origin);
long ftell(FILE *stream);
void rewind(FILE *stream); Rewind file Set the file posi�on to the beginning
Open/Close File
37 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Mode Description
"r" Read Open file for reading. The file shall exist.
"w" Write Open file for wri�ng. If the file does not exist, create a new file; otherwise, discard exis�ng contents.
"a" Append Open file for wri�ng. If the file does not exist, create a new file; otherwise, append to the exis�ng file.
"r+" Read/Write Open file for reading/wri�ng. The file shall exist.
"w+" Read/Write Open file for reading/wri�ng. If the file does not exist, create a new file; otherwise, discard exis�ng contents.
"a+" Read/Append Open file for reading/wri�ng. If the file does not exist, create a new file; otherwise, append to the exis�ng file.
File Stream
You can use stdin, stdout, stderr to denote standard input stream (keyboard), standard output stream (console) and standard error stream (console).
Example 2
1 #include <stdio.h>
2 #define SIZE 80 // size of string buffer
3
4 int main() {
5 FILE * pFile;
6 char buffer[SIZE];
7
8 pFile = fopen("test.txt" , "r");
9 if (pFile == NULL) {
10 perror("Error opening file");
11 } else {
12 while (!feof(pFile)) {
13 if (fgets(buffer, SIZE, pFile) == NULL) break;
14 fputs (buffer , stdout);
15 }
16 fclose(pFile);
17 }
18 return 0;
19 }
We can declare and ini�alize a string via char pointer; and operate the string via char pointer.
38 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
1 #include <stdio.h>
2
3 int main() {
4 char *msg = "hello"; // Append a terminating null character '\0'
5 char *p;
6
7 for (p = msg; *p != '\0'; ++p) {
8 printf("'%c' ", *p);
9 }
10 printf("\n");
11 }
[TODO]
13. Miscellaneous
<math.h>: contains func�on prototypes for mathema�cal func�ons (e.g., pow(), sqrt()).
<ctype.h>: (character type) contains func�on prototypes for tes�ng character proper�es (isupper(), isalpha(), isspace()) and case conversion (toupper(),
tolower()).
13.3 Keywords
ISO C90 (ANSI C 89) has 32 keywords:
Type: int, double, long, char, float, short, unsigned, signed, typedef, sizeof (10).
Control: if, else, switch, case, break, default, for, do, while, continue, goto (11).
Func�on: return, void (2)
Data Structure: struct, enum, union (3)
Memory: auto, register, extern, const, volatile, static (6).
39 of 40 22/03/2020, 19:54
C Basics - C Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html
Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan (ehchua@ntu.edu.sg) | HOME
40 of 40 22/03/2020, 19:54