Chapter 3: Number Systems, Scalar Types, and Input and Output
Chapter 3: Number Systems, Scalar Types, and Input and Output
For example,
binary number decimal number
0 0
1 1
10 2
11 3
100 4
101 5
110 6
1100 1010 202
Decimal Numbers
• Decimal
The digits' weight increases by powers of 10. The weighted values for
each position is determined as follows:
For example,
A decimal number 4261 can be thought of as follows.
4 * 1000 + 2 * 100 + 6 * 10 + 1 * 1
= 4000 + 200 + 60 + 1
= 4261 (decimal)
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
For example,
binary 10 is decimal 2.
the binary value 1100 1010 represents the decimal value 202.
1 * 128 + 1 * 64 + 0 * 32 + 0 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1
= 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0
= 202 (decimal)
Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.
C for Engineers and Scientists
• Octal
The octal system is based on the binary system with a 3-bit boundary. The octal
number system uses base 8 includes 0 through 7. The weighted values for each
position is as follows:
83 82 81 80
512 64 8 1
For example, the binary value 1 010 000 111 101 110 100 011 equals to
octal value (12075643)8.
To convert octal number to decimal number, multiply the value in each position
by its octal weight and add each value together. For example, the octal value
(167)8 represents decimal value 119.
• Hexadecimal
Similar to octal, the hexadecimal system is also based on the binary system but
using 4-bit boundary. The hexadecimal number system uses base 16 including
the digits 0 through 9 and the letters A, B, C, D, E, and F. The letters A through F
represent the decimal numbers 10 through 15. For the decimal values from 0 to 15,
the corresponding hexadecimal values are listed below.
Decimal 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Hexadecimal F E D C B A 9 8 7 6 5 4 3 2 1 0
The conversion between binary value and hexadecimal value is similar to octal
number,but using four-bit sections.
The hexadecimal value 20A represents decimal value 522.
Following table provides all the information you need to convert from one type
number into any other type number for the decimal values from 0 to16.
0001 01 01 01 1010 12 10 A
0010 02 02 02 1011 13 11 B
0011 03 03 03 1100 14 12 C
0100 04 04 04 1101 15 13 D
0101 05 05 05 1110 16 14 E
0110 06 06 06 1111 17 15 F
0111 07 07 07 10000 20 16 10
1000 10 08 08
Character Set
The character set in C includes the following members:
Comments
Comments of a C program can be enclosed within a pair of delimiters /* and */.
The symbol // will comment out a subsequent text terminated at the end of
a line. For example,
/* This is a comment */
/* This is a comment
across multiple lines */
Declaration
– An identifier for a variable shall consist of lowercase and uppercase letters,
underscore _, and digits. It shall not start with digits.
– A variable has to be declared before it can be used inside a program. The
following format can be used to declare a variable of simple type.
dataType varName;
where dataType is one of valid data types and varName is an identifier.
– In C99, variables can be declared almost anywhere even after executable
statements.
int main() {
int i;
i = 90;
int j;
j = 20;
return 0;
}
> int i
> sizeof(int)
4
> sizeof(i)
4
> sizeof(2*i)
4
> sizeof(long long)
8
Program: limits.c
#include <stdio.h>
#include <limits.h>
Int main() {
printf(”INT_MAX = %d\n”, INT_MAX);
printf(”INT_MIN = %d\n”, INT_MIN);
printf(”UINT_MAX = %d\n”, UINT_MAX);
return 0;
}
Output:
INT_MAX = 2147483647
INT_MIN = -2147483648
UINT_MAX = 4294967295
– Data of long long integral type contains 8 bytes. They have the
similar representation as the data type of int.
– The long long int ranges from LLONG_MIN to LLONG_MAX,
which are -9223372036854775808LL (-263) and
9223372036854775807LL (263–1), respectively.
– The long long int ranges from 0 to ULLONG_MAX, which is 264–1.
• Integer Constants
Example:
Example:
C:/Ch> int i
C:/Ch> i = 036
30
C:/Ch> i = 0x1e
30
C:/Ch> i = 0b11110
30
C:/Ch> printf(”i = %d\n”, i)
i = 30
C:/Ch> printf(”i = 0%o\n”, i)
i = 036
C:/Ch> printf(”i = 0x%x\n”, i)
i = 0x1e
C:/Ch> printf(”i = 0b%b\n”, i)
i = 0b11110
• Boolean Type
The keyword bool of a declarator in header file stdbool.h can be used
to declare variables with boolean data type. For example, the following
statement
bool b;
declares a boolean variable b.
A boolean variable only has only two possible values: 1 and 0.
Value 1 stands for true and value 0 stands for false. Macro true and
false are defined in header file stdbool.h.
20000 0 1 0 0 1 0 0 0 H
20001 0 1 1 0 0 1 0 1 e
20002 0 1 1 0 1 1 0 0 l
20003 0 1 1 0 1 1 0 0 l
20004 0 1 1 0 1 1 1 1 o
20005 0 0 1 0 0 0 0 1 !
– The macros for minimum and maximum values of signed char are
CHAR_MIN and CHAR_MAX defined in header file limits.h.
CHAR_MIN is equal to -128(-27) and CHAR_MAX is equal to
127(27-1).
– The macro UCHAR_MAX, defined in the header file limits.h,
specifies the maximum value of unsigned short. It is equal to
255(28-1).
• Character Constants
• Escape Characters
Escape Code Description
\a (alert) Produces an audible or visible alert. The active position shall
not be changed.
\b (backspace) Moves the active position to the previous position on the
current line.
\f (form feed) Moves the active position to the initial position at the start
of the next logical page.
\n (new line) Moves the active position to the initial position of the next
line.
\r (carriage return) Moves the active position to the initial position of the
current line.
\t (horizontal tab) Moves the active position to the next horizontal
tabulation position on the current line.
\v (vertical tab) Moves the active position to the initial position of the next
vertical tabulation position.
\\ (backslash) Produces a backslash character \.
\’ (single quote) Produces a single quote character ‘.
\” (double quote) Produces a double quote character “.
\? (question mark) Produces a question mark character ?.
• String Literals
• Floating-Point Types
The float data type uses 32 bits (4 bytes) for its storage. The minimum
and maximum values of float data type are defined as macros
FLT_MIN and FLT_MAX, respectively, in header file float.h.
The double data type uses 64 bits(8 bytes) as its storage. The minimum
and maximum values of double data type are defined as macros
DBL_MIN and DBL_MAX, respectively, in header file float.h
Metanumbers
Below is a list of metanumbers for floating-point numbers and their
mathematical equivalent.
+0.0 0+
-Inf -
+Inf +
> 0.0/0.0
nan
> 1.0/0.0
inf
> -1.0/0.0
-inf
> sqrt(4)
2.000
> sqrt(-4)
nan
Sample Problem:
The system in Figure1 (a) consists of a single body with mass m moving on a
horizontal surface. An external force p acts on the body. The coefficient of
kinetic friction between body and horizontal surface is . The free body diagram
for the system is shown in Figure1 (b).
Equation of motion:
The equation of the motion of the system can be derived based on the Newton's
second law.
N = mg (1)
f =N (2)
p-f = ma (3)
From equation (1), (2) and (3), the formula for calculating the acceleration
of the rigid body can be derived as follows.
a = (p- mg)/m (4)
Problem Statement:
For the system shown in Figure1(a), given m = 5 kg, g = 9.81 m/s2,
= 0.2. The external force p is expressed as a function of time t,
p(t) = 20 when t >= 0
calculate the acceleration a.
Solutions.
1. Declare four variables of double type to represent the mass of the
body (m), the friction coefficient (mu), the external force (p), and
the acceleration(a), respectively.
2. Define macro M_G with the value of 9.81.
int main() {
/* declare variables */
double a, /* acceleration */
mu, /* friction coefficient */
m, /* mass */
p; /* external force */
/* Initialize variables */
mu = 0.2;
m = 5.0;
p = 20.0;
/* display output */
printf("Acceleration a = %f (m/s^2)\n", a);
return 0;
Output: }
int main() {
/* declare variables */
double a; /* acceleration */
double mu; /* friction coefficient */
double m; /* mass */
double p; /* external force */
/* Initialize variables */
mu = 0.2;
m = 5.0;
p = 20.0;
/* display output */
printf("Acceleration a = %f (m/s^2)\n", a);
return 0;
}
Example:
int i, *p;
i = 10;
p = &i; // p contains the address of i
Initialization
– The declaration of a variable may be accompanied by an initializer that specifies
the value of the variable should have at the beginning of its lifetime.
– The initializer for a scalar shall be a single expression, such as
Introduction to Formatted
Input and Output
• Function printf()
Precisely formatted output is accomplished using the output function
printf. The printf function has following form
Example:
> printf(“%d”, 4261) // decimal integer
4261
> printf(“%lld”, 4261); // decimal long long integer
4261
> printf(“%b”, 4261); // binary number in Ch
1000010100101
> printf(“%o”, 4261); // octal number
10245
> printf(“%x”, 4261); // hexadecimal number
10a5
> printf(“%f”, 15.0F); // print out a float
15.000000
> printf(“%f”, 15.0); // print out a double float
15.000000
> printf(“%lf”, 15.0);
15.000000
> printf(“%c”, ‘a’);
a
> printf(“%s”, “This is a string.”);
This is a string.
For example, the format “%.2f” specifies the precision with 2 digits after
the decimal point.
> printf(”%.2f”, 12.1234)
12.12
> printf(”%.2f”, 12.5678)
12.57
> printf(”%.20f”, 0.2)
0.20000000000000001110
The fractional part after the specified precision number is rounded up.
A floating-point number may not be represented exactly.
Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.
C for Engineers and Scientists
• Function scanf()
Precise formatting input is accomplished using the input function
scanf. The scanf function has following form
Example:
> int i
> float f
> double d
> char c
> scanf(“%d”, &i);
10
>i
10
> scanf(“%f”, &f);
10.2
>f
10.20
> scanf(“%lf”, &d);
15
>d
15.0000
> scanf(“%c”, &c);
a
>c
a
Example:
> int i
> scanf(“%d”, &i); // input number in decimal
4261
> Long long l
> scanf(“%lld”, &l); // input into long long number
4261
>l
4261
> scanf(“%b”, &i); // input number in binary in Ch
1000010100101 // or 0b1000010100101
>i
4261
> scanf(“%o”, &i); // input number in octal
10245 // or ‘010245’
>i
4261
> scanf(“%x”, &i); // input number in hexadecimal
10A5 // or ‘0x10A5’ or ‘0X10A5’
>i
4261
int main() {
int num;
double d;
Problem Statement:
/* File: accelio.c */
#include <stdio.h>
#define M_G 9.81
Program:
int main() {
double a, mu, m, p, t;
Using formatted
input function printf("***** Acceleration Calculator *****\n\n");
scanf. printf("Please enter value for mass in kilogram\n");
scanf("%lf", &m);
printf("mass is %lf (kg)====\n\n", m);
printf("Please enter value for friction coefficient\n");
scanf("%lf", &mu);
printf("friction coefficient is %lf\n\n", mu);
printf("Please enter value for time in second\n");
scanf("%lf", &t);
printf("time is %lf (s)\n\n", t);
p = 4*(t-3)+20;
a = (p-mu*m*M_G)/m;
printf("Acceleration a = %f (m/s^2)\n", a);
return 0;
}
Interactive execution:
> accelio.c
***** Acceleration Calculator *****
int main() {
/* declare variables */
double a, /* acceleration */
mu, /* friction coefficient */
m, /* mass */
p; /* external force */
/* Initialize variables */
mu = 0.2;
m = 5.0;
p = 20.0;
/* display output */
printf("Acceleration a = %f (m/s^2)\n", a);
return 0;
Output: }
int main() {
int num;
double d;
program
in ChIDE
editing pane
When a program is
executed in debug
mode, the input breakpoint
and output are
redirected in a
debug pane
separate debug selection bar
console window.
debug pane
output pane
debug
command
pane