Thread: Big Letter became small letter

  1. #16
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by cogeek
    Ok,
    Code:
            printf("The character is %c\n");
    but still getting
    the carachter is 4
    ???
    You must give printf something to print:

    Code:
            printf("The character is %c\n", c);
    Regards.

    Dave

  2. #17
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    OK: i writed this:
    #include <stdio.h>
    main()
    {
    int c, ch;
    printf("Add a character: ");
    scanf("%c", &ch);
    ch = tolower(ch);
    printf("The character is %c\n", ch);
    }
    but when riting a character i get the same for example:
    bash-2.05b$ ./big
    Add a character: A
    The character is A

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The biggest problem is with scanf. It expects a pointer to char, but you gave it a pointer to int. That's not a good thing.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      char ch;
      printf("Add a character: ");
      scanf("%c", &ch);
      ch = (char)tolower(ch);
      printf("The character is %c\n", ch);
      return 0;
    }
    My best code is written with the delete key.

  4. #19
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    I din't get what the (char) mean
    ?

  5. #20
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Casts the returned integer down to a char. Both are integral types.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Look in your book for casting

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I din't get what the (char) mean
    It means "I really do want a char, I promise that I know what I'm doing". Why do you need to tell the compiler that? Because ch is a char and tolower returns an int. Trying to assign a wider type to a narrower type requires a warning, and the cast silences the warning in situations where you really do know what you're doing. Try removing the (char) cast and see what happens, then put it back and compare the results.
    My best code is written with the delete key.

  8. #23
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    And I can't believe no one's told you this yet, but I will however, in lieu of certain "initiatives", shall we say, be nice about it.

    When you post source code as if it was regular text, it's very hard to read. The symbols are hard to read, and there is no indentation. Instead, place [/code] at the end of your source code, and [code] at the beginning, and when you post your message, the site will automatically format your code nicely - there's a good example in Prelude's second-to-last post.
    Last edited by sean; 12-12-2004 at 04:10 PM.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sean_mackrory, regarding not #include<ctype.h>
    If that was the problem, he'd have gotten a compile error.
    Actually no, you wouldn't. You'd get a compiler warning. At least with GCC you will:
    Code:
    ctyp.c: In function `main':
    ctyp.c:5: warning: implicit declaration of function `toupper'
    However, it still compiles. And, since no one here pays attention to their warnings anyway...

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    * S T U D E N T *
    Join Date
    Oct 2003
    Posts
    30
    Quote Originally Posted by cogeek
    I read the manual of C and writed this:
    #include <stdio.h>
    main()
    {
    char c;
    printf("Add a character: ");
    scanf("%c", &c);
    toupper(c);
    printf("The character is %c\n");
    }

    but when running and writing a charachet i get
    the character is 7
    help me please
    could your problem be the fact your using a %c in your printf statement and then not assigning the variable associated with it! Rule1 broken perhaps??? *wags finger*

    also "int main()" is better than just main() as main returns an int.

    Code:
    #include <stdio.h>
    
    int main()
    {
            char c;
            printf("Add a character: ");
            scanf("%c", &c);
            toupper(c);
            printf("The character is %c\n", c);
            return(0);
    }
    try that? (ive not yet so i might edit this post yet! .... i AM tired!)

    --CHriS
    'rm -fr /bin/laden'
    'kill all'

  11. #26
    * S T U D E N T *
    Join Date
    Oct 2003
    Posts
    30
    right well theres an improvement already!

    guyattc2-practical_c3-> ./file
    Add a character: c
    The character is c
    guyattc2-practical_c3->

    now you see your other problem is youve not actually assigned anything to the operation "toupper(c)"

    so heres a revision of the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
            char input;
    	char output;
            printf("Add a character: ");
            scanf("%c", &input);
            output = toupper(input);
            printf("The character is %c\n", output);
            return(0);
    }
    and heres the output:

    guyattc2-practical_c3-> gcc -o file file.c
    guyattc2-practical_c3-> ./file
    Add a character: c
    The character is C
    guyattc2-practical_c3->

    game, set and match! *bows* hehe sorry im in a random m00d today!

    --CHriS
    'rm -fr /bin/laden'
    'kill all'

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by goosematt
    right well theres an improvement already!

    ...code snipped...

    game, set and match! *bows* hehe sorry im in a random m00d today!

    --CHriS
    How many days left are there in that month thing?

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Quote Originally Posted by quzah
    How many days left are there in that month thing?

    Quzah.
    » Roughly, 23 days; 10 hours; 56 minutes [GMT -7] As of right now.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small string program help please
    By learning C++ in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2003, 10:19 AM
  2. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  3. Capatalizing the first letter of a word
    By cprog in forum C Programming
    Replies: 0
    Last Post: 12-07-2002, 06:58 PM
  4. Big and Small
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 10:35 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM