Printing a char.

This is a discussion on Printing a char. within the C Programming forums, part of the General Programming Boards category; Hi, I made this program that is supposed to print a char variable, but it's not working. It wont print ...

  1. #1
    Jst
    Jst is offline
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17

    Printing a char.

    Hi,
    I made this program that is supposed to print a char variable, but it's not working. It wont print the variable str, instead it sais:
    [Warning] initialization makes integer from pointer without a cast
    This is my code:
    Code:
    #include <stdio.h>
    
    main()
    {
          char str="Hello World";
          printf("%c", str);
          system("pause");
          return 0;
    }

    Tnx in advance,
    Jst.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Next time please post your actual code as the code you provided can not create that error.
    So lets show what you probably have:
    Code:
    #include <stdio.h>
    
    main()
    {
          char *str="Hello World";
          printf("%c", str);
          system("pause");
          return 0;
    }
    So right now str is a point to an array of characters. Which means it holds multiple characters.
    When you do
    Code:
    printf("%c", str);
    it tells printf to print one char from str. But str is not a character its many. So which character do you want?

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by Thantos
    Next time please post your actual code as the code you provided can not create that error.
    Hmm, his original code definitely creates that error with my compiler...
    Code:
    itsme@itsme:~/C$ gcc -Wall error.c -o error
    error.c:4: warning: return type defaults to `int'
    error.c: In function `main':
    error.c:5: warning: initialization makes integer from pointer without a cast
    error.c:7: warning: implicit declaration of function `system'
    itsme@itsme:~/C$
    The reason is that char is basically just a 1-byte int, and "Hello World" is evaluated as a pointer (a pointer to the first character in the string). See why the warning is there now? You really are trying to assign a pointer to an int.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Jst
    Jst is offline
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    I read a few tutorials about pointers, I think I understand a few things about it now. I'm now able to print one letter, it's the correct one, but it needs to print the whole string and I'm not sure how to do that.
    Code:
    #include <stdio.h>
    
    main()
    {
          char *txt="Hello World";
          printf("%c", *txt);
          system("pause");
          return 0;
    }
    I know how to get another letter then the first one, but I'dont know how to get the whole line of text.


    Tnx
    Jst.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    To print a string you should use %s instead of %c. %s expects a pointer so you won't have to dereference txt to print the string.
    If you understand what you're doing, you're not learning anything.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by itsme86
    Hmm, his original code definitely creates that error with my compiler...
    Code:
    itsme@itsme:~/C$ gcc -Wall error.c -o error
    error.c:4: warning: return type defaults to `int'
    error.c: In function `main':
    error.c:5: warning: initialization makes integer from pointer without a cast
    error.c:7: warning: implicit declaration of function `system'
    itsme@itsme:~/C$
    The reason is that char is basically just a 1-byte int, and "Hello World" is evaluated as a pointer (a pointer to the first character in the string). See why the warning is there now? You really are trying to assign a pointer to an int.
    Weird the first error should have been at initalization since a char can not hold the address of a string.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Dang it. That'll teach me not to look at line numbers I thought it was complaining on the printf line not the initialzation line

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well, in all fairness, the OP wasn't kind enough to post which line was giving the error
    If you understand what you're doing, you're not learning anything.

  9. #9
    Jst
    Jst is offline
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    Quote Originally Posted by itsme86
    Well, in all fairness, the OP wasn't kind enough to post which line was giving the error
    Next time I will post the line wich gives the error .

    This is my code now:
    Code:
    #include <stdio.h>
    
    main()
    {
          char *txt="Hello World\n";
          printf("%s", txt);
          system("pause");
          return 0;
    }
    It works, tnx for all your help.
    Jst.

  10. #10
    Jst
    Jst is offline
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    Code:
    #include <stdio.h>
    
    main()
    {
          char *str;
          printf("Name: ");
          scanf("%s", str);
          printf("Hello %s", str); 
          system("pause");
          return 0;
    }
    When I compile this code, nothing's wrong, but when I run it and I enter my name it stops working and gives that WinXP error thing, where you can choose to send the error or not.


    What is wrong with the code?
    Jst.

  11. #11
    ocy
    ocy is offline
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Code:
    ...
          printf("Name: ");
          scanf("%s", &str);
          printf("Hello %s", str); 
    ...
    Notice the difference? You'd missed something out, something very important.

    The scanf function needs to know where exactly to store your input, in memory. Since, there's a missing '&', so the function didn't know where. A mistake learners usually make, so, bare that in mind the next time you code, ok.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Also as a good practice, explicitly state the return value for main() and explicitly state the arguments like so when you intend to have no arguments:
    Code:
    int main(void)
    The cost of software maintenance increases with the square of the programmer's creativity.

  13. #13
    Jst
    Jst is offline
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    Code:
    #include <stdio.h>
    
    int main(void)
    {
          char *str;
          printf("Name: ");
          scanf("%s", &str);
          printf("Hello %s\n", &str); 
          system("pause");
          return 0;
    }
    This is my code now, tnx for the reply's it works .
    Jst.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well, you're lucky it works because str is pointing to a random memory address. You haven't allocated any memory for it. Try declaring str as an array instead of a pointer.
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by itsme86
    Well, you're lucky it works because str is pointing to a random memory address. You haven't allocated any memory for it. Try declaring str as an array instead of a pointer.
    I was under the impression that you can use either an array or a pointer in this case. I thought that *str and str[0] both pointed the same place, so why is *str unacceptible here?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21