Thread: Behavior of symbolic name different in assignment and printf

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

    Behavior of symbolic name different in assignment and printf

    (/EDIT
    I'm sorry, I've just spotted a mistake in the code - I left a point in the macro. I'm searching for a way to delete the thread but I can't seem to find one. To moderators: feel free to delete it and sorry for posting.)


    Hi all, this is my first post (and for a question like this I felt like searching a C forum, rather than posting in such places as StackOverflow).

    I'm playing with C while reading a manual (K&R, 2nd edition).

    I'm at symbolic names, i.e. the #define directive.
    I know that names are mostly used with constant values and expressions, but I've just read in the manual one line saying it can replace the name with any sequence of characters, so I was checking just that (Code::Blocks IDE, gcc compiler).

    First example (works as expected)

    Here I'm defining a name for a "piece" of operation, the multiplication by 3.
    It works as expected, assigning 6 (actually, replacing with "2 * 3" and hence assigning 6) to a and then printing its value of 6.

    Code:
    #include <stdio.h>
    
    #define MY_PART * 3.
    
    
    main()
    {
        int a = 2 MY_PART;
    
    
        printf("%d", a);
    }
    Second Example (doesn't work as expected)

    If I use, though, the name as an argument to the printf function, outputting an integer doesn't seem to perform the expected multiplication (2 * 3).
    Even odder (at least to me), outputting a float does.

    Code:
    #include <stdio.h>
    
    #define MY_PART * 3.
    
    
    main()
    {
        printf("%d\n", a MY_PART); // prints 0 (?)
    
        printf("%f\n", a MY_PART); // prints 6
    }
    Why the integer printing doesn't work? And why the float one does?

    Thanks.
    Last edited by atava; 01-30-2021 at 02:04 AM. Reason: Found a mistake in code, the very questions asked are not valid

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,616
    > Why the integer printing doesn't work? And why the float one does?
    Let's look at this first.

    > int a = 2 MY_PART;
    The calculation is done as a floating point calculation, but the assignment forces the result back to an integer.

    > printf("%d\n", a MY_PART); // prints 0 (?)
    > printf("%f\n", a MY_PART); // prints 6
    It's still a floating point calculation, but there is no assignment to force truncation back to integer.
    The %d will just see a bunch of bits of a floating point (actually double), do it's best to interpret those as an integer and fail (giving you zero).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected Printf Behavior
    By John... in forum C Programming
    Replies: 4
    Last Post: 02-04-2016, 11:24 PM
  2. Odd printf behavior?
    By tallen387 in forum C Programming
    Replies: 4
    Last Post: 04-10-2011, 08:35 AM
  3. Odd printf behavior?
    By tallen387 in forum C Programming
    Replies: 1
    Last Post: 04-07-2011, 07:03 AM
  4. printf() vs puts() behavior
    By Overworked_PhD in forum C Programming
    Replies: 7
    Last Post: 05-30-2008, 12:43 AM
  5. Behavior of printf
    By sewilli in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 01:40 AM

Tags for this Thread