Thread: Beginner question

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Originally posted by quzah
    Well the first two responses cleared this up for him.
    Yeah, but he still he understood everything differently, judging by his posts, he thinks that a char is a way to put a number in program by using a 'letter' (example: a) to specify a certain number, like another way to put numbers. Thats why he obviosly doesn't get it, and thats why I stated everything again.

    Originally posted by quzah On a side note, a char does not hold a string. A char holds one single character. An array of characters can be used to old a string, also, a character pointer which points to allocated space can be used for strings. However, a single char does not hold a string. Just to be picky.
    Also true, but I also learned about this a while after I learned of ''strings''. Its good that you put it on a side note, because I really feel that is also a little complicated for now. So if your don't get it now, don't worry, you'll understand it later on!
    Last edited by James00; 05-21-2003 at 07:36 PM.

  2. #17
    Registered User
    Join Date
    May 2003
    Posts
    49
    Oh, I think I understand it now I just have a few questions about the example codes you gave:



    #include <stdio.h>
    int main()
    {
    char letter;



    You said you can give the value to letter later, but you don't explain how. I thought I could give it it's value later by putting:

    letter = 'h';

    but it didn't work. Can you please explain how and where in the program you gave letter it's value?






    #include <stdio.h>
    int main()
    {
    char words[20];


    This is to tell it how many characters are in the variable's value, right? Is "words" the variable, and if it is, how do I give it it's value?
    Also, if I can do this:

    #include <stdio.h>
    int main()
    {
    char words[] = "I am the best";

    and let the computer figure out how many characters there are, why would I ever want to use the previous example and find the number of characters myself?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    You said you can give the value to letter later, but you don't explain how. I thought I could give it it's value later by putting:

    letter = 'h';

    but it didn't work. Can you please explain how and where in the program you gave letter it's value?
    It does work. You're probably not looking at the data correctly. A character may be displayed in numerous ways. Here are a few:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char letter;
    
        letter = 'h';
    
        printf("The decimal value of letter is %d.\n", letter );
        printf("The character value of letter is %c.\n", letter );
    
        return 0;
    }
    See? It's all on how you look at the data.

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

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Glad to hear that I was able to help you out! Anyway, I'll try and explain each question you have as easy as I possibly can.

    You said you can give the value to letter later, but you don't explain how. I thought I could give it it's value later by putting:

    letter = 'h';

    but it didn't work. Can you please explain how and where in the program you gave letter it's value?
    As quzah said it should be able to work. What I think happened is that you never used printf to display the variable. What you did is give the letter variable the value of 'h', but you didn't display it, which is done with the printf function. Type and compile this and you will see what I mean:

    Code:
    #include <stdio.h>
    
    int main()
    {
    char letter;
    
    letter='h';
    
    printf("The variable letter holds the '%c' character",letter);
    
    return 0;
    }
    The ' ' marks that surround the %c are just used so the user can see that its specifing a character, you don't really have to put them in order for the variable to be displayed. As you can see here, the printf function displays the variable on the screen.

    There are also other ways in which you can assign a value to a char variable, for example using the scanf function which reads what the user is typing and converts it to a variable. Example:

    Code:
    scanf("%s",word);
    This is probably a little to advanced right now, so I suggest that you continue to read your book and you will see this function very soon. It is one of the very first functions explained, and I think that it is better if you read it out of the book. If you don't understand how the function works, don't worry, just keep reading.

    #include <stdio.h>
    int main()
    {
    char words[20];

    This is to tell it how many characters are in the variable's value, right? Is "words" the variable, and if it is, how do I give it it's value?
    Yes you are absolutely correct, words is the variable! There are several ways in which you can give a char variable a value, if you want to put more than 1 character (string or 'words') you could the same as with the letter varible earlier in this post (just change the ' ' quotes for " " ), or you could also use the scanf function that I mentioned to you before (remember, if you don't quite understand how that function works, just keep reading).

    Also, if I can do this:

    #include <stdio.h>
    int main()
    {
    char words[] = "I am the best";

    and let the computer figure out how many characters there are, why would I ever want to use the previous example and find the number of characters myself?
    That is a good question, I used to have the same questions when I was starting up. Anyway, the real potential of variables is that you can change there values anytime and as many times as you want, and char varibles are almost always used to store something the user types. For example, you want to make a program in which the program asks what is the name of the user, and then the user types his name. This name is stored in a char variable and later used in a printf statement to display "Hello NAME!!!". And this is why you normally don't put values to char variables, you just declare them. If you have any other questions, feel free to ask.

    P.S. When you post code, just put [*code] (without asterisk) at the beginning of it, and [*/code] (without asterisk) at the end so that it is diplayed neatly!
    Last edited by James00; 05-22-2003 at 01:06 AM.

  5. #20
    Registered User
    Join Date
    May 2003
    Posts
    49
    Code:
    #include <stdio.h>
    int main()
    {
    char words[20];
    This is to tell it how many characters are in the variable's value, right? Is "words" the variable, and if it is, how do I give it it's value?




    Yes you are absolutely correct, words is the variable! There are several ways in which you can give a char variable a value, if you want to put more than 1 character (string or 'words') you could the same as with the letter varible earlier in this post (just change the ' ' quotes for " " ), or you could also use the scanf function that I mentioned to you before (remember, if you don't quite understand how that function works, just keep reading).



    Actually, what I meant was how I give words it's value (it's single letter or mulitple words), as in, do I give "words" it's value by doing this:

    Code:
    #include <stdio.h>
    int main()
    {
    char words [20] = "Hey, my name is Joe.";
    Or by:

    Code:
    words = "Hey, my name is Joe.";
    or some other way?



    Also, if I can do this:
    Code:
    #include <stdio.h>
    int main()
    {
    char words[] = "I am the best";
    and let the computer figure out how many characters there are, why would I ever want to use the previous example and find the number of characters myself?
    --------------------------------------------------------------------------------



    That is a good question, I used to have the same questions when I was starting up. Anyway, the real potential of variables is that you can change there values anytime and as many times as you want, and char varibles are almost always used to store something the user types. For example, you want to make a program in which the program asks what is the name of the user, and then the user types his name. This name is stored in a char variable and later used in a printf statement to display "Hello NAME!!!". And this is why you normally don't put values to char variables, you just declare them. If you have any other questions, feel free to ask.



    Either I'm misunderstanding your answer or you misunderstood my question, but it sounds like your telling me why people don't specify the number of characters in a value (char[]) sometimes. I was actually asking why I would I want to put:

    Code:
    #include <stdio.h>
    int main()
    {
    char words [20];
    and find out how many characters there are (20) rather than letting the computer do that:

    Code:
    #include <stdio.h>
    int main()
    {
    char words[];


  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    [Actually, what I meant was how I give words it's value (it's single letter or mulitple words), as in, do I give "words" it's value by doing this:


    Code:
    #include <stdio.h>
    int main()
    {
    char words [20] = "Hey, my name is Joe.";
    Or by:


    Code:
    words = "Hey, my name is Joe.";
    or some other way?
    You could use both methods you have described above, but there is also another way (actually, many more, but to advanced right now) to give a value to a char variable, and I explained it in my last post. Here it is:


    There are also other ways in which you can assign a value to a char variable, for example using the scanf function which reads what the user is typing and converts it to a variable. Example:


    Code:
    scanf("%s",word);
    This is probably a little to advanced right now, so I suggest that you continue to read your book and you will see this function very soon. It is one of the very first functions explained, and I think that it is better if you read it out of the book. If you don't understand how the function works, don't worry, just keep reading.



    Either I'm misunderstanding your answer or you misunderstood my question, but it sounds like your telling me why people don't specify the number of characters in a value (char[]) sometimes. I was actually asking why I would I want to put:


    Code:
    #include <stdio.h>
    int main()
    {
    char words [20];
    and find out how many characters there are (20) rather than letting the computer do that:


    Code:
    #include <stdio.h>
    int main()
    {
    char words[];
    Ok I misunderstood your question. The answer to your question is that when you only declare a variable, and not give it a value right away, you always have to tell the compiler how much characters (memory) the variable can hold up. So if you do this, char words[]; you will get an error, that statement is not correct, you can only put [] when you are giving the value right away. So remember to alway put how many characters (memory) you will need in your variable when you are not giving it any value at the beginning. You don't have to put exactly how many characters you will use, the number between the brackets ( [ ] ) just has to be 1 higher than your total characters used (including a space!). So if you declare a char variable the following way, char words[20];, you can put between 19 and 1 characters. Hope that you now understand this!!!
    Last edited by James00; 05-22-2003 at 03:27 PM.

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    49
    How do I give a variable it's value later on in the program after telling it how many characters are in the value of the variable (char words[20]? Well, here is an example code of what I'm talking about (by the way, tell me if I'm doing something wrong here ):

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char call [67];
        	printf("%s", call);
    
       Now, where and how do I give the value of the variable "call"?
    
    	return 0;
    }
    Last edited by Tride; 05-22-2003 at 06:10 PM.

  8. #23
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Ok I will post a simple program where you declare a char variable and the user types his name (his name becomes the variable). Here it is (type in your editor and post it):

    Code:
    #include <stdio.h>
    
    int main()
    {
    char name[20];
    
    printf("Please write your name and press ENTER: ");     //the \n thing tells the printf function to move the cursor one line down (kind of like pressing ENTER) 
    scanf("%s",&name);
    printf("Your name is %s!!! \n",name);
    
    return 0;
    }
    This is a very basic program, you must understand almost everything (the // are comments, they don't affect the code, you don't have to write them). What you may not understand is the scanf function, but as I have told you before, just continue to read your book and it will be explained shortly!

    P.S. Do you have a book about C?

  9. #24
    Registered User
    Join Date
    May 2003
    Posts
    49
    #include <stdio.h>
    code]int main()
    {
    char name[20];

    printf("Please write your name and press ENTER: "); //the \n thing tells the printf function to move the cursor one line down (kind of like pressing ENTER)
    scanf("%s",&name);
    printf("Your name is %s!!! \n",name);

    return 0;
    }[/code]


    This is a very basic program, you must understand almost everything (the // are comments, they don't affect the code, you don't have to write them). What you may not understand is the scanf function, but as I have told you before, just continue to read your book and it will be explained shortly!


    Actually, I know what the scanf function is, what the \n thing is, and what comments are . But anyway, about your code. Is declaring the number of characters (char name [20];, of course) something you only do when the user is inputing something? Because I don't understand what the point of declaring the number of characters in the variable is if the user is going to put a random amount of characters, which could very well be over 19. And how do I give the value to the variable later in the program using char word [20]; (without scanf)?

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An array is setting aside a fixed block of N elements of a given type. Thus:

    char foo[N];

    This allocates a block of N number of characters. You can do whatever you want with these. You can treat them as a string, reserving at least one for a NULL to. You can use all 20 if you don't plan on using them as a string.

    There are many reasons to use arrays. If you know you'll only need N amount of an item, or perhaps if you know you'll need less than N. Additionally, you can just read N elements in, and if there are more, handle what you've got, then go back and pick up the rest.

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

  11. #26
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    First of all, good to see that you know more than I thought, now I can explain things to you much easier! Now, let me put it this way, you only declare the number of characters (as in char name[20]; ) when you are not giving its value right away (as in char name[]="I am the best"; ), its that simple. This is a Rule of Thumb, don't forget it! This is the way that the C language was made, and you will get an error if you don't do it like this. You have to specify the number of memory that each variable has so that the program will not produce an error. For example run the program I posted last time, and when the program prompts you to give your name, write more than 20 characters, and after you press ENTER the program will most likely crash!!! (it may not happen on your PC, if this is the case then write a huge number of chracters (about 100) and press ENTER, you will then surely get an error and the program will stop running). This just proves that the computer needs to know how much memory you need for each variable, and if you use more than that memory, then the program crashes. There is obviously a way to prevent the user from typing more characters than that of the variable, but that is a little advanced for now. Just remember this, be sure to give each char variable a decent amount of characters (depending on what you are asking) so that the user doesn't use more than the ones you specified. For example, if you prompt the user to put his name put [20], but if you ask him for his last name, it might be a nice bet to put [30] just in case someone has a very large last name. Just don't put a number to high in the brackets [ ] because your program will run slower! If you still don't understand I can explain it to you in a different way, just give me your thoughts.
    Last edited by James00; 05-22-2003 at 07:59 PM.

  12. #27
    Registered User
    Join Date
    May 2003
    Posts
    49
    I think I understand, but the only thing is (and this doesn't really have to do with it) when I run the last code example you gave, it doesn't print anything I input after a space. (e.g. in the program, if I input "My name is Eric Ridon" it will only display "My".) Is this supposed to happen?

    Other than that, I'm pretty sure I understand it.
    Last edited by Tride; 05-23-2003 at 12:37 PM.

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Tride
    I think I understand, but the only thing is (and this doesn't really have to do with it) when I run the last code example you gave, it doesn't print anything I input after a space. (e.g. in the program, if I input "My name is Eric Ridon" it will only display "My".) Is this supposed to happen?

    Other than that, I'm pretty sure I understand it.
    Your problem has nothing to do with printf. It's with scanf. Scanf %s stops on whitespace. Thus, once it hits a space, it stops reading input.

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

  14. #29
    Registered User
    Join Date
    May 2003
    Posts
    49
    And is there any way to change that or is that just what is?

  15. #30
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Tride
    And is there any way to change that or is that just what is?
    Yes and no. No you cannot get %s to include spaces. Yes you can get around it.

    1) Don't use scanf. Use fgets or something similar instead. (Do not use gets] ever.)

    2) Use the %[ ] format specifier for scanf to get around it, by specifying white space and omitting it.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM