Help with variable? ;[

Discussion in 'Programming General' started by Thebombdotorg, May 8, 2008.

Help with variable? ;[
  1. Unread #1 - May 8, 2008 at 5:08 PM
  2. Thebombdotorg
    Joined:
    Mar 23, 2007
    Posts:
    1,020
    Referrals:
    1
    Sythe Gold:
    5

    Thebombdotorg Guru
    Banned

    Help with variable? ;[

    Okay, so I'm attempting to learn C, but I can't get my head past something..

    printf ("Astronomy is %dderful \n",1);

    Okay, %d is the variable, and it says what the variable is, \n",1); The variable = 1.

    but why wouldn't you just say ("Astronomy is 1derful") Or whatever, instead of typing %d AND typing what %d is after the string.
     
  3. Unread #2 - May 8, 2008 at 8:25 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Help with variable? ;[

    It's useful for times when you will have data that is not static (changing). Here's an example that should help you understand. It prompts you to enter two numbers and then proceeds to display the sum of the two numbers you entered and as it is impossible to determine what the final output will be beforehand, the computer must perform a calculation based on the given input before displaying the the output which includes the result of that computation.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int number_one, number_two;
      printf("Enter two numbers to be added together:\n");  
      
      scanf("%d", &number_one);
      scanf("%d", &number_two);  
      
      printf("\nThe total of your two numbers is: %d", number_one + number_two);
      
      getchar();
      getchar();
      return 0;
    }
     
  5. Unread #3 - May 9, 2008 at 6:26 PM
  6. Thebombdotorg
    Joined:
    Mar 23, 2007
    Posts:
    1,020
    Referrals:
    1
    Sythe Gold:
    5

    Thebombdotorg Guru
    Banned

    Help with variable? ;[

    I think I understand your coding, but I still don't get why you wouldn't just put

    scanf("number_one");
    scanf("number_two");

    Ignore my syntax, please. I don't know much, because I can't get my head past the variables.
     
  7. Unread #4 - May 10, 2008 at 10:04 AM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Help with variable? ;[

    This doesn't work because the scanf() function requires two arguments to be passed.

    Code:
    scanf( [placeholder identifying the variable type], &Variable_to_save_to )
    The first argument is a placeholder prefixed by "%" and surrounded by quotation marks. This indicates the type of variable we will be saving the input into. Some examples are %d (integer), %c (character), %s (string of characters), %f (floating point), %x (hexadecimal), etc.

    The second argument is the variable you want to save the input into which in this case is "Variable_to_save_to". You prefix the second argument (the variable name) with "&" to indicate that you are referencing to its place in memory and not its value because you are looking to modify it, not just read it.

    So a valid use of scanf() would essentially be:
    Code:
    scanf( "%c", &my_character );
    Hope this clears things up somewhat, but feel free to add me on MSN anyway if you like.
     
< Grabbing things of a website such as runescape? | Musical Code >

Users viewing this thread
1 guest


 
 
Adblock breaks this site