Small C Programming question

Discussion in 'Mac Help/Tips' started by kaltsasa, Jul 23, 2002.

  1. kaltsasa macrumors 6502a

    Joined:
    Jan 9, 2002
    Location:
    Kellogg IA
    #1
    I'm trying to teach myself the C programming lanquage but i can't eve get hello world off the ground lol, here's the code i have, it'd refuses to compile and says there is a problem with the { any help would be usefull.....maby i'm not cut out to program, i'm u sing the apple program builder in mac os 10.1


    #include <stdio.h>
    main();
    {
    printf ("Hellow World!/n");
    }
     
  2. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #2
    Lose the ; after main()

    Syntax -- the number one problem of student programmers... :rolleyes: :D
     
  3. kaltsasa thread starter macrumors 6502a

    Joined:
    Jan 9, 2002
    Location:
    Kellogg IA
  4. Mr. Anderson Moderator emeritus

    Mr. Anderson

    Joined:
    Nov 1, 2001
    Location:
    VA
    #4
    you'll know when you've made to the big time and really learned c when you can first understand this and be able to recreate it.

    Code:
    #include "stdio.h"
    #define e 3
    #define g (e/e)
    #define h ((g+e)/2)
    #define f (e-g-h)
    #define j (e*e-g)
    #define k (j-h)
    #define l(x) tab2[x]/h
    #define m(n,a) ((n&(a))==(a))
    
    long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L };
    int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 };
    
    main(m1,s) char *s; {
        int a,b,c,d,o[k],n=(int)s;
        if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); }
        else switch(m1-=h){
            case f:
                a=(b=(c=(d=g)<<g)<<g)<<g;
                return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
            case h:
                for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a);
            case g:
                if(n<h)return(g);
                if(n<j){n-=g;c='D';o[f]=h;o[g]=f;}
                else{c='\r'-'\b';n-=j-g;o[f]=o[g]=g;}
                if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
                return(o[b-g]%n+k-h);
            default:
                if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f;
                for(*s=a=f;a<e; ) *s=(*s<<e)|main(h+a++,(char *)m1);
            }
    }
    
    its an obfuscated version of "Hello World"

    good luck

    D
     
  5. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #5
    :eek: That's a pretty frightening piece of code, there Duke!

    And I thought my 32-page function was complex... :rolleyes:
     
  6. Mr. Anderson Moderator emeritus

    Mr. Anderson

    Joined:
    Nov 1, 2001
    Location:
    VA
    #6
    its not mine, just did a search on obfuscated code - there are contests regularly to see who can do the worst. kind of fun actually. this is light weight stuff.

    I don't code anymore really, but I started out my life as a C programmer, moved on to C++ - now that's a good language to play obfuscation with.

    D
     
  7. sageenos macrumors member

    Joined:
    Jan 5, 2002
    Location:
    South Carolina
    #7
    Re: Small C Programming question

    if I'm correct...you need to change the /n to \n in order to have a proper new line character...but I'm a little rusty.
     
  8. Taft macrumors 65816

    Taft

    Joined:
    Jan 31, 2002
    Location:
    Chicago
    #8
    Re: Re: Small C Programming question

    Yep. \ is the escape character. A \ and another character combined form a special character.

    \n is the end line character.

    This is comething that MS never quite standardized. For the filesystem seperation characters, you use \ on MS systems (like C:\Program Files\...). But programmatically, if you try to put them in the string, it interprets all of them as escape characters. Damn I hate them!!!!!

    Taft
     
  9. G4scott macrumors 68020

    G4scott

    Joined:
    Jan 9, 2002
    Location:
    Austin, TX
    #9
    I know how to decently program in BASIC on my TI calculator... I'm hoping to learn C or C++ this coming year. I just hope that I have better luck in learning a programming language than spanish :p
     
  10. sageenos macrumors member

    Joined:
    Jan 5, 2002
    Location:
    South Carolina
    #10
    I took a programming class in high school a couple years ago and it was so much fun. I got to make my first computer game in that class...I had to stay up late the night before my project was due hacking away at the game engine then labor over making all the data files for the game..then I had to test all the data with the engine for bugs. If only research papers were so much fun to work on during the wee-morning hours! I can't wait till Programming classes this fall. Cps101 should be java followed by Cps102 which should be C/C++. I can't wait till I get a mac so I can start working with Objective C. I could start in Obj-C now, but I'm having trouble using gcc 2.95.3 to compile my source files...maybe I'll figure it out soon.
     
  11. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #11
    Spanish? 32 pages?

    G4scott:

    You should know better, you can't programme a computer using Spanish. Try an Asian language since most of the components come from Asia. :D


    szark:

    Why in the world would you not simply a function into its component functionality instead of having 32 pages of code? :confused:
     
  12. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #12
    Re: 32 pages?

    That's what I'd like to know! :D

    Seriously, though, it's one of those things that starts small and gets out of hand relatively quickly as new functionality is added. Basically it's a giant case statement that could easily be broken up, but it would end up as about 50-70 separate functions...

    Maybe I'll get around to dividing it up one of these days...months...years... :rolleyes:
     
  13. awrc macrumors regular

    Joined:
    Apr 20, 2002
    Location:
    Milwaukee, WI
    #13
    Re: Spanish? 32 pages?

    Oh, I can think of some circumstances where I've had to write very long (maybe not 32 pages) functions - largely complex math code where the C function is an implementation of a math function and pulling out code into separate functions actually makes things less readable, not more. It's also often true in these cases that you don't want to make unnecessary function calls because of the overhead and because you know that your compiler will produce markedly better code if you just give it a big hunk of straightforward code to chew on rather than a bunch of little chunks interspersed with calls to subroutines. It can optimize the little chunks, it can optimize the subroutines, but generally the optimization doesn't cross subroutine boundaries (or didn't last time I was poking around in the guts of a compiler, which was admittedly seven or eight years ago), so you lose performance.

    These functions can often end up looking larger than they really are, too, because they cry out for liberal commenting to explain just what's going on, and you also end up using lots of extra whitespace just to prevent the code from becoming horribly dense and unreadable.

    Al
     
  14. G4scott macrumors 68020

    G4scott

    Joined:
    Jan 9, 2002
    Location:
    Austin, TX
    #14
    I have a question... What programming languages does Apple's project builder support? It may sound a little silly, but hey, i'm new to this whole thing...
     
  15. Beej macrumors 68020

    Beej

    Joined:
    Jan 6, 2002
    Location:
    Buffy's bedroom
    #15
    Objective-C and Java.
     
  16. balliet macrumors member

    Joined:
    Dec 21, 2001
    #16
    I think that depends on what you mean by support. Objective C and Java are probably best supported. C and c++ are supported too. But really, it can support anything. You can create custom targets that will run any command you want. So you could use it for just about any language. (Although you'd only get syntax highlighting, etc for c/c++/objc/java).
     
  17. Ensign Paris macrumors 68000

    Ensign Paris

    Joined:
    Nov 4, 2001
    Location:
    Europe
    #17
    I have always wanted to get into using C, I can program Basic to a fairly good level and I love AppleScript, I did one hell of alot of work in Facespan (facespan.com) when the OSX version is release I will be continueing work on a program to control Apache and another to manage a PDF work flow (my job)

    Anyone know any good C books?

    Ensign
     
  18. kaltsasa thread starter macrumors 6502a

    Joined:
    Jan 9, 2002
    Location:
    Kellogg IA
    #18
    My boss recomended Sam's learn C in 21 days, havnt had the time to take a look at it yet though.
     
  19. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #19
    Re: Re: Spanish? 32 pages?

    That's a good point -- this program has WAY too many levels of function calls already (but speed isn't a big issue for this particular program).

    extra whitespace -- definitely needed.

    commenting -- what's that??? :confused: :rolleyes: :D

    Ensign:

    I found Sam's Perl in 21 days very helpful in teaching myself Perl, so I imagine the C book would be good also.
     
  20. awrc macrumors regular

    Joined:
    Apr 20, 2002
    Location:
    Milwaukee, WI
    #20
    I'd personally recommend avoiding C to anyone who's looking to teach themselves programming. It's tremendously flexible, but this comes at a price - C gives you a huge amount of rope with which to hang yourself, and when you get into reasonably complex code (pointers to arrays of pointers to lists of pointers to functions) and start messing about with stuff like pointer arithmetic, bad things are almost guaranteed to happen. The only way I've found to make that sort of stuff manageable and maintainable is to adopt a pseudo-OO programming style, and if you're going to do that, you might as well use a language designed for it.

    C's like having parents who set absolutely no rules or boundaries - you might turn out a remarkably balanced and well-rounded individual, or you might turn out a sociopathic axe murderer. Lots of flexibility, but lots of room to write really scary code and do things in bizarre and perverse ways (see the obfuscated code example somebody posted - I've known people who wrote code like that all the time).

    Overall, I think it's much better to start with a strongly-typed language like Java or C++ where the compiler can spot more potential problems at compile-time and the restrictions imposed by the type system combined with the additional structure OO places on your code make it less likely you'll create a monster.

    Both are more like having strict parents, although at times the way C++ has been cobbled together over the years is "strict" in the sense of "they kept me locked in a cage in the basement until I was 18". Yes, you can still turn out to be an axe murderer, but you'll be a polite axe murderer.

    Objective-C's the best (or worst) of both worlds. It's weakly typed, wth all the potential power (and possibilities for mayhem that arise When Good Code Goes Bad) that implies. However, if you're good about statically typing object references where possible, the compiler can still catch a lot of stuff, and the dynamic aspects of the language have made me swear off C++ for life (or at least until the next time I have to use it for work). You're also less likely to find yourself dealing with the nastier stuff than in C (unless you really try hard to) if you're doing the OO thing.

    I can't think of a good parental analogy for Objective-C - perhaps the parent who doesn't place many limits but warns you before you're about to do something stupid and says "told you so" if you go ahead and do it anyway :D

    There are those who go by the adage "strong typing, weak mind" but that's really just "programmer macho" along the lines of "I coded this operating system entirely in machine language by toggling this little switch here several hundred thousand times". Strong typing is generally your friend, and if you're doing stuff advanced enough to find it getting in the way, you're probably good enough by that point to work around it.

    Fortunately ProjectBuilder keeps all four options open, but OS X itself kinda favors the use of Objective-C or Java rather than the other two.

    For those who really, really want to learn C itself, I don't really have a book to recommend - my shelves have the big Osborne "Complete Reference" books for both C and C++ on them but I've barely cracked the cover of either. What I do find quite useful, though, is a smaller/cheaper book, also from Osborne, called "C/C++ Programmer's Reference" by Herbert Schildt. It's $20 ($12.95 plus shipping from Bookpool) and about the size of a large paperback. It isn't intended to teach either language, but it's very handy to have around. It covers both languages, most of the functions in the standard libraries, has some stuff on the STL and even points out C99-specific stuff.
     
  21. mmcneil macrumors regular

    Joined:
    Sep 4, 2001
    Location:
    San Diego, CA
    #21
    Go straight to Cocoa

    Just got a copy of "Building Cocoa Applications", O'Reilly and Apple has put a LOT of power at your fingertips with PB and IB. This is much easier than X-windows:D
     

Share This Page