Thread: display variables

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    display variables

    i got a problem in displaying variables.

    i wrote this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    
    {
    
     int x;             
     int *pointer;   
     pointer=&x;     
     scanf("%ld", x);        
     printf ("*pointer");
     return 0;
    
    }
    but the command printf isnt displaying the variable.
    
    maybe this c++ code helps:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    
    {
    
     int x;             
     int *pointer;   
     pointer=&x;     
     cin>>x;          
     cout<<*pointer;
     return 0;
    
    }
    but i need this c++ code in ordinary C. whats the translation for "cout << *pointer"?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Why do you include so many header files without using them? Anyway, your problem is that you were using both scanf and printf wrong:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int x;
      int *pointer;
    
      pointer = &x;
      scanf ("%d", &x);
      printf ("%d\n", *pointer);
    
      return 0;
    }
    scanf takes the address of non-array types. If you forget this then the program will seg fault. Also, %ld designates a long integer, not an integer, with scanf this distinction is very important so you should get into the habit of using the correct flag. printf takes format flags as well, the way you had it the only thing printed to the screen would be "*pointer".

    -Prelude
    My best code is written with the delete key.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM