Thread: Please Tell me..

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    39

    Please Tell me..

    Please Tell me.. where i m WrOng..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    void main(void)
    char ch ;
    scanf("%C",ch);
    printf ("%C", ch);
    }
    Output of this Code, Should the value of 'ch'.. but this is nOt the output by Complier.. output is empty...


    whY!!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your problem is in the scanf line.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There's also a missing brace at the beginning of main - that may be a typo or some such, but the code as it stands does not compile at all.

    --
    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
    Registered User
    Join Date
    Nov 2006
    Posts
    39
    yup.. i missing braket..
    but what is the problem in ???? "Your problem is in the scanf line."

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you should listen to what your compiler is telling you:
    Code:
    temp.c:6: warning: return type of `main' is not `int'
    temp.c: In function `main':
    temp.c:8: warning: format argument is not a pointer (arg 2)

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your argument to scanf must be a pointer to (an address of) ch, not the value of ch which is what ch (by itself) represents. The question then is "how do I get the address of the variable ch?"
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    Well, you should listen to what your compiler is telling you:
    Code:
    temp.c:6: warning: return type of `main' is not `int'
    temp.c: In function `main':
    temp.c:8: warning: format argument is not a pointer (arg 2)
    Make those two errors
    They shouldn't be treated as warnings since they will not allow your program to run properly (or will invoke undefined behavior)!
    -------------------------------------
    So those two, you should fix!
    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.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Also, &#37;C should be lowercase %c ???

    But it is interesting that so many students are given

    main() or some variant... rather than getting started off correctly by their professors

    int main(int argc, char **argv)

    I don't know why they are taught wrong to begin with. Even if the explanation of that is pending until some later lesson.
    (Let's not get into a flame war about unicode argv. LOL). And whether main() truly returns something in all cases, or whether it's recommended to call exit(n) someplace.... ah, more flames.
    Last edited by nonoob; 09-26-2008 at 07:32 AM.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah well there is no grounds for a unicode flame war, nonoob. A char is too small to hold a unicode character. So no matter how you slice it his format string is incorrect. I doubt he was taught wrong. He was probably given correct code and forgot his first week lesson on how C is case sensitive.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nonoob;790599[b
    main()[/b] or some variant... rather than getting started off correctly by their professors

    int main(int argc, char** argv)
    Besides, both of those are correct ( int main and int main(int, char**) ), so there's no reason to start off with the second directly.
    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.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    On that note, in C int main() is not the same as int main(void). In C++ they are interchangeable however in C, int main() is the same as saying int main(...). Your compiler will typically make mention of it if you have your warning levels maxed out.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by master5001
    On that note, in C int main() is not the same as int main(void). In C++ they are interchangeable however in C, int main() is the same as saying int main(...). Your compiler will typically make mention of it if you have your warning levels maxed out.
    That's true, if main() were prototyped. When main() is defined, there is no difference between int main() and int main(void) in both C and C++ (other than the fact that one is four characters shorter than the other).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Even in C++ I am always suspicious of people who put int main(). Since that typically means they are not a C programmer. I have more love for C'ers than strict C++'ers. By more love I really mean I am less abusive of their mistakes...

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    37
    scanf() function always put the given value at given address not anywhere else.
    here
    Code:
        scanf("%c",ch);
    after giving the input if you expect from scanf() to put that value in ch variable then it gonna not be happen anyway cause scanf() dont know at which location it should put that value.

    you have to give the address of ch instead of its name
    Code:
     scanf("%c",&ch);
    where, &ch -- address of ch

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by master5001
    Even in C++ I am always suspicious of people who put int main(). Since that typically means they are not a C programmer. I have more love for C'ers than strict C++'ers. By more love I really mean I am less abusive of their mistakes...
    C99 defines main() without void twice. Try finding the "strict C++'ers" on the C99 committee
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed