Thread: doesn't print what it's supposed to

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    1

    doesn't print what it's supposed to

    Hi guys. I've got a problem with a piece of code that it doesn't seem to work anymore. It used to, but now it doesn't and i don't know where i'm wrong. The code is this:

    Code:
    #include <stdio.h>
    #include <conio.h>
    main ()
    {
        int a;
        printf ("Type a value for a: \n");
        scanf("%d", &a);
        printf("%d in octal is: %o\n", a, a);
        printf("%d in hexadecimal is: %x\n", a, a);
    }
    i chose a to be 5 and it displays the following:

    "Type a value for a:
    5
    5 in octal is: 5
    5 in hexadecimal is: 5

    Process returned 23 <0x17> execution time : 1.031 s".

    I first saw this when trying to display the address of a pointer. Am i missing something? I used to run this code on dev-c++ successfully but after a day or so of practice, it's not working anymore. I switched from dev-c++ to code blocks thinking that i would solve this problem, but no. I also have to say that i'm a newbie to programming . Thank you in advance.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That code prints exactly what it is required to. It is your understanding that is flawed if you expect them to be different.

    The value 5 prints the same in octal (base 8), decimal (base 10), and hexadecimal (base 16) because it is less than 8. Try a larger value. A value of 8 will print as 10 in octal, and 8 in both hex and decimal. A value of 10 will print as 12 (octal), 10 (decimal), or A (hex).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Process returned 23 <0x17> execution time : 1.031 s".
    Plus you get some random number printed because you fall of the end of main without an explicit
    return 0;
    statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    > Process returned 23 <0x17> execution time : 1.031 s".
    Plus you get some random number printed because you fall of the end of main without an explicit
    return 0;
    statement.
    Section (5.1.2.2.3) (c99, c11). C89 doesn't seem to say the same thing, but I only glanced at it.
    If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument; reaching the } that terminates the
    main function returns a value of 0
    . If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified.
    So although I think it's poor form, it's not 100% necessary to explicitly have return 0; at the end of main(), unlike other functions that expect a return value.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    As an experiment. Note that online compilers are not compliant implementations although they use compliant compilers as their backends (this is just for fun)

    Code:
    #include <stdio.h>
    
    #if defined(__clang__)
        static const char *cn = "clang";
    #elif defined(__ICC) || defined(__INTEL_COMPILER)
        static const char *cn = "intel";
    #elif defined(__GNUC__) || defined(__GNUG__)
        static const char *cn = "gcc";
    #else
        static const char *cn = "unknown";
    #endif
     
    int main(void)
    {
        printf("%s\n", cn);
    #if defined(__GNUC__)
        printf("v %d.%d.%d\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
        #ifdef __STRICT_ANSI__
            printf("Strict ANSI\n");
        #endif
    #endif
    
        printf("File: %s\n", __FILE__);
    }
    for ideone, this does return a non-zero number and

    Code:
    gcc
    v 4.8.1
    prog.c
    Codepad still not working, but from the about page:
    Code:
     C: gcc 4.1.2 
    flags: -O -fmessage-length=0 -fno-merge-constants -fstrict-aliasing -fstack-protector-all
    Last edited by Hodor; 02-01-2014 at 07:20 PM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hodor View Post
    Section (5.1.2.2.3) (c99, c11). C89 doesn't seem to say the same thing, but I only glanced at it.
    C89 doesn't. In C89 (and in earlier versions of C predating that standard) the result of falling off the end of a function that returns a value is undefined.

    C++ made main() special, so falling off the end was equivalent to a "return 0". This was adopted in C99.


    This is actually one small area where fair number of C (post C89) and C++ compilers are technically non-compliant. If warning levels are turned up, a fair few compilers do warn about non-void functions (including main()) falling off the end though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't this print?
    By Matt Holden in forum C Programming
    Replies: 1
    Last Post: 01-21-2013, 03:04 PM
  2. No errors just doesn't do what its supposed to.
    By jxmuller in forum C++ Programming
    Replies: 30
    Last Post: 06-08-2006, 10:59 PM
  3. Supposed to return float but doesn't.
    By bliss in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2005, 12:49 PM
  4. Replies: 1
    Last Post: 04-02-2003, 07:50 PM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 08:30 AM