Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:12:17+00:00 2026-06-01T19:12:17+00:00

I was having some problems with a sample code i was testing, since my

  • 0

I was having some problems with a sample code i was testing, since my abs function was not returning the correct result. abs(-2) was outputing -2 (this, by the way, is suposed to be the absolute value function, if that was unclear)

After getting a bit desperate, i eventually had the following code

#include <stdio.h>

unsigned int abs(int x) {
    return 1;
}

int main() {
    printf("%d\n", abs(-2));
    return 0;
}

This does nothing useful but it serves to show my problem. This was outputing -2, when it was expected to output 1.

if i change the function name to something else (abs2 for example), the result is now correct. Also, if i change it to receive two arguments instead of one, it also fixes the problem.

My obvious guess: a conflict with the standart abs function. But this still doesn’t explain why the output is -2 (it should be 2, if using the standart abs function). I tried checking the assembly output of both versions (with the function named abs and abs2)

Here’s the diff output for both assemblys:

23,25c23,25
< .globl abs
<   .type   abs, @function
< abs:
---
> .globl abs2
>   .type   abs2, @function
> abs2:
54c54
<   .size   abs, .-abs
---
>   .size   abs2, .-abs2
71c71,74
<   movl    -4(%rbp), %edx
---
>   movl    -4(%rbp), %eax
>   movl    %eax, %edi
>   call    abs2
>   movl    %eax, %edx

From what i understand, the first version (where the function is named abs) is simply discarding the function call, thus using the parameter x instead of abs(x)

So to sum up: why does this happen, especially since i couldn’t find a way to get any sort of warning or error about this.

Tested on Debian Squeeze, ggc 4.4.5, and also on gcc 4.1.2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:12:18+00:00Added an answer on June 1, 2026 at 7:12 pm

    GCC is playing tricks on you due to the interplay of the following:

    • abs is a built-in function;
    • you’re declaring abs to return unsigned int while the standard (and built-in) abs returns signed int.

    Try compiling with gcc -fno-builtin; on my box, that gives the expected result of 1. Compiling without that option but with abs declared as returning signed int causes the program to print 2.

    (The real solution to this problem is to not use library identifiers for your own functions. Also note that you shouldn’t be printing an unsigned int with %d.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems with a datagridview element I'm using in VS2008. This DataGridView
I'm having some problems with the following code: private class ClientPluginLoader : MarshalByRefObject {
I seem to be having some problems creating a jquery plugin. I am testing
Here is some sample code to illustrate the problem I am having. #include <iostream>
Having some problems with a solution that apparently works: <script type=text/javascript > //following code
I'm having some slight problems with fading one div into another, here's my code
I am working through a KnockOutJs Sample and am having some posting problems with
I'm having a troubling problem testing some code for and Android app. My app
Im having some problems with a simple toggle in Safari. A really annoying flickering
My model is quite simple but I'm having some problems with the vector I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.