How Data Are Stored in The Computer: Decimal System
How Data Are Stored in The Computer: Decimal System
How Data Are Stored in The Computer: Decimal System
Basic knowledge
Hexadecimal
Using 0, 1, ……,9, A, B, C, D, E, and F as a system
of numbers, and when coming to sixteen to carry
one.
The C Programming Language
Basic knowledge
binary decimal
(1011)2=23+ 21+ 20=8+2+1=11
(1011.11)2=11+2-1+2-2 =11+0.5+0.25=11.75
decimal binary
269.6875 (?????)2
2 269 1 0.6875
2 134 0 2
2 67 1 1.3750
2 33 1 2
2 16 0 (100001101)2 0.750 (1011)2
2 8 0 2
2 4 0 1.50
2 2 0 2
2 1 1 1
0 269.6875 (100001101.1011)2
The C Programming Language
Basic knowledge
octal binary
7 3 6 . 2 5 1100 . 0101
001 100 . 010 100
111 011 110 . 010 101
1 4 . 2 4
hexadecimal binary
A 3 F . 2 B
06/11/22 29
for
06/11/22 30
A B C D
ABDCBDCBDCBDC……
06/11/22 31
for
• (1) Expression 1 in the general form of for statement mainly assigns initial
value to loop variable, and it can also be other expressions independent of
loop variable.
• "Expression 1" can be omitted, but the semicolon behind it cannot be omitted.
for( ; i < 5 ; i = i + 1 )
{
sum = sum + i ;
}
When the loop statement is executed, it skips the step of
"Solving Expressions 1" and the rest remains unchanged.
06/11/22 32
for
• (2The value of expression 2 determines whether to
continue the loop.
• "Expression 2" can also be omitted, but the semicolon
after expression 2 cannot be omitted.
for ( i = 1; ; i = i + 1 )
{/*The omission of expression 2 causes the loop to be
dead*/
sum = sum + i;
}
06/11/22 33
for
• (3) "Expression 3" is generally used to change the value
of cyclic variables
• "Expression 3" can also be omitted. Expression 3 can
sometimes be placed after or elsewhere in the built-in
statement as part of the for loop. For example,
for( i = 1 ; i < 5 ; )
{
sum = sum + i ;
i=i+1;
}
06/11/22 34
for
(4) It is possible to omit any two or even all three of
expressions 1, 2 and 3, but neither semicolon can be
omitted. For example,
i = 1;
for ( ; ; )
{ sum = sum + i; i = i + 1; }
0123456789
06/11/22 37
Constants
• Constants are data values that cannot be
changed during the execution of a program.
Like variables, constants have a type. In this
section, we discuss Boolean, character,
integer, real, complex, and string constants.
• Topics discussed in this section:
Constant Representation
Coding Constants
Symbolic Names for Control
Characters
Examples of Integer Constants
Examples of Real Constants
Some Strings
FIGURE 1-10 Null Characters and Null Strings
Symbolic constant
Question: the radius of a circle is provided,
calculate its circumference and area.
void main ( )
{ float r, c, a; something to replace
scanf (“%f”, &r); constant 3.1415926?
c = 2 * 3.1415926 * r ;
a = 3.1415926 * r * r; Symbolic Constant
printf(“c= %6.2f, a=%6.2f \n”, c,
a);
#define PAI 3.1415926
Constants and Variables
Symbolic constant
#define PAI 3.1416 /* define symbolic constant */
void main()
{
float r,c,a;
scanf("%f",&r);
c=2*PAI*r; /* When compiling,PAI be replaced by
3.1416 */
a=PAI*r*r; /* When compiling,PAI be replaced by
3.1416 */
printf("c=%6.2f,s=%6.2f\n",c,a);
} macro substitution
Symbolic constant:
Embedded
#define ONE 1
#define COM “company”
#define MAX 100
#define TWO ONE+ONE
a= b+2; a= b+ TWO;
printf (“%s”, “company”); printf(“%s”,COM);
int array[100]; int array[MAX];
Chapter 1. A Tutorial Introduction
1.4 Symbolic Constants
Character Counting
/*Version 2*/
/*Version 1*/ #include <stdio.h>
#include <stdio.h> main()
main() {
{ double nc;
long nc; for(nc=0;getchar()!=EOF;++nc)
nc=0; ; null statement
while(getchar()! printf(“%.0f\n”,nc);
=EOF) }
++nc;
printf(“%ld\n”,nc);
} ++ means increment by one.
++nc nc=nc+1
Chapter 1. A Tutorial Introduction
1.5 Character Input and Output
a character between
Line Counting single quotes, called character
constant, represents an integer
value equal to the numerical
#include <stdio.h> value of the character. For
/* count lines in input */ example, ‘A’‘s value is 65, ‘\n’,
main() a escape sequence , is also a
{ legal character constant.
int c, nl;
nl=0;
while(( c=getchar())!=EOF)
if(c==‘\n’)
++nl; What value does the
printf(“%d\n”,nl); ‘\n’ has?
}
Chapter 1. A Tutorial Introduction
1.5 Character Input and Output
#include <stdio.h>
#define IN 1 /*inside a word */
Word Counting: #define OUT 0 /*outside a word */
/*count words in input */
main()
{
int c, nw, state;
state=OUT;
What is the nw=0;
function of the while((c=getchar())!=EOF) {
if(c==' '|| c=='\n'|| c=='\t')
variable state? state=OUT;
else if (state==OUT) {
state=IN;
++nw;
}
}
printf("%d\n", nw);
}
Chapter 1. A Tutorial Introduction
1.6 Arrays
1.6 Arrays
Array Example:
#include <stdio.h>
main()
To count
{ digits, white spaces, others.
int c, i, nwhite, nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;++i)
ndigit[i]=0;
while((c=getchar())!=EOF)
if(c>=‘0’ && c <=‘9’) ++ndigit[c-’0’];
else if(c==‘ ’ || c ==‘\n’|| c==‘\t’) ++nwhite;
else ++nother;
printf(“digits= ”);
for(i=0,i<10;i++)
printf (“%d”, ndigit[i]);
printf(“, white space=%d, other=%d\n”, nwhite,
nother);
}
Chapter 1. A Tutorial Introduction
1.6 Arrays
The declaration
int ndigit[10];
ndigit is an array of 10 integers. Its subscripts
start at zero, ndigit[0]…ndigit[9].
char variables and constants are identical to ints
in arithmetic expressions. c – ‘0’ is an integer
expression with a value between 0 and 9.
e.g. :
#define N 5
main()
{
int i=10;
int b[N];
which statement is
int c[i]; not correct?
correct
}
Chapter 1. A Tutorial Introduction
1.7 Functions
1.7 Functions
Function definition form:
1 2 3
return-type function-name (parameter
declarations)
{
declarations
statements
}
Chapter 1. A Tutorial Introduction
1.7 Functions
for(p=1;n>0;--n)
p=p* base;
return p;
}
Chapter 1. A Tutorial Introduction
1.9 Character Arrays
Algorithm:
#include <stdio.h>
#define MAXLINE 1000 /*maxinum input line size */
int getline (char line[ ], int maxline);
void copy(char to[ ], char from[ ]):
main()
{
int len, max;
char line [MAXLINE], longest [MAXLINE];
max = 0;
while(( len = getline (line, MAXLINE)) > 0)
if(len > max)
{max = len; copy (longest, line) ;}
if(max>0) /* there was a line */
printf(“%s”, longest);
return 0;
}
Chapter 1. A Tutorial Introduction
1.9 Character Arrays
#include<stdio.h>
#define MAXLINE 1000 main()
{
int max; int len;
char line[MAXLINE]; extern int max;
char longest[MAXLINE]; extern 全局数组可以没长度 char
int getline(void); longest[];
void copy(void); max=0;
while((len=getline())>0)
if(len>max){
max=len;
In this example, copy();
the extern declarations }
can be omitted. if(max>0)
printf("%s", longest);
return 0;
Chapter 1. A Tutorial Introduction
1.10 External Variables and scope
int getline(void)
{
int c, i;
extern char line[];
for(i=0; i<MAXLINE-1 && (c=getchar()) != EOF && c != '\n'; i++)
line[i]=c;
if(c=='\n'){
line[i]=c;
void copy(void)
i++;
{
}
int i;
line[i]='\0';
extern char line[], longest[];
return i;
} i=0;
while((longest[i]=line[i])!='\0')
++i;
}
Chapter 1. A Tutorial Introduction
1.10 External Variables and scope
• P.8 1-1
• P.21 1-12