I'm beginner in C
this is my code that prints ascii table in different representation such as decimal, hexadecimal, octal and binary.
and I don't know how to reprint a statement (row) that shows what in column every 20 line or rows.
this is my code
#include <stdio.h>
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
int main () {
unsigned char c = 0; /* for the character */
int n = 0; /* integer for the for loop Iteration */
printf_s("\nthis's ASCII table with all representations: \n");
printf_s("\n deci hex oct binary characters\n"); /* header for the table */
for (c = 0; c <= 255; c++)
{
printf_s ("\n %3d %2X %03o \t", c, c, c, c); /* print Ascii using for loop with multi representations */
for (n = sizeof (c) * CHAR_BIT - 1; n >= 0; n--) /* method to convert decimal to base 2 (binary) */
printf_s ("%d", ((c >> n) & 0x1) ? 1 : 0);
printf_s("%5c", c);
putchar ('\n');
if(c==255)
break; /* to stop in 255 and not making infinite loop (because of unsigned char) */
}
putchar ('\n');
return 0;
}
this is how my output look like
deci hex oct binary characters
0 0 000 00000000
1 1 001 00000001 ☺
2 2 002 00000010 ☻
3 3 003 00000011 ♥
4 4 004 00000100 ♦
5 5 005 00000101 ♣
6 6 006 00000110 ♠
7 7 007 00000111
8 8 010 00001000
9 9 011 00001001
10 A 012 00001010
11 B 013 00001011 ♂
12 C 014 00001100 ♀
13 D 015 00001101
14 E 016 00001110 ♫
15 F 017 00001111 ☼
16 10 020 00010000 ►
17 11 021 00010001 ◄
18 12 022 00010010 ↕
19 13 023 00010011 ‼
20 14 024 00010100 ¶
21 15 025 00010101 §
22 16 026 00010110 ▬
23 17 027 00010111 ↨
24 18 030 00011000 ↑
25 19 031 00011001 ↓
26 1A 032 00011010 →
27 1B 033 00011011
8 1C 034 00011100 ∟
29 1D 035 00011101 ↔
30 1E 036 00011110 ▲
31 1F 037 00011111 ▼
32 20 040 00100000
33 21 041 00100001 !
34 22 042 00100010 "
35 23 043 00100011 #
36 24 044 00100100 $
37 25 045 00100101 %
38 26 046 00100110 &
39 27 047 00100111 '
40 28 050 00101000 (
41 29 051 00101001 )
42 2A 052 00101010 *
43 2B 053 00101011 +
44 2C 054 00101100 ,
45 2D 055 00101101 -
46 2E 056 00101110 .
47 2F 057 00101111 /
48 30 060 00110000 0
49 31 061 00110001 1
50 32 062 00110010 2
51 33 063 00110011 3
52 34 064 00110100 4
53 35 065 00110101 5
54 36 066 00110110 6
55 37 067 00110111 7
56 38 070 00111000 8
57 39 071 00111001 9
58 3A 072 00111010 :
59 3B 073 00111011 ;
60 3C 074 00111100 <
61 3D 075 00111101 =
62 3E 076 00111110 >
63 3F 077 00111111 ?
64 40 100 01000000 #
65 41 101 01000001 A
66 42 102 01000010 B
67 43 103 01000011 C
68 44 104 01000100 D
69 45 105 01000101 E
70 46 106 01000110 F
71 47 107 01000111 G
72 48 110 01001000 H
73 49 111 01001001 I
74 4A 112 01001010 J
75 4B 113 01001011 K
76 4C 114 01001100 L
77 4D 115 01001101 M
78 4E 116 01001110 N
79 4F 117 01001111 O
80 50 120 01010000 P
81 51 121 01010001 Q
82 52 122 01010010 R
83 53 123 01010011 S
84 54 124 01010100 T
85 55 125 01010101 U
86 56 126 01010110 V
87 57 127 01010111 W
88 58 130 01011000 X
89 59 131 01011001 Y
90 5A 132 01011010 Z
91 5B 133 01011011 [
92 5C 134 01011100 \
93 5D 135 01011101 ]
94 5E 136 01011110 ^
95 5F 137 01011111 _
96 60 140 01100000 `
97 61 141 01100001 a
98 62 142 01100010 b
99 63 143 01100011 c
100 64 144 01100100 d
101 65 145 01100101 e
102 66 146 01100110 f
103 67 147 01100111 g
104 68 150 01101000 h
105 69 151 01101001 i
106 6A 152 01101010 j
107 6B 153 01101011 k
108 6C 154 01101100 l
109 6D 155 01101101 m
110 6E 156 01101110 n
111 6F 157 01101111 o
112 70 160 01110000 p
113 71 161 01110001 q
114 72 162 01110010 r
115 73 163 01110011 s
116 74 164 01110100 t
117 75 165 01110101 u
118 76 166 01110110 v
119 77 167 01110111 w
120 78 170 01111000 x
121 79 171 01111001 y
122 7A 172 01111010 z
123 7B 173 01111011 {
124 7C 174 01111100 |
125 7D 175 01111101 }
126 7E 176 01111110 ~
127 7F 177 01111111 ⌂
128 80 200 10000000 Ç
129 81 201 10000001 ü
130 82 202 10000010 é
131 83 203 10000011 â
132 84 204 10000100 ä
133 85 205 10000101 à
134 86 206 10000110 å
135 87 207 10000111 ç
136 88 210 10001000 ê
137 89 211 10001001 ë
138 8A 212 10001010 è
139 8B 213 10001011 ï
140 8C 214 10001100 î
141 8D 215 10001101 ì
142 8E 216 10001110 Ä
143 8F 217 10001111 Å
144 90 220 10010000 É
145 91 221 10010001 æ
146 92 222 10010010 Æ
147 93 223 10010011 ô
148 94 224 10010100 ö
149 95 225 10010101 ò
150 96 226 10010110 û
151 97 227 10010111 ù
152 98 230 10011000 ÿ
153 99 231 10011001 Ö
154 9A 232 10011010 Ü
155 9B 233 10011011 ¢
156 9C 234 10011100 £
157 9D 235 10011101 ¥
158 9E 236 10011110 ₧
159 9F 237 10011111 ƒ
160 A0 240 10100000 á
161 A1 241 10100001 í
162 A2 242 10100010 ó
163 A3 243 10100011 ú
164 A4 244 10100100 ñ
165 A5 245 10100101 Ñ
166 A6 246 10100110 ª
167 A7 247 10100111 º
168 A8 250 10101000 ¿
169 A9 251 10101001 ⌐
170 AA 252 10101010 ¬
171 AB 253 10101011 ½
172 AC 254 10101100 ¼
173 AD 255 10101101 ¡
174 AE 256 10101110 «
175 AF 257 10101111 »
176 B0 260 10110000 ░
177 B1 261 10110001 ▒
178 B2 262 10110010 ▓
179 B3 263 10110011 │
180 B4 264 10110100 ┤
181 B5 265 10110101 ╡
182 B6 266 10110110 ╢
183 B7 267 10110111 ╖
184 B8 270 10111000 ╕
185 B9 271 10111001 ╣
186 BA 272 10111010 ║
187 BB 273 10111011 ╗
188 BC 274 10111100 ╝
189 BD 275 10111101 ╜
190 BE 276 10111110 ╛
191 BF 277 10111111 ┐
192 C0 300 11000000 └
193 C1 301 11000001 ┴
194 C2 302 11000010 ┬
195 C3 303 11000011 ├
196 C4 304 11000100 ─
197 C5 305 11000101 ┼
198 C6 306 11000110 ╞
199 C7 307 11000111 ╟
200 C8 310 11001000 ╚
201 C9 311 11001001 ╔
202 CA 312 11001010 ╩
203 CB 313 11001011 ╦
204 CC 314 11001100 ╠
205 CD 315 11001101 ═
206 CE 316 11001110 ╬
207 CF 317 11001111 ╧
208 D0 320 11010000 ╨
209 D1 321 11010001 ╤
210 D2 322 11010010 ╥
211 D3 323 11010011 ╙
212 D4 324 11010100 ╘
213 D5 325 11010101 ╒
214 D6 326 11010110 ╓
215 D7 327 11010111 ╫
216 D8 330 11011000 ╪
217 D9 331 11011001 ┘
218 DA 332 11011010 ┌
219 DB 333 11011011 █
220 DC 334 11011100 ▄
221 DD 335 11011101 ▌
222 DE 336 11011110 ▐
223 DF 337 11011111 ▀
224 E0 340 11100000 α
225 E1 341 11100001 ß
226 E2 342 11100010 Γ
227 E3 343 11100011 π
228 E4 344 11100100 Σ
229 E5 345 11100101 σ
230 E6 346 11100110 µ
231 E7 347 11100111 τ
232 E8 350 11101000 Φ
233 E9 351 11101001 Θ
234 EA 352 11101010 Ω
235 EB 353 11101011 δ
236 EC 354 11101100 ∞
237 ED 355 11101101 φ
238 EE 356 11101110 ε
239 EF 357 11101111 ∩
240 F0 360 11110000 ≡
241 F1 361 11110001 ±
242 F2 362 11110010 ≥
243 F3 363 11110011 ≤
244 F4 364 11110100 ⌠
245 F5 365 11110101 ⌡
246 F6 366 11110110 ÷
247 F7 367 11110111 ≈
248 F8 370 11111000 °
249 F9 371 11111001 ∙
250 FA 372 11111010 ·
251 FB 373 11111011 √
252 FC 374 11111100 ⁿ
253 FD 375 11111101 ²
254 FE 376 11111110 ■
255 FF 377 11111111
And the output I want it to be look like this
deci hex oct binary characters
0 0 000 00000000
1 1 001 00000001 ☺
2 2 002 00000010 ☻
3 3 003 00000011 ♥
4 4 004 00000100 ♦
5 5 005 00000101 ♣
6 6 006 00000110 ♠
7 7 007 00000111
8 8 010 00001000
9 9 011 00001001
10 A 012 00001010
11 B 013 00001011 ♂
12 C 014 00001100 ♀
13 D 015 00001101
14 E 016 00001110 ♫
15 F 017 00001111 ☼
16 10 020 00010000 ►
17 11 021 00010001 ◄
18 12 022 00010010 ↕
19 13 023 00010011 ‼
20 14 024 00010100 ¶
deci hex oct binary characters
21 15 025 00010101 §
22 16 026 00010110 ▬
23 17 027 00010111 ↨
24 18 030 00011000 ↑
25 19 031 00011001 ↓
26 1A 032 00011010 →
27 1B 033 00011011
28 1C 034 00011100 ∟
29 1D 035 00011101 ↔
30 1E 036 00011110 ▲
31 1F 037 00011111 ▼
32 20 040 00100000
33 21 041 00100001 !
34 22 042 00100010 "
35 23 043 00100011 #
36 24 044 00100100 $
37 25 045 00100101 %
38 26 046 00100110 &
39 27 047 00100111 '
40 28 050 00101000 (
deci hex oct binary characters
41 29 051 00101001 )
42 2A 052 00101010 *
43 2B 053 00101011 +
44 2C 054 00101100 ,
45 2D 055 00101101 -
46 2E 056 00101110 .
47 2F 057 00101111 /
48 30 060 00110000 0
49 31 061 00110001 1
50 32 062 00110010 2
51 33 063 00110011 3
52 34 064 00110100 4
53 35 065 00110101 5
54 36 066 00110110 6
55 37 067 00110111 7
56 38 070 00111000 8
57 39 071 00111001 9
58 3A 072 00111010 :
59 3B 073 00111011 ;
60 3C 074 00111100 <
deci hex oct binary characters
61 3D 075 00111101 =
62 3E 076 00111110 >
63 3F 077 00111111 ?
64 40 100 01000000 #
65 41 101 01000001 A
66 42 102 01000010 B
67 43 103 01000011 C
68 44 104 01000100 D
69 45 105 01000101 E
70 46 106 01000110 F
71 47 107 01000111 G
72 48 110 01001000 H
73 49 111 01001001 I
74 4A 112 01001010 J
75 4B 113 01001011 K
76 4C 114 01001100 L
77 4D 115 01001101 M
78 4E 116 01001110 N
79 4F 117 01001111 O
80 50 120 01010000 P
deci hex oct binary characters
81 51 121 01010001 Q
82 52 122 01010010 R
83 53 123 01010011 S
84 54 124 01010100 T
85 55 125 01010101 U
86 56 126 01010110 V
87 57 127 01010111 W
88 58 130 01011000 X
89 59 131 01011001 Y
90 5A 132 01011010 Z
91 5B 133 01011011 [
92 5C 134 01011100 \
93 5D 135 01011101 ]
94 5E 136 01011110 ^
95 5F 137 01011111 _
96 60 140 01100000 `
97 61 141 01100001 a
98 62 142 01100010 b
99 63 143 01100011 c
100 64 144 01100100 d
deci hex oct binary characters
101 65 145 01100101 e
102 66 146 01100110 f
103 67 147 01100111 g
104 68 150 01101000 h
105 69 151 01101001 i
106 6A 152 01101010 j
107 6B 153 01101011 k
108 6C 154 01101100 l
109 6D 155 01101101 m
110 6E 156 01101110 n
111 6F 157 01101111 o
112 70 160 01110000 p
113 71 161 01110001 q
114 72 162 01110010 r
115 73 163 01110011 s
116 74 164 01110100 t
117 75 165 01110101 u
118 76 166 01110110 v
119 77 167 01110111 w
120 78 170 01111000 x
deci hex oct binary characters
121 79 171 01111001 y
122 7A 172 01111010 z
123 7B 173 01111011 {
124 7C 174 01111100 |
125 7D 175 01111101 }
126 7E 176 01111110 ~
127 7F 177 01111111 ⌂
128 80 200 10000000 Ç
129 81 201 10000001 ü
130 82 202 10000010 é
131 83 203 10000011 â
132 84 204 10000100 ä
133 85 205 10000101 à
134 86 206 10000110 å
135 87 207 10000111 ç
136 88 210 10001000 ê
137 89 211 10001001 ë
138 8A 212 10001010 è
139 8B 213 10001011 ï
140 8C 214 10001100 î
deci hex oct binary characters
141 8D 215 10001101 ì
142 8E 216 10001110 Ä
143 8F 217 10001111 Å
144 90 220 10010000 É
145 91 221 10010001 æ
146 92 222 10010010 Æ
147 93 223 10010011 ô
148 94 224 10010100 ö
149 95 225 10010101 ò
150 96 226 10010110 û
151 97 227 10010111 ù
152 98 230 10011000 ÿ
153 99 231 10011001 Ö
154 9A 232 10011010 Ü
155 9B 233 10011011 ¢
156 9C 234 10011100 £
157 9D 235 10011101 ¥
158 9E 236 10011110 ₧
159 9F 237 10011111 ƒ
160 A0 240 10100000 á
deci hex oct binary characters
161 A1 241 10100001 í
162 A2 242 10100010 ó
163 A3 243 10100011 ú
164 A4 244 10100100 ñ
165 A5 245 10100101 Ñ
166 A6 246 10100110 ª
167 A7 247 10100111 º
168 A8 250 10101000 ¿
169 A9 251 10101001 ⌐
170 AA 252 10101010 ¬
171 AB 253 10101011 ½
172 AC 254 10101100 ¼
173 AD 255 10101101 ¡
174 AE 256 10101110 «
175 AF 257 10101111 »
176 B0 260 10110000 ░
177 B1 261 10110001 ▒
178 B2 262 10110010 ▓
179 B3 263 10110011 │
180 B4 264 10110100 ┤
deci hex oct binary characters
181 B5 265 10110101 ╡
182 B6 266 10110110 ╢
183 B7 267 10110111 ╖
184 B8 270 10111000 ╕
185 B9 271 10111001 ╣
186 BA 272 10111010 ║
187 BB 273 10111011 ╗
188 BC 274 10111100 ╝
189 BD 275 10111101 ╜
190 BE 276 10111110 ╛
191 BF 277 10111111 ┐
192 C0 300 11000000 └
193 C1 301 11000001 ┴
194 C2 302 11000010 ┬
195 C3 303 11000011 ├
196 C4 304 11000100 ─
197 C5 305 11000101 ┼
198 C6 306 11000110 ╞
199 C7 307 11000111 ╟
200 C8 310 11001000 ╚
deci hex oct binary characters
201 C9 311 11001001 ╔
202 CA 312 11001010 ╩
203 CB 313 11001011 ╦
204 CC 314 11001100 ╠
205 CD 315 11001101 ═
206 CE 316 11001110 ╬
207 CF 317 11001111 ╧
208 D0 320 11010000 ╨
209 D1 321 11010001 ╤
210 D2 322 11010010 ╥
211 D3 323 11010011 ╙
212 D4 324 11010100 ╘
213 D5 325 11010101 ╒
214 D6 326 11010110 ╓
215 D7 327 11010111 ╫
216 D8 330 11011000 ╪
217 D9 331 11011001 ┘
218 DA 332 11011010 ┌
219 DB 333 11011011 █
220 DC 334 11011100 ▄
deci hex oct binary characters
221 DD 335 11011101 ▌
222 DE 336 11011110 ▐
223 DF 337 11011111 ▀
224 E0 340 11100000 α
225 E1 341 11100001 ß
226 E2 342 11100010 Γ
227 E3 343 11100011 π
228 E4 344 11100100 Σ
229 E5 345 11100101 σ
230 E6 346 11100110 µ
231 E7 347 11100111 τ
232 E8 350 11101000 Φ
233 E9 351 11101001 Θ
234 EA 352 11101010 Ω
235 EB 353 11101011 δ
236 EC 354 11101100 ∞
237 ED 355 11101101 φ
238 EE 356 11101110 ε
239 EF 357 11101111 ∩
240 F0 360 11110000 ≡
deci hex oct binary characters
241 F1 361 11110001 ±
242 F2 362 11110010 ≥
243 F3 363 11110011 ≤
244 F4 364 11110100 ⌠
245 F5 365 11110101 ⌡
246 F6 366 11110110 ÷
247 F7 367 11110111 ≈
248 F8 370 11111000 °
249 F9 371 11111001 ∙
250 FA 372 11111010 ·
251 FB 373 11111011 √
252 FC 374 11111100 ⁿ
253 FD 375 11111101 ²
254 FE 376 11111110 ■
255 FF 377 11111111
This is working for me fine.
#include <stdio.h>
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
int main () {
unsigned char c = 0; /* for the character */
int n = 0; /* integer for the for loop Iteration */
printf("\nthis's ASCII table with all representations: \n");
for (c = 0; c <= 255; c++)
{
if (c%20 == 0)
printf("\n deci hex oct binary characters\n");
printf ("\n %3d %2X %03o \t", c, c, c); /* print Ascii using for loop with multi representations */
for (n = sizeof (c) * CHAR_BIT - 1; n >= 0; n--){ /* method to convert decimal to base 2 (binary) */
printf ("%d", ((c >> n) & 0x1) ? 1 : 0);
}
printf("%5c", c);
putchar ('\n');
if(c==255)
break; /* to stop in 255 and not making infinite loop (because of unsigned char) */
}
putchar ('\n');
return 0;
}
Related
How do you send a keystroke using a .lua script?
i.e. Trigger a F7 keypress (or any key).
My specific environment is Windows 64bit and running a lua script within OBS Studio(but I guess the environment doesn't matter).
I just started playing with this for one of my sons games and I came across this list. Use
keypress(101) would send the keypress "E"
Here is the list via raw pastebin link containing all codes and the corresponding keys.
https://pastebin.com/raw/h13kPdD3
Unknown
0
Backspace
8
Tab
9
Clear
12
Return
13
Pause
19
Escape
27
Space
32
QuotedDouble
34
Hash
35
Dollar
36
Percent
37
Ampersand
38
Quote
39
LeftParenthesis
40
RightParenthesis
41
Asterisk
42
Plus
43
Comma
44
Minus
45
Period
46
Slash
47
Zero
48
One
49
Two
50
Three
51
Four
52
Five
53
Six
54
Seven
55
Eight
56
Nine
57
Colon
58
Semicolon
59
LessThan
60
Equals
61
GreaterThan
62
Question
63
At
64
LeftBracket
91
BackSlash
92
RightBracket
93
Caret
94
Underscore
95
Backquote
96
A
97
B
98
C
99
D
100
E
101
F
102
G
103
H
104
I
105
J
106
K
107
L
108
M
109
N
110
O
111
P
112
Q
113
R
114
S
115
T
116
U
117
V
118
W
119
X
120
Y
121
Z
122
LeftCurly
123
Pipe
124
RightCurly
125
Tilde
126
Delete
127
KeypadZero
256
KeypadOne
257
KeypadTwo
258
KeypadThree
259
KeypadFour
260
KeypadFive
261
KeypadSix
262
KeypadSeven
263
KeypadEight
264
KeypadNine
265
KeypadPeriod
266
KeypadDivide
267
KeypadMultiply
268
KeypadMinus
269
KeypadPlus
270
KeypadEnter
271
KeypadEquals
272
Up
273
Down
274
Right
275
Left
276
Insert
277
Home
278
End
279
PageUp
280
PageDown
281
LeftShift
304
RightShift
303
LeftMeta
310
RightMeta
309
LeftAlt
308
RightAlt
307
LeftControl
306
RightControl
305
CapsLock
301
NumLock
300
ScrollLock
302
LeftSuper
311
RightSuper
312
Mode
313
Compose
314
Help
315
Print
316
SysReq
317
Break
318
Menu
319
Power
320
Euro
321
Undo
322
F1
282
F2
283
F3
284
F4
285
F5
286
F6
287
F7
288
F8
289
F9
290
F10
291
F11
292
F12
293
F13
294
F14
295
F15
296
World0
160
World1
161
World2
162
World3
163
World4
164
World5
165
World6
166
World7
167
World8
168
World9
169
World10
170
World11
171
World12
172
World13
173
World14
174
World15
175
World16
176
World17
177
World18
178
World19
179
World20
180
World21
181
World22
182
World23
183
World24
184
World25
185
World26
186
World27
187
World28
188
World29
189
World30
190
World31
191
World32
192
World33
193
World34
194
World35
195
World36
196
World37
197
World38
198
World39
199
World40
200
World41
201
World42
202
World43
203
World44
204
World45
205
World46
206
World47
207
World48
208
World49
209
World50
210
World51
211
World52
212
World53
213
World54
214
World55
215
World56
216
World57
217
World58
218
World59
219
World60
220
World61
221
World62
222
World63
223
World64
224
World65
225
World66
226
World67
227
World68
228
World69
229
World70
230
World71
231
World72
232
World73
233
World74
234
World75
235
World76
236
World77
237
World78
238
World79
239
World80
240
World81
241
World82
242
World83
243
World84
244
World85
245
World86
246
World87
247
World88
248
World89
249
World90
250
World91
251
World92
252
World93
253
World94
254
World95
255
ButtonX
1000
ButtonY
1001
ButtonA
1002
ButtonB
1003
ButtonR1
1004
ButtonL1
1005
ButtonR2
1006
ButtonL2
1007
ButtonR3
1008
ButtonL3
1009
ButtonStart
1010
ButtonSelect
1011
DPadLeft
1012
DPadRight
1013
DPadUp
1014
DPadDown
1015
Thumbstick1
1016
Thumbstick2
1017
This question already has answers here:
Convert int to double
(3 answers)
Closed 4 years ago.
I am learning C from a text. An example code provided by the author is:
#include <math.h>
main()
{ int i;
printf("\t Number \t\t Square Root of Number\n\n");
for (i=0; i<=360; ++i)
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
}
which on my computer produces an incorrect output I don't understand:
Number Square Root of Number
0 259
1 515
2 771
3 1027
4 1283
5 1539
6 1795
7 2051
8 2307
9 2563
10 2819
11 3075
12 3331
13 3587
14 3843
15 4099
16 4355
17 4611
18 4867
19 5123
20 5379
21 5635
22 5891
23 6147
24 6403
25 6659
26 6915
27 7171
28 7427
29 7683
30 7939
31 8195
32 8451
33 8707
34 8963
35 9219
36 9475
37 9731
38 9987
39 10243
40 10499
41 10755
42 11011
43 11267
44 11523
45 11779
46 12035
47 12291
48 12547
49 12803
50 13059
51 13315
52 13571
53 13827
54 14083
55 14339
56 14595
57 14851
58 15107
59 15363
60 15619
61 15875
62 16131
63 16387
64 16643
65 16899
66 17155
67 17411
68 17667
69 17923
70 18179
71 18435
72 18691
73 18947
74 19203
75 19459
76 19715
77 19971
78 20227
79 20483
80 20739
81 20995
82 21251
83 21507
84 21763
85 22019
86 22275
87 22531
88 22787
89 23043
90 23299
91 23555
92 23811
93 24067
94 24323
95 24579
96 24835
97 25091
98 25347
99 25603
100 25859
101 26115
102 26371
103 26627
104 26883
105 27139
106 27395
107 27651
108 27907
109 28163
110 28419
111 28675
112 28931
113 29187
114 29443
115 29699
116 29955
117 30211
118 30467
119 30723
120 30979
121 31235
122 31491
123 31747
124 32003
125 32259
126 32515
127 32771
128 33027
129 33283
130 33539
131 33795
132 34051
133 34307
134 34563
135 34819
136 35075
137 35331
138 35587
139 35843
140 36099
141 36355
142 36611
143 36867
144 37123
145 37379
146 37635
147 37891
148 38147
149 38403
150 38659
151 38915
152 39171
153 39427
154 39683
155 39939
156 40195
157 40451
158 40707
159 40963
160 41219
161 41475
162 41731
163 41987
164 42243
165 42499
166 42755
167 43011
168 43267
169 43523
170 43779
171 44035
172 44291
173 44547
174 44803
175 45059
176 45315
177 45571
178 45827
179 46083
180 46339
181 46595
182 46851
183 47107
184 47363
185 47619
186 47875
187 48131
188 48387
189 48643
190 48899
191 49155
192 49411
193 49667
194 49923
195 50179
196 50435
197 50691
198 50947
199 51203
200 51459
201 51715
202 51971
203 52227
204 52483
205 52739
206 52995
207 53251
208 53507
209 53763
210 54019
211 54275
212 54531
213 54787
214 55043
215 55299
216 55555
217 55811
218 56067
219 56323
220 56579
221 56835
222 57091
223 57347
224 57603
225 57859
226 58115
227 58371
228 58627
229 58883
230 59139
231 59395
232 59651
233 59907
234 60163
235 60419
236 60675
237 60931
238 61187
239 61443
240 61699
241 61955
242 62211
243 62467
244 62723
245 62979
246 63235
247 63491
248 63747
249 64003
250 64259
251 64515
252 64771
253 65027
254 65283
255 65539
256 65795
257 66051
258 66307
259 66563
260 66819
261 67075
262 67331
263 67587
264 67843
265 68099
266 68355
267 68611
268 68867
269 69123
270 69379
271 69635
272 69891
273 70147
274 70403
275 70659
276 70915
277 71171
278 71427
279 71683
280 71939
281 72195
282 72451
283 72707
284 72963
285 73219
286 73475
287 73731
288 73987
289 74243
290 74499
291 74755
292 75011
293 75267
294 75523
295 75779
296 76035
297 76291
298 76547
299 76803
300 77059
301 77315
302 77571
303 77827
304 78083
305 78339
306 78595
307 78851
308 79107
309 79363
310 79619
311 79875
312 80131
313 80387
314 80643
315 80899
316 81155
317 81411
318 81667
319 81923
320 82179
321 82435
322 82691
323 82947
324 83203
325 83459
326 83715
327 83971
328 84227
329 84483
330 84739
331 84995
332 85251
333 85507
334 85763
335 86019
336 86275
337 86531
338 86787
339 87043
340 87299
341 87555
342 87811
343 88067
344 88323
345 88579
346 88835
347 89091
348 89347
349 89603
350 89859
351 90115
352 90371
353 90627
354 90883
355 91139
356 91395
357 91651
358 91907
359 92163
360 92419
Any idea or clue as to why? If its obvious maybe point to a reference that will point out some stupid mistake I have made?
Read up on this: printf
And use %f for double not %d which is for int
You're using the wrong format specifier here:
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
Make sure to use %f for variables of type double (what a function like sqrt() returns):
printf("\t %d \t\t\t %f \n",i, sqrt((double) i));
You have used a wrong format specifier.
Try,
printf("\t %d \t\t\t %f \n" ,i , sqrt((double) i));
%f for double. Good luck.
I have a database table with the following information:
Test Time
1 1.59
1 1.56
1 1.57
1 1.57
1 0.49
1 1.57
1 1.57
1 1.57
1 1.57
1 1.56
15 0.85
15 0.85
15 0.86
15 0.85
15 0.85
15 0.13
15 0.85
15 0.85
15 0.84
15 0.86
16 1.45
16 1.41
16 1.44
16 1.44
16 0.47
16 1.44
16 1.44
16 1.45
16 1.44
16 1.47
17 5.28
17 5.29
17 3.88
17 5.29
17 5.29
17 4.51
17 5.29
17 5.29
17 5.34
17 5.15
18 3.51
18 3.52
18 3.58
18 3.58
18 4.55
18 3.58
18 3.58
18 3.58
18 3.58
18 3.49
19 2.49
19 2.49
19 2.42
19 2.49
19 2.49
19 2.21
19 2.49
19 2.49
19 2.49
19 2.50
20 2.51
20 2.50
20 2.51
20 2.51
20 1.84
20 2.51
20 2.51
20 2.46
20 2.51
20 2.51
21 2.18
21 2.23
21 2.18
21 2.18
21 2.18
21 1.79
21 2.18
21 2.18
21 2.20
21 2.20
22 0.72
22 0.71
22 0.69
22 0.69
22 0.11
22 0.69
22 0.69
22 0.71
22 0.69
22 0.69
23 133.53
23 133.53
23 133.45
23 133.40
23 133.53
23 118.98
23 133.53
23 133.53
23 132.38
23 133.64
24 1.05
24 1.03
24 1.05
24 1.05
24 0.13
24 1.05
24 1.05
24 1.05
24 1.05
24 1.06
25 2.35
25 2.35
25 2.36
25 2.36
25 2.35
25 0.51
25 2.35
25 2.35
25 2.36
25 2.38
26 0.81
26 0.80
26 0.81
26 0.81
26 0.13
26 0.81
26 0.81
26 0.81
26 0.81
26 0.81
27 15.34
27 15.34
27 15.35
27 15.19
27 15.34
27 14.53
27 15.34
27 15.34
27 15.12
27 15.06
28 1.01
28 1.00
28 0.99
28 0.99
28 0.38
28 0.99
28 1.01
28 0.99
28 0.99
28 0.99
29 0.99
29 0.99
29 0.99
29 1.01
29 0.99
29 0.42
29 0.99
29 0.99
29 1.00
29 1.02
30 0.99
30 0.97
30 0.97
30 0.97
30 0.37
30 0.97
30 0.99
30 0.97
30 0.97
30 0.98
31 21.76
31 21.76
31 21.76
31 21.51
31 21.76
31 21.76
31 23.72
31 21.76
31 21.23
31 21.30
32 39.83
32 39.99
32 40.75
32 46.50
32 40.75
32 40.75
32 41.34
32 40.75
32 40.75
32 40.75
33 3.60
33 3.60
33 3.60
33 3.44
33 3.60
33 3.60
33 2.86
33 3.60
33 3.53
33 3.53
34 61.21
34 62.01
34 61.98
34 61.93
34 61.98
34 61.98
34 64.00
34 61.98
34 61.98
34 62.03
35 4.79
35 4.79
35 4.78
35 4.79
35 4.79
35 4.79
35 8.66
35 4.79
35 4.76
35 4.79
36 1.14
36 1.15
36 0.89
36 0.39
36 0.89
36 0.89
36 0.89
36 0.91
36 0.89
36 0.89
37 0.69
37 0.69
37 0.71
37 0.74
37 0.69
37 0.69
37 0.12
37 0.69
37 0.68
37 0.70
38 1.10
38 1.09
38 1.09
38 0.55
38 1.09
38 1.09
38 1.09
38 1.10
38 1.09
38 1.09
39 1.67
39 1.67
39 1.68
39 1.65
39 1.67
39 1.67
39 0.23
39 1.67
39 1.61
39 1.70
40 1.92
40 1.91
40 1.89
40 0.69
40 1.89
40 1.89
40 1.90
40 1.93
40 1.89
40 1.89
41 12.03
41 12.03
41 12.20
41 11.93
41 12.03
41 12.03
41 3.96
41 12.03
41 11.96
41 12.20
42 2.16
42 2.17
42 2.09
42 1.33
42 2.09
42 2.09
42 2.09
42 2.09
42 2.09
42 2.10
43 27.22
43 27.18
43 27.18
43 27.37
43 27.18
43 27.18
43 27.18
43 20.92
43 26.64
43 27.45
44 21.51
44 20.76
44 8.70
44 21.15
44 21.15
44 21.15
44 21.18
44 21.24
44 21.15
44 21.15
45 17.22
45 17.22
45 17.17
45 17.20
45 17.22
45 17.22
45 17.22
45 14.74
45 16.99
45 16.99
46 15.52
46 14.91
46 25.94
46 15.39
46 15.39
46 15.77
46 15.92
46 15.39
46 15.39
46 15.39
47 8.10
47 8.10
47 8.10
47 7.78
47 8.10
47 8.10
47 8.10
47 7.38
47 7.98
47 8.02
48 8.96
48 8.90
48 8.33
48 9.08
48 9.08
48 9.08
48 9.08
48 8.86
48 9.08
48 9.08
49 3.94
49 3.94
49 3.91
49 3.94
49 3.94
49 3.94
49 3.94
49 1.35
49 3.90
49 3.94
50 0.78
50 0.76
50 0.18
50 0.76
50 0.76
50 0.76
50 0.77
50 0.76
50 0.76
50 0.76
51 1.28
51 1.28
51 1.28
51 1.28
51 1.28
51 1.28
51 0.46
51 1.28
51 1.28
51 1.31
52 7.89
52 7.88
52 7.95
52 7.25
52 7.95
52 7.96
52 7.63
52 7.95
52 7.95
52 7.95
53 16.90
53 16.90
53 16.90
53 15.46
53 17.14
53 16.90
53 14.44
53 16.90
53 16.12
53 15.39
54 1.17
54 1.16
54 1.14
54 0.16
54 1.14
54 1.14
54 1.18
54 1.14
54 1.14
54 1.14
55 0.94
55 0.94
55 0.94
55 0.95
55 0.94
55 0.94
55 0.22
55 0.94
55 0.94
55 0.95
56 2.35
56 2.31
56 3.01
56 1.31
56 3.01
56 3.00
56 3.09
56 3.01
56 3.01
56 3.01
57 2.62
57 2.62
57 2.62
57 2.65
57 2.62
57 2.62
57 1.21
57 2.62
57 1.94
57 1.95
58 3.21
58 3.18
58 3.18
58 2.61
58 3.18
58 3.17
58 3.21
58 3.18
58 3.18
58 3.18
59 2.31
59 2.31
59 2.31
59 2.38
59 2.31
59 2.31
59 1.04
59 2.31
59 1.64
59 1.65
60 3.95
60 3.93
60 3.95
60 3.08
60 3.95
60 3.95
60 3.96
60 3.95
60 3.95
60 3.95
61 3.18
61 3.18
61 3.18
61 3.21
61 3.18
61 3.18
61 2.61
61 3.18
61 3.19
61 3.21
62 0.88
62 0.87
62 0.23
62 0.87
62 0.87
62 0.87
62 0.88
62 0.87
62 0.87
62 0.87
63 0.85
63 0.85
63 0.85
63 0.86
63 0.85
63 0.85
63 0.85
63 0.18
63 0.86
63 0.86
64 0.75
64 0.75
64 0.14
64 0.74
64 0.74
64 0.74
64 0.75
64 0.74
64 0.74
64 0.74
65 0.75
65 0.75
65 0.75
65 0.76
65 0.75
65 0.75
65 0.75
65 0.14
65 0.75
65 0.76
67 0.72
67 0.71
67 0.13
67 0.71
67 0.71
67 0.71
67 0.72
67 0.71
67 0.71
67 0.71
68 1.46
68 1.46
68 1.46
68 1.47
68 1.46
68 1.46
68 1.46
68 1.00
68 1.46
68 1.47
69 6.15
69 6.08
69 2.70
69 6.18
69 6.18
69 6.18
69 6.01
69 6.18
69 6.18
69 6.18
70 1.75
70 1.75
70 1.75
70 1.77
70 1.74
70 1.75
70 1.75
70 0.22
70 1.74
70 1.78
71 0.74
71 0.73
71 0.12
71 0.73
71 0.73
71 0.73
71 0.75
71 0.73
71 0.73
71 0.73
72 0.74
72 0.74
72 0.74
72 0.75
72 0.74
72 0.74
72 0.74
72 0.11
72 0.74
72 0.74
73 0.68
73 0.68
73 0.15
73 0.67
73 0.67
73 0.67
73 0.69
73 0.67
73 0.67
73 0.67
74 0.71
74 0.71
74 0.71
74 0.72
74 0.71
74 0.71
74 0.71
74 0.12
74 0.71
74 0.71
75 1.71
75 1.68
75 0.87
75 1.68
75 1.68
75 1.68
75 1.71
75 1.68
75 1.68
75 1.68
76 0.86
76 0.86
76 0.86
76 0.88
76 0.86
76 0.86
76 0.86
76 0.13
76 0.87
76 0.87
77 23.15
77 22.03
77 10.40
77 22.81
77 22.81
77 22.69
77 22.65
77 22.81
77 22.81
77 22.81
78 22.65
78 22.65
78 22.65
78 22.99
78 22.70
78 22.65
78 22.65
78 10.34
78 22.78
78 22.98
79 30.96
79 30.23
79 18.67
79 30.34
79 30.34
79 30.71
79 30.50
79 30.34
79 30.34
79 30.34
80 1.17
80 1.17
80 1.17
80 1.16
80 1.18
80 1.17
80 1.17
80 0.17
80 1.17
80 1.17
81 2.17
81 2.06
81 0.32
81 2.14
81 2.14
81 2.14
81 2.15
81 2.14
81 2.14
81 2.14
82 0.68
82 0.68
82 0.68
82 0.69
82 0.68
82 0.68
82 0.68
82 0.11
82 0.68
82 0.69
83 0.90
83 0.89
83 0.14
83 0.90
83 0.90
83 0.90
83 0.91
83 0.90
83 0.90
83 0.90
84 0.84
84 0.84
84 0.84
84 0.85
84 0.84
84 0.84
84 0.84
84 0.12
84 0.84
84 0.84
85 1.82
85 1.86
85 0.36
85 1.87
85 1.87
85 1.87
85 1.86
85 1.87
85 1.87
85 1.87
86 31.20
86 31.13
86 31.20
86 31.20
86 15.70
86 31.20
86 31.11
86 31.34
86 30.98
86 31.20
87 22.92
87 23.66
87 23.64
87 23.33
87 22.92
87 11.26
87 22.92
87 22.92
87 22.91
87 22.92
88 6.14
88 6.16
88 6.14
88 6.14
88 6.14
88 6.13
88 5.34
88 6.18
88 6.36
88 6.14
89 8.98
89 9.33
89 9.21
89 6.32
89 8.86
89 8.98
89 8.98
89 8.98
89 8.96
89 8.98
90 8.12
90 8.27
90 8.12
90 8.12
90 8.10
90 8.12
90 8.12
90 8.00
90 5.93
90 8.17
91 34.22
91 33.89
91 33.65
91 21.09
91 33.78
91 33.65
91 33.65
91 33.65
91 33.64
91 33.65
92 2.29
92 2.31
92 2.29
92 2.29
92 2.29
92 2.30
92 0.64
92 2.29
92 2.28
92 2.36
93 1.28
93 1.26
93 1.25
93 0.15
93 1.22
93 1.25
93 1.25
93 1.25
93 1.25
93 1.25
94 0.70
94 0.70
94 0.70
94 0.70
94 0.70
94 0.70
94 0.12
94 0.70
94 0.71
94 0.71
95 0.98
95 0.97
95 0.99
95 0.12
95 0.98
95 0.99
95 0.99
95 0.99
95 0.98
95 0.99
96 3.58
96 3.57
96 3.58
96 3.58
96 3.58
96 3.54
96 0.91
96 3.58
96 3.57
96 3.55
97 8.28
97 8.24
97 8.29
97 13.93
97 8.35
97 8.29
97 8.29
97 8.29
97 8.32
97 8.29
98 8.16
98 8.15
98 8.16
98 8.16
98 8.16
98 8.10
98 11.85
98 8.16
98 8.12
98 8.13
99 7.67
99 7.70
99 7.72
99 7.48
99 7.67
99 7.72
99 7.72
99 7.72
99 7.72
99 7.72
100 23.82
100 23.82
100 23.80
100 23.82
100 23.82
100 24.32
100 11.11
100 23.82
100 24.68
100 25.04
101 20.06
101 19.58
101 19.28
101 6.31
101 19.60
101 19.28
101 19.28
101 19.45
101 19.28
101 19.28
102 2.45
102 2.45
102 2.46
102 2.45
102 2.45
102 2.51
102 1.06
102 2.45
102 2.47
102 2.55
103 1.20
103 1.24
103 1.22
103 0.77
103 1.21
103 1.22
103 1.22
103 1.22
103 1.22
103 1.22
104 343.20
104 343.20
104 343.80
104 343.20
104 343.20
104 340.98
104 476.10
104 343.20
104 343.71
105 1.79
105 1.78
105 1.78
105 0.58
105 0.33
105 1.81
105 1.78
105 1.78
105 1.77
105 1.78
106 13.22
106 13.21
106 13.22
106 13.22
106 13.13
106 12.05
106 12.01
106 13.22
106 13.22
106 15.93
108 7.53
108 6.60
108 6.60
108 5.60
108 5.62
108 6.53
108 6.60
108 6.60
108 6.60
108 6.60
109 5.26
109 5.27
109 5.26
109 5.26
109 5.24
109 4.26
109 4.24
109 5.26
109 5.26
109 6.22
110 7.48
110 6.59
110 6.59
110 5.60
110 5.61
110 6.54
110 6.59
110 6.59
110 6.59
110 6.59
111 5.27
111 5.28
111 5.27
111 5.27
111 5.24
111 4.25
111 4.25
111 5.27
111 6.25
111 5.27
112 0.72
112 0.71
112 0.71
112 0.14
112 0.14
112 0.71
112 0.71
112 0.71
112 0.71
112 0.71
113 0.73
113 0.73
113 0.73
113 0.73
113 0.73
113 0.16
113 0.21
113 0.73
113 0.73
113 0.74
114 0.71
114 0.70
114 0.70
114 0.12
114 0.12
114 0.70
114 0.70
114 0.70
114 0.70
114 0.70
115 0.97
115 0.97
115 0.97
115 0.97
115 0.98
115 0.26
115 0.27
115 0.97
115 0.97
115 0.99
116 0.91
116 0.91
116 0.91
116 0.18
116 0.18
116 0.91
116 0.91
116 0.91
116 0.91
116 0.91
117 0.86
117 0.86
117 0.86
117 0.86
117 0.85
117 0.12
117 0.12
117 0.86
117 0.86
117 0.85
118 3.16
118 3.15
118 3.15
118 0.85
118 0.79
118 3.20
118 3.15
118 3.15
118 3.14
118 3.15
119 5.78
119 5.78
119 5.78
119 5.78
119 5.75
119 5.08
119 5.16
119 5.78
119 5.78
119 7.66
120 2.91
120 2.97
120 2.97
120 2.21
120 2.22
120 2.87
120 2.97
120 2.97
120 2.97
120 2.97
121 1.15
121 1.16
121 1.16
121 1.16
121 1.16
121 1.16
121 1.15
121 0.52
121 0.54
121 1.16
These test iterate or runs ten times so I want to create a stored procedure or function that calculate the average time for each test. How can I do that in T-sql?
Here is my code so far:
IF (OBJECT_ID('Test_CalculateAverageRunTime') IS NOT NULL)
DROP PROCEDURE Test_CalculateAverageRunTime
GO
CREATE PROCEDURE Test_CalculateAverageRunTime
(
#Test varchar(50) = NULL
)
AS
BEGIN
SET NOCOUNT ON
SELECT test, AVG(time) as AverageRunTime
FROM Table1 t
WHERE (#Test IS NULL OR Test = #Test)
SET NOCOUNT OFF
END
GO
Seems like am lost entirely and am not good in writing store procedures. Anyone who can help?
IF (OBJECT_ID('Test_CalculateAverageRunTime') IS NOT NULL)
DROP PROCEDURE Test_CalculateAverageRunTime
GO
CREATE PROCEDURE Test_CalculateAverageRunTime
(
#Test varchar(50) = NULL
)
AS
BEGIN
SET NOCOUNT ON
SELECT test, AVG(time) as AverageRunTime
FROM Table1 t
WHERE (#Test IS NULL OR Test = #Test)
group by test
SET NOCOUNT OFF
END
GO
I have searched for how to calculate the execution time of a program, and i have done exactly in describe of these topic
Execution time of C program
Calculating time of execution with time() function
but my code didn't count the time execution and i don't know why.
here are my code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
time_t begin, end;
begin = clock();
int c = 0;
for (int i = 0; i < 255; i++) {
printf("%d ",c++);
}
end = clock();
printf("\n");
double timeTaken = ((double) (end - begin)) / CLOCKS_PER_SEC;
printf("time taken: %lf", timeTaken);
return (EXIT_SUCCESS);
}
here are what it printed
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
time taken: 0.000000
I have checked many times for error, but it still print "time taken: 0.000000". Can you guys help me out? Thanks a lot.
Actually, you are iterating loop for a small number of times. Try iterating the loop for large number of times. Eg. iterate for 1e9 times. Then you will get noticeable time period. Modern processors are very fast, such that they have frequency upto 2.9 GHz or more, which mean that they can execute around 2 billion instructions, if available, within a second.
#include<stdio.h>
main()
char i=13;
while(i)
{
i++;
}
printf("%d",i);
}
the out put turns out to be zero . how come does it happen
#include<stdio.h>
main()
char i=48;
if(i)
{
printf("%d",i);
}
}
this program run sucessfully and prints 48. isn't i considered a character o for which it is supposed to fail .How is i stored in the memory as a character or a number
You're missing a bracket after 'main()'. Your first code actually keeps counting up from 13, until it hits 127 (if, on your system, chars are represented as signed integers) because this is the largest positive integer for a char on such a system; then flips to -128 and keeps counting up to 0. At 0, you exit your loop, and print the result. To visualize what happens, copy this and try it out:
#include <stdio.h>
int main()
{
char i=13;
while(i){
i++;
printf("%d ", i);
}
printf("\n%d",i);
}
Output:
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 -128 -127 -126 -125 -124 -123 -122 -121 -120 -119 -118 -117 -116 -115 -114 -113 -112 -111 -110 -109 -108 -107 -106 -105 -104 -103 -102 -101 -100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -59 -58 -57 -56 -55 -54 -53 -52 -51 -50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
The positive to negative change has to do with the IEEE representation of integers on computers. You can find many related questions on SO if you google for them.
An if statement compares the numeric value, not the ASCII character that it happens to represent. Only the numeric value 0 will result in the if failing to execute in your second example.