Thread: Help with printf

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Help with printf

    Can someone explain what i am doing wrong..I am new to C and trying to print out the hexadecimal, decimal, etc. to the ouput but I get the same output for each character I type in which obviously isn't right. I am not declaring the c as a constant anything so im not sure why it's not working. Could someone give me suggestions please?? Thanks
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    int main (void)
    {
        char buffer [BUFSIZ] ;
        char c[5];
        
        printf ("Enter Character: ");
        fgets (buffer, BUFSIZ, stdin) ;
        if ( buffer [strlen (buffer)] == '\n')
            buffer [strlen(buffer)] = '\0' ;
         while(gets(c) != NULL)   
            {        puts(c); 
              printf("hexadecimal integer using lower case a to F: %x\n", c);
            printf("hexadecimal integer using upper case A to F: %X\n", c);
            printf("Decimal Number: %u\n", c);
            printf("octal long: %lo\n", c);
            printf("hex short: 0x%hx\n", c);
    
     
        getchar () ;
    
        return 0 ;
                 printf("\n"); 
    }
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You type enter character and then read in to buffer, instead of c. Why?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    reply

    From what I understand from the person trying to teach me this.. You need to hold it in the buffer. I don't fully understand how it all works..am reading on it now but that's the way I thought I was to do it?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    c is array
    so when printing c - - you print the address of the first char, that will never change

    to print first char in that array use c[0]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by f4ichick02 View Post
    From what I understand from the person trying to teach me this.. You need to hold it in the buffer. I don't fully understand how it all works..am reading on it now but that's the way I thought I was to do it?
    Well, there's nothing wrong with holding it in a buffer -- but then you need to go from the buffer into c. Right now you read into a buffer, which you then promptly ignore and go and get more input for c, which defeats the purpose of a buffer (i.e., you've went and allowed buffer overflows again, which the use of buffer+fgets was supposed to prevent).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM