summaryrefslogtreecommitdiff
path: root/Teach_Yourself_C_in_24_Hours/hour23.ipynb
blob: ba61a82dd104737cfe52aea32c421ccad07d9aca (plain)
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
{
 "metadata": {
  "name": "",
  "signature": "sha256:95f715496e847b4ccbd328c0da6129f101af5cd4b3c551a2f5644703db14657d"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Hour 23 : Compiling Programs: The C Preprocessor"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 23.1, Page No 394"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "METHOD = \"ABS\"\n",
      "def ABS(val):\n",
      "    if val < 0:\n",
      "        return -(val)\n",
      "    else:\n",
      "        return (val)\n",
      "MAX_LEN = 8\n",
      "NEGATIVE_NUM = -10\n",
      "array = range(MAX_LEN)\n",
      "print \"The orignal values in array:\"\n",
      "for i in range(MAX_LEN):\n",
      "    array[i] = (i + 1) * NEGATIVE_NUM;\n",
      "    print \"array[\",i,\"]:\",array[i]\n",
      "print \"\\nApplying the \",METHOD,\" macro:\\n\"\n",
      "for i in range(MAX_LEN):\n",
      "    print \"ABS(%d) : \"%(array[i]), ABS(array[i])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The orignal values in array:\n",
        "array[ 0 ]: -10\n",
        "array[ 1 ]: -20\n",
        "array[ 2 ]: -30\n",
        "array[ 3 ]: -40\n",
        "array[ 4 ]: -50\n",
        "array[ 5 ]: -60\n",
        "array[ 6 ]: -70\n",
        "array[ 7 ]: -80\n",
        "\n",
        "Applying the  ABS  macro:\n",
        "\n",
        "ABS(-10) :  10\n",
        "ABS(-20) :  20\n",
        "ABS(-30) :  30\n",
        "ABS(-40) :  40\n",
        "ABS(-50) :  50\n",
        "ABS(-60) :  60\n",
        "ABS(-70) :  70\n",
        "ABS(-80) :  80\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 23.2, Page No 398"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#There is no concept of the mentioned directives in Python.\n",
      "UPPER_CASE = True\n",
      "LOWER_CASE = False\n",
      "if UPPER_CASE:\n",
      "    print \"THIS LINE IS PRINTED OUT,\\n\"\n",
      "    print \"BECAUSE UPPER_CASE IS DEFINED.\\n\"\n",
      "if not LOWER_CASE:\n",
      "    print \"\\nThis line is printed out,\\n\"\n",
      "    print \"because LOWER_CASE is not defined.\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "THIS LINE IS PRINTED OUT,\n",
        "\n",
        "BECAUSE UPPER_CASE IS DEFINED.\n",
        "\n",
        "\n",
        "This line is printed out,\n",
        "\n",
        "because LOWER_CASE is not defined.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 23.3, Page No 401"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "C_LANG = 'C'\n",
      "B_LANG = 'B'\n",
      "NO_ERROR = 0\n",
      "if C_LANG == 'C' and B_LANG == 'B':\n",
      "    del C_LANG\n",
      "    C_LANG = \"I only know C language.\\n\"\n",
      "    print C_LANG\n",
      "    del B_LANG\n",
      "    B_LANG = \"I know BASIC.\\n\"\n",
      "    print C_LANG,B_LANG\n",
      "elif C_LANG == 'C':\n",
      "    del C_LANG\n",
      "    C_LANG = \"I only know C language.\\n\"\n",
      "    print C_LANG\n",
      "elif B_LANG == 'B':\n",
      "    del B_LANG\n",
      "    B_LANG = \"I only know BASIC.\\n\"\n",
      "    print B_LANG\n",
      "else:\n",
      "    print \"\u201cI don\u2019t know C or BASIC.\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "I only know C language.\n",
        "\n",
        "I only know C language.\n",
        "I know BASIC.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 23.4, Page No 403"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ZERO = 0\n",
      "ONE = 1\n",
      "TWO = ONE + ONE\n",
      "THREE = ONE + TWO\n",
      "TEST_1 = ONE\n",
      "TEST_2 = TWO\n",
      "TEST_3 = THREE\n",
      "MAX_NUM = THREE\n",
      "NO_ERROR = ZERO\n",
      "def StrPrint(ptr_s,max1):\n",
      "    for i in range(max1):\n",
      "        print \"Content: \",ptr_s[i],\"\\n\"\n",
      "\n",
      "str1 = [\"The choice of a point of view\",\"is the initial act of culture.\",\"--- by O. Gasset\"]\n",
      "if TEST_1 == 1:\n",
      "    if TEST_2 == 2:\n",
      "        if TEST_3 == 3:\n",
      "            StrPrint(str1,MAX_NUM)\n",
      "        else:\n",
      "            StrPrint(str1,MAX_NUM - ONE)\n",
      "    else:\n",
      "        StrPrint(str1,MAX_NUM - ONE)\n",
      "else:\n",
      "    \"No TEST macro has been set.\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Content:  The choice of a point of view \n",
        "\n",
        "Content:  is the initial act of culture. \n",
        "\n",
        "Content:  --- by O. Gasset \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 9
    }
   ],
   "metadata": {}
  }
 ]
}