Professional Web Applications Themes

Trouble with third C program - Mac Programming

Hi everybody, I recently started learning C on the Mac. I wrote a short program that is supposed to output an ansii character code and its corresponding character. However, when compiled with gcc, I get a garbled output. Can someone look over my code and tell me where I ed up? (I don't think the problem is the code, but something about darwin's ascii implementation) The Code: #include <stdio.h> int main() { int i,j; for(i=0; i<26; i++) { for(j=0; j<10; j++) { printf("%d = %c", 10*i+j, 10*i+j); } printf("\n"); } return 0; } The Output: 0 = 1 = 2 ...

  1. #1

    Default Trouble with third C program

    Hi everybody,

    I recently started learning C on the Mac. I wrote a short program
    that is supposed to output an ansii character code and its
    corresponding character. However, when compiled with gcc, I get a
    garbled output. Can someone look over my code and tell me where I
    ed up? (I don't think the problem is the code, but something
    about darwin's ascii implementation)

    The Code:

    #include <stdio.h>

    int main()

    {
    int i,j;
    for(i=0; i<26; i++)
    {
    for(j=0; j<10; j++)
    {
    printf("%d = %c", 10*i+j, 10*i+j);
    }
    printf("\n");
    }
    return 0;
    }

    The Output:

    0 = 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 =9 =
    10 =
    11 =
    12 =
    14 = 15 = 16 = 17 = 18 = 19 =
    20 = 21 = 22 = 23 = 24 = 25 = 26 = 27 = 8 = 29 =
    30 = 31 = 32 = 33 = !34 = "35 = #36 = $37 = %38 = &39 = '
    40 = (41 = )42 = *43 = +44 = ,45 = -46 = .47 = /48 = 049 = 1
    50 = 251 = 352 = 453 = 554 = 655 = 756 = 857 = 958 = :59 = ;
    60 = <61 = =62 = >63 = ?64 = 65 = A66 = B67 = C68 = D69 = E
    70 = F71 = G72 = H73 = I74 = J75 = K76 = L77 = M78 = N79 = O
    80 = P81 = Q82 = R83 = S84 = T85 = U86 = V87 = W88 = X89 = Y
    90 = Z91 = [92 = \93 = ]94 = ^95 = _96 = `97 = a98 = b99 = c
    100 = d101 = e102 = f103 = g104 = h105 = i106 = j107 = k108 = l109 = m
    110 = n111 = o112 = p113 = q114 = r115 = s116 = t117 = u118 = v119 = w
    120 = x121 = y122 = z123 = {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 = ?255 = ?256 = 257 = 258 = 259 =

    Thanks!
    'cid 'ooh
    Acid Guest

  2. Moderated Post

    Default Re: Trouble with third C program

    Removed by Administrator
    Miro Guest
    Moderated Post

  3. #3

    Default Re: Trouble with third C program

    Hi,


    com (Acid Pooh) writes: 

    Up until here you have control characters. You don't want to print
    them without knowning exactly what they do, because some control
    sequences can mess your display up quite thoroughly.

    Most Unix users have at some point dumped binary files to their screen
    and had to shut down the session to get back a sane display.
     

    These are the normal printable ASCII characters. No problem.
     

    Here you are printing garbage. Terminal.app interprets the byte
    values 128-255 as UTF-8, but UTF-8 requires a correct sequence of byte
    values, which you don't provide. Note that this is not part of ASCII,
    ASCII is only defined from 0-127.

    You can change the interpretation in the perferences of Terminal.app,
    if you need to.
     

    Here you wrap around to the beginning, because printf("%c") will
    always take your input modulo 256. That is because in C "char" is
    just a fance name for "byte".


    benny
    Benjamin Guest

  4. #4

    Default Re: Trouble with third C program

    In article <benny.turtle-trading.net>,
    Benjamin Riefenstahl <de> wrote:

    [snip] 
    >
    > These are the normal printable ASCII characters. No problem.[/ref]

    Except that it's hard to read because there's nothing to separate each
    ASCII character from the ASCII code that follows it. Try changing the
    format spec to something like "%d=%c " or "%d='%c' ".
    Wayne Guest

Similar Threads

  1. #39886 [NEW]: sessions along with other program trouble
    By chris_rd99 at hotmail dot com in forum PHP Bugs
    Replies: 1
    Last Post: December 19th, 02:48 PM
  2. Running another program in current program
    By Durai in forum PERL Beginners
    Replies: 5
    Last Post: April 29th, 01:25 PM
  3. Replies: 2
    Last Post: January 14th, 09:13 AM
  4. Trouble installing Linux (probably 5 partition trouble)
    By Chris in forum Linux Setup, Configuration & Administration
    Replies: 6
    Last Post: December 17th, 09:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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