C copysign() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, copysign() builds a number with the size of x and the sign of y. copysign(3.5, -1.0) is -3.5.

You pass two double values. The result is a double. Include <math.h>. On GCC, link with -lm.

Syntax

Pass the magnitude, then the sign source. Start with this call.

C
copysign(3.5, -1.0);
CallWhat it does
copysign(3.5, -1.0)Keeps 3.5 as the size and applies a minus sign.
copysign(-2.0, 4.0)Keeps 2.0 as the size and applies a plus sign.

Include <math.h>. On GCC, link with -lm. The magnitude comes from x; the sign comes from y.

Library declarations
C
double copysign(double x, double y);
float copysignf(float x, float y);
long double copysignl(long double x, long double y);

Parameters

ParameterDescription
xRequired. The value that supplies the magnitude (absolute size).
yRequired. The value that supplies the sign bit.

Return Value

Return ValueDescription
doubleA value with |x| and the sign of y.

C Examples

Example 1 — Basic Sign Copy

Take a positive size and force a negative sign from another value.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", copysign(3.5, -1.0));
    printf("%.1f\n", copysign(-2.0, 4.0));
    return 0;
}

How It Works

  1. Include math.h. That declares copysign.
  2. First call. Size 3.5, sign from -1.0 → -3.5.
  3. Second call. Size 2.0, sign from 4.0 → 2.0.
  4. Result type. Return value is still double.

Example 2 — Signed Zero

On IEEE floating-point systems, +0.0 and -0.0 can differ only in the sign bit.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double a = copysign(0.0, -1.0);
    double b = copysign(0.0, 1.0);

    printf("a = %.1f, signbit = %d\n", a, signbit(a));
    printf("b = %.1f, signbit = %d\n", b, signbit(b));
    return 0;
}

How It Works

  1. Copy a minus onto zero. copysign(0.0, -1.0) can make -0.0.
  2. Copy a plus onto zero. copysign(0.0, 1.0) stays +0.0.
  3. Use signbit. Print how the sign bit is set.
  4. Why it matters. Some algorithms care about signed zero.

Example 3 — Force Sign From a Flag

Use +1.0 or -1.0 as a simple sign flag for a magnitude.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double mag = 12.5;
    double sign_flag = -1.0;
    double v = copysign(mag, sign_flag);

    printf("value = %.1f\n", v);
    return 0;
}

How It Works

  1. Pick a magnitude. mag is 12.5.
  2. Pick a sign flag. -1.0 means negative.
  3. Combine with copysign. Result is -12.5.
  4. Useful pattern. Keep size and direction in separate variables.

Frequently Asked Questions

copysign(x, y) returns a value with the magnitude of x and the sign of y.
x supplies the size. y supplies the sign. Both are double.
A double with |x| and the sign taken from y.
Include <math.h>. On GCC, link with -lm.
No. Multiplying flips the current sign. copysign sets the sign from y, even when x is already negative.

Next: cos()

Compute the cosine of a floating-point angle in radians.

cos() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful