Help with tuturial concept noob

This is a discussion on Help with tuturial concept noob within the C Programming forums, part of the General Programming Boards category; Hi, well, I've decided to start learning code, and I chose C, but I was reading the tutorial, and this ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    Help with tuturial concept noob

    Hi, well, I've decided to start learning code, and I chose C, but I was reading the tutorial, and this one part really confused me. I just don't understand the purpose of the line, because the program runs the same with or without it, and the concept is confusing to me.

    This is what the tutorial said

    "This is where things start to get interesting: the scanf function works by taking a string and some variables modified with &. The string tells scanf what variables to look for: notice that we have a string containing only "%d" -- this tells the scanf function to read in an integer. The second argument of scanf is the variable, sort of. We'll learn more about what is going on later, but the gist of it is that scanf needs to know where the variable is stored in order to change its value. Using & in front of a variable allows you to get its location and give that to scanf instead of the value of the variable. Think of it like giving someone directions to the soda aisle and letting them go get a coca-cola instead of fetching the coke for that person. The & gives the scanf function directions to the variable. "

    That kinda went completely over my head.

    This is the code associated with what they are talking about

    Code:
     #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
    
      printf( "Please enter a number: " );
      scanf( "%d", &this_is_a_number );
      printf( "You entered %d", this_is_a_number );
      getchar();
      return 0;
    }
    What is the purpose of the whole this_is_a_number thing? What is the &?

    Because if I delete all of the instances where it says thisisanumber, it runs the same, which is understandable to me, because the code makes sense. It is simply printing what it scanned for. I just don't get the thisisanumber thing.

    I appreciate your response, and I hope one day I can look back at this and facepalm myself as you are probably doing right now.

    Thanks,

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    That tutorial sounds confusing. No wonder you don't get it. this_is_a_number is an integer variable that hasn't been assigned a value. The scanf statement assigns a value to this_is_a_number. The following printf statement prints the value you just assigned to this_is_a_number. I'm not entirely sure why you need an ampersand using scanf, but I know you need one.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    Don't waste your time with that tutorial, buy the book: Sams Teach yourself C in 21 days, and learn C with success.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I don't recommend 'Teach yourself xxx in dd days' books.
    The C programming language, The C Book(C Book)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Long explanation short, scanf reads input from user and stores that data in variables of your choice. So you read an integer. Then you need somewhere to store it.
    That is what you do with the "&this_is_a_number" part. Pure and simple.
    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.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Quote Originally Posted by mrfahrenheit94 View Post
    Code:
     #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
    
      printf( "Please enter a number: " );
      scanf( "%d", &this_is_a_number );
      printf( "You entered %d", this_is_a_number );
      getchar();
      return 0;
    }
    What is the purpose of the whole this_is_a_number thing? What is the &?

    Because if I delete all of the instances where it says thisisanumber, it runs the same, which is understandable to me, because the code makes sense. It is simply printing what it scanned for. I just don't get the thisisanumber thing.
    To answer your questions in order, this_is_a_number is exactly what it says on the tin. C, being a statically typed language, has ints for integers, and such like. You can find out what this_is_a_number is by looking at the definition on an earlier line: int this_is_a_number;

    Perhaps more interesting is the ampersand. It retrieves the address of a variable. That is a unary operator in C, which means as an operator it takes one "thing" as its operand. In context of scanf, it becomes an argument to compliment the %d part of the string.

    If you removed those parts and it still works, I have to wonder what that means. It could be that you're running an old executable that does not reflect those changes. Either way, the tutorial's code is correct. Your changes should result in complaints from working compilers, "too few arguments to scanf".

    I agree with the recommendation for a book if you don't have one. That said if you don't understand these answers, reply, but I also didn't want to leave your questions unanswered. That can be a bit of a let-down.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406

    input

    when you write int you r saying to the compiler -the name written here will be a variale to hold a whole number, please reserve me a memory space (address) to hold it. then in scanf u are saying -please store the value input into the address reserved earlier of the variable named after the ampersand sign. think of furniture types moving into house addresses
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. Question about concept of class
    By Roy01 in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2006, 12:15 AM
  3. my noob program doesnt work
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2005, 11:40 AM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. help !!concept of class templates
    By sanju in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2003, 08:12 AM

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