Thread: Variable output as memory address when it should be int

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    50

    Variable output as memory address when it should be int

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int a;
    	int howMany;
    	int maxAmmt = 10;
    	
    	printf("How many lines do you want to loop? (Max 10)\n");
    	scanf("%d", &howMany);
    	
    	for(a = 1; a <= maxAmmt; a++){
    		printf("%d\n, a");
    		
    	}
    Why does the output give a as the memory address’? I want it to show the current integer on the loop.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You misplaced a double quote, i.e., it should have been:
    Code:
    printf("%d\n", a);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    I won’t say what I want to say in case of children. But I get so frustrated at myself for not catching these little typos! I try so hard to go over and over the code, but it’s like selective dyslexia for me. Sorry to bug you on something small again.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try compiling at a high warning level. Your compiler may give you a warning that would make your mistake more obvious to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Ctylersills View Post
    I won’t say what I want to say in case of children. But I get so frustrated at myself for not catching these little typos! I try so hard to go over and over the code, but it’s like selective dyslexia for me. Sorry to bug you on something small again.
    Don't be too hard on yourself - You're always going to make mistakes; everyone does.

    The difference is that as you get more experienced you'll be able to pick up on what is going wrong.


    Looking at other peoples code on forums like this helps as well - You'll see that the same problems keep coming back up.

    Like the classic...
    Code:
    scanf("%d", apple); // doesn't work...
    Have a look at it and see if you can see and fix the problem

    (No one give him the answer)
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Ctylersills, in MY experience, it is impossible to write a code without syntatic errors... In my coding I write something, compile it and get a lot of errors and warnings...
    Click_here tip if gold: Compile it with all available warnings enabled (-Wall -Wextra) and let the compiler check possible errors for you.

    But, sometimes we do valid, but wrong things, anyway... In this case, use a debugger (GDB, por instance)...

    []s
    Fred

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanning variable without address of variable
    By vead in forum C Programming
    Replies: 4
    Last Post: 02-04-2018, 06:25 AM
  2. Local variable address in memory
    By bkane521 in forum Linux Programming
    Replies: 6
    Last Post: 03-03-2013, 08:10 AM
  3. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  4. Replies: 5
    Last Post: 08-14-2009, 03:15 AM
  5. hardbinding a memory address to a variable in code
    By quickNitin in forum C Programming
    Replies: 3
    Last Post: 07-24-2006, 01:09 AM

Tags for this Thread