Thread: little help for newb

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    little help for newb

    ive decided to learn c, as a base to learn object c cocoa and java, so i got a copy of K&R and was fine for few pages, wrote a few short things from imagination which worked and i was happy, but then the next example fails to compile, so its not me cause its the example and i just get an error when compiling it.

    im using xcode, so there is a chance i could have set that up wrong, but i didnt get any errors till this example.

    the example is
    Code:
    #include <stdio.h> 
    #define IN 1 /* inside a word */ 
    #define OUT 0 /* outside a word */ 
    /* count lines, words, and characters in input */ 
    main() 
    { 
    	int c, nl, nw, nc, state; 
    	state = OUT; 
    	nl = nw = nc = 0; 
    	while ((c = getchar()) != EOF) { 
    	++nc;
    		if (c == '\n') 
    			++nl; 
    		if (c == ' ' || c == '\n' || c = '\t') 
    			state = OUT; 
    		else if (state == OUT) { 
    			state = IN; 
    			++nw; 
    		} 
    	} 
    	printf("&#37;d %d %d\n", nl, nw, nc); 
    }
    and the error is shown by xcode as being "if (c == ' ' || c == '\n' || c = '\t') " in that but it could be the line above.
    but either way im stuck cause its the first example in the section so i dont understand the subject enough to even have a clue what im doing wrong.

    sorry it such a simple question but i cant find anyone with the same problem on the net.
    thanks
    Last edited by Salem; 12-02-2008 at 10:27 AM. Reason: Added [code][/code] tags, learn to use them yourself.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    sorry the error is "invalid lvalue in assignment"

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I suppose that you are getting "invalid lvalue in assignment" on line 14. That is because you are trying to assing '\t' into
    Code:
    c == ' ' || c == '\n' || c
    - you are lucky, because if you had done:
    Code:
    (c == ' ') || (c == '\n') || (c = '\t')
    then it would have compiled fine, but you would have wondered why the word count was wrong, when all other values where right...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimbojones View Post
    ive decided to learn c, as a base to learn object c...
    Whoa, wait hang on.
    Object C of everything. Is there a specific reason you want to learn objective C? If you want to simply learn object-oriented programming, then C++ is the logical step.
    And if you want to learn object-oriented programming through C++, then you can skip C altogether.

    So, why did you want to learn Objective C again?

    Code:
    #include <stdio.h> 
    #define IN 1 /* inside a word */ 
    #define OUT 0 /* outside a word */ 
    /* count lines, words, and characters in input */ 
    main() 
    { 
    	int c, nl, nw, nc, state; 
    	state = OUT; 
    	nl = nw = nc = 0; 
    	while ((c = getchar()) != EOF) { 
    	++nc;
    		if (c == '\n') 
    			++nl; 
    		if (c == ' ' || c == '\n' || c == '\t') 
    			state = OUT; 
    		else if (state == OUT) { 
    			state = IN; 
    			++nw; 
    		} 
    	} 
    	printf("&#37;d %d %d\n", nl, nw, nc); 
    }
    Use code tags.
    See missing = I added in red. You used assignment, not comparison.

    Also, don't use implicit main: http://cpwiki.sourceforge.net/Implicit_main
    Bad book.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    whats funny is i didnt understand your reply, and was wondering how to say "thankyou, but im a dunce and dont get it" and wondering why the K&R book was wrong, and i noticed the missing = and it all makes sense, sorry for dumb question.

    thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a bad book because it also uses implicit main, which is typically frowned upon today and removed altogether in newer C standards.
    The missing "=" was probably a typo.
    And you have managed to avoid the why question, regarding objective C...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One of the terms in C is "lvalue", and that is anything that can be on the left side of an assignment.
    So, formally, an assignment consists of an lvalue and an rvalue.
    Code:
    lvalue = rvalue;
    So, the lvalue must be something that can be assigned into, such as a simple variable or a pointer dereference.

    Something like:
    Code:
    int a, b;
    
    a + b = 7
    obviously doesn't make much sense, and the compiler will say "not an lvalue" because "a + b" is not something that you can assign a value to.

    By the way, "l" in lvalue stands for "left", and "r" in rvalue, as you probably could guess is "right".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    By the way, "l" in lvalue stands for "left", and "r" in rvalue, as you probably could guess is "right".
    I've heard that this is a coincidence, and lvalue actually means location value, given the idea is to store an rvalue somewhere. That should eliminate from the mind things that aren't assignable, such as arrays.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whiteflags View Post
    I've heard that this is a coincidence, and lvalue actually means location value, given the idea is to store an rvalue somewhere. That should eliminate from the mind things that aren't assignable, such as arrays.
    Wikipedia seems to think that L-value is to do with "left" rather than "location", but I suppose it can be seen to mean both things.

    http://en.wikipedia.org/wiki/Value_(computer_science)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    cocoa is objective c so thats why, but having looked on the net at people asking the question "where should i start" with regards to this, all that seems to happen is people all start giving their opinion and then they disagree with each other and you end up with confusion and angry people, so i thought id just learn c, then move on, probably have to do java next as i want to have a decent ish grasp of that by march so i can knock off the first year of OU course before the summer.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    cocoa is objective c so thats why, but having looked on the net at people asking the question "where should i start" with regards to this, all that seems to happen is people all start giving their opinion and then they disagree with each other and you end up with confusion and angry people
    And you'd be right.

    I personally don't think there is a correct order or algorithm to learning programming, but you should be aware that you are learning procedural programming if you start with C, rather than object oriented as you intended to later. Some people think this is the right idea. Nevertheless, OOP is a separate paradigm and when you do learn how to do it expect to spend a lot of time learning foundational theory. Don't leave here thinking that learning C will do anything for you besides introduce bits of syntax earlier.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Jimbo, it seems to me that Elysia is pretty much fully invested in the Microsoft Kool-Aid. This seems to have resulted in your specific mention of Cocoa immediately following Objective-C being completely ignored.

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by rags_to_riches View Post
    Jimbo, it seems to me that Elysia is pretty much fully invested in the Microsoft Kool-Aid.
    Be that as it may, but nor is everything Microsoft the Source Of All Evil, that some people claim it to be.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rags_to_riches View Post
    Jimbo, it seems to me that Elysia is pretty much fully invested in the Microsoft Kool-Aid. This seems to have resulted in your specific mention of Cocoa immediately following Objective-C being completely ignored.
    There are many languages out there, and I cannot know them all. Cocoa was unknown to me.
    Furthermore, Objective C is a pretty bad language if you ask me - it tried to add to C what it severely lacked - objected oriented programming.
    However, there is a far more advanced and better language for that which incorporates all C and more which is called C++.
    Furthermore, ObjC is not as widely supported as either C or C++ - it is like it is somewhere in between, which is never good.

    So then let's change this from languages to another question:
    What is it that you wish to do?
    Clearly you intend to write some sort of software - where is it targeted? What will they do?
    Usually, with the kind of answers, people can suggest you the right tool for the job.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    well my job allows me to do pretty much what i like, so i thought id get a BSc in computing and another job in application developement, and specifically only in OSX (maybe linux) so its cocoa, and i'd assume the degree will address java and sql, php, etc etc.... so basically anything that avoids microsoft.

Popular pages Recent additions subscribe to a feed