summaryrefslogtreecommitdiff
path: root/ANSI_C_Programming/chapter7.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'ANSI_C_Programming/chapter7.ipynb')
-rw-r--r--ANSI_C_Programming/chapter7.ipynb767
1 files changed, 767 insertions, 0 deletions
diff --git a/ANSI_C_Programming/chapter7.ipynb b/ANSI_C_Programming/chapter7.ipynb
new file mode 100644
index 00000000..e79f1d7e
--- /dev/null
+++ b/ANSI_C_Programming/chapter7.ipynb
@@ -0,0 +1,767 @@
+{
+ "metadata": {
+ "name": "chapter7.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 7:THE C PREPROCESSOR"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:226"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using macros in for loop\n",
+ "#Note:Implementing the concept of macros using function\n",
+ "def UPPER(): #set UPPER() as 25\n",
+ " return 25\n",
+ "for i in range(1,UPPER()+1,1):\n",
+ " print \"%d\\n\" % (i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "\n",
+ "2\n",
+ "\n",
+ "3\n",
+ "\n",
+ "4\n",
+ "\n",
+ "5\n",
+ "\n",
+ "6\n",
+ "\n",
+ "7\n",
+ "\n",
+ "8\n",
+ "\n",
+ "9\n",
+ "\n",
+ "10\n",
+ "\n",
+ "11\n",
+ "\n",
+ "12\n",
+ "\n",
+ "13\n",
+ "\n",
+ "14\n",
+ "\n",
+ "15\n",
+ "\n",
+ "16\n",
+ "\n",
+ "17\n",
+ "\n",
+ "18\n",
+ "\n",
+ "19\n",
+ "\n",
+ "20\n",
+ "\n",
+ "21\n",
+ "\n",
+ "22\n",
+ "\n",
+ "23\n",
+ "\n",
+ "24\n",
+ "\n",
+ "25\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:227"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Defing PI as 3.14 using macros(here functions)\n",
+ "def PI(): #set PI() as 3.14\n",
+ " return 3.14\n",
+ "r=6.25\n",
+ "area=PI()*r*r\n",
+ "print \"Area of circle=%f\\n\" % (area)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Area of circle=122.656250\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:229"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Defining AND & OR operations using macros\n",
+ "def AND(x,y): #set AND(x,y) for \"and\" operation\n",
+ " return (x and y)\n",
+ "def OR(a,b): #set OR(a,b) for \"or\" operation\n",
+ " return (a or b)\n",
+ "f=1\n",
+ "x=4\n",
+ "y=90\n",
+ "if (AND(f<5,OR(x<=20,y<=45))):\n",
+ " print \"Your PC will always work fine...\\n\"\n",
+ "else:\n",
+ " print \"In front of the maintenance man\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your PC will always work fine...\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:229"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Using macros inside another macros\n",
+ "def AND(x,y): #set AND(x,y) for \"and\" operation\n",
+ " return (x and y)\n",
+ "def ARANGE():\n",
+ " return (AND(a>25,a<50)) #check for a>25 and a<50\n",
+ "a=30\n",
+ "if (ARANGE()):\n",
+ " print \"within range\\n\"\n",
+ "else:\n",
+ " print \"out of range\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "within range\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:230"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using macros to replace whole statement\n",
+ "def FOUND():\n",
+ " print \"The Yankee Doodle Virus\\n\"\n",
+ "signature='Y'\n",
+ "if (signature=='Y'):\n",
+ " FOUND()\n",
+ "else:\n",
+ " print \"Safe... as yet!\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Yankee Doodle Virus\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:230"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#macros with arguments to find area of circle\n",
+ "def AREA(x):\n",
+ " return (3.14*x*x)\n",
+ "r1=6.25\n",
+ "r2=2.5\n",
+ "a=AREA(r1)\n",
+ "print \"Area of circle=%f\\n\" % (a)\n",
+ "a=AREA(r2)\n",
+ "print \"Area of circle=%f\\n\" % (a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Area of circle=122.656250\n",
+ "\n",
+ "Area of circle=19.625000\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:231"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#compiler interpretation of above program\n",
+ "r1=6.25\n",
+ "r2=2.5\n",
+ "a=3.14*r1*r1\n",
+ "print \"Area of circle=%f\\n\" % (a)\n",
+ "a=3.14*r2*r2\n",
+ "print \"Area of circle=%f\\n\" % (a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Area of circle=122.656250\n",
+ "\n",
+ "Area of circle=19.625000\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:231"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#macros with argument to check if user-input is digit or not\n",
+ "def ISDIGIT(y):\n",
+ " return (y>=48 and y<=57)\n",
+ "print \"Enter any digit\"\n",
+ "ch=raw_input()\n",
+ "if (ISDIGIT(ord(ch))):\n",
+ " print \"You entered a digit\\n\"\n",
+ "else:\n",
+ " print \"Illegal input\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any digit\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You entered a digit\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:232"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#evaluating an expression\n",
+ "def SQUARE(n):\n",
+ " return (n*n)\n",
+ "j=64/SQUARE(4) #it's function not macros ,so j=4\n",
+ "print \"j=%d\\n\" % (j)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "j=4\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:233"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#gotoxy is not present in python,On Linux and other UNIX-like systems you can use curses library.\n",
+ "#We can use setsyx func. of curses in place of gotoxy but it will be limited to unix only.\n",
+ "def HLINE():\n",
+ " for i in range(0,79,1):\n",
+ " print \"%c\" % 45, #characters are bytes and therefore encoding-dependent(non utf-8)\n",
+ " print \"\\n\"\n",
+ "def VLINE(X,Y):\n",
+ " print \"%c\" % 124 #If you want to write 179 in place of 124 then use utf-8 encoding\n",
+ "HLINE()\n",
+ "for y in range(1,25,1):\n",
+ " VLINE(39,y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n",
+ "\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n",
+ "|\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:236-237"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using ifdef macros for conditional compilation\n",
+ "OKAY=False\n",
+ "if OKAY:\n",
+ " import logging\n",
+ " print \"statement 1\"\n",
+ " print \"statement 2\" #detects virus\n",
+ " print \"statement 3\" \n",
+ " print \"statement 4\" #specific to stone virus\n",
+ "print \"statement 5\"\n",
+ "print \"statement 6\"\n",
+ "print \"statement 7\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "statement 5\n",
+ "statement 6\n",
+ "statement 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:237"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using ifdef else macros for conditional compilation\n",
+ "INTEL=False\n",
+ "if INTEL:\n",
+ " print \"code suitable for an Intel PC\"\n",
+ "else:\n",
+ " print \"code suitable for a Motorola PC\"\n",
+ "print \"code common to both the computers\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "code suitable for a Motorola PC\n",
+ "code common to both the computers\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:238"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using ifdef else macros for conditional compilation\n",
+ "INTEL=True\n",
+ "if INTEL:\n",
+ " print \"code suitable for a Intel PC\"\n",
+ "else:\n",
+ " print \"code suitable for a motorola PC\"\n",
+ "print \"code common to both the computers\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "code suitable for a Intel PC\n",
+ "code common to both the computers\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:239"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using #if directive for conditional compilation\n",
+ "TEST=6\n",
+ "if TEST<=5:\n",
+ " print \"statement 1\"\n",
+ " print \"statement 2\"\n",
+ " print \"statement 3\"\n",
+ "else:\n",
+ " print \"statement 4\"\n",
+ " print \"statement 5\"\n",
+ " print \"statement 6\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "statement 4\n",
+ "statement 5\n",
+ "statement 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:239"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using nested if else directives for conditional compilation\n",
+ "ADAPTER='SVGA'\n",
+ "if ADAPTER=='VGA':\n",
+ " print \"code for video graphics array\"\n",
+ "else:\n",
+ " if ADAPTER=='SVGA':\n",
+ " print \"code for super video graphics array\"\n",
+ " else:\n",
+ " print \"code for extended graphics array\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "code for super video graphics array\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:239-240"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using #elif directives for conditional compilation\n",
+ "ADAPTER='SVGA'\n",
+ "if ADAPTER=='VGA':\n",
+ " print \"code for video graphics array\"\n",
+ "elif ADAPTER=='SVGA':\n",
+ " print \"code for super video graphics array\"\n",
+ "else:\n",
+ " print \"code for extented graphics adapter\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "code for super video graphics array\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:241"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Controlled execution of functions\n",
+ "def fun1():\n",
+ " print \"Inside fun1\\n\"\n",
+ "def fun2():\n",
+ " print \"Inside fun 2\\n\"\n",
+ "def main():\n",
+ " print \"Inside main\\n\"\n",
+ "fun1()\n",
+ "main()\n",
+ "fun2()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Inside fun1\n",
+ "\n",
+ "Inside main\n",
+ "\n",
+ "Inside fun 2\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:242"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Statements causing warnings & error\n",
+ "def f1():\n",
+ " a=5\n",
+ "def f2(x):\n",
+ " print \"Inside f2\" #passed parameter is not being used\n",
+ "def f3():\n",
+ " x=6\n",
+ " return x\n",
+ " x+=1 #control can never reach this statement\n",
+ "f1()\n",
+ "f2(7)\n",
+ "f3()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Inside f2\n"
+ ]
+ },
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 20,
+ "text": [
+ "6"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file