C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Positive And Negative Numbers in C Programming Language

In this tutorial, we will learn about working with positive and negative numbers in C programming language, including how to represent them, store them in variables, and perform arithmetic operations on them.

Representation of positive and negative numbers:

In C programming language, positive and negative integers are represented using the two's complement system. This system allows for easy arithmetic operations and efficient storage of both positive and negative numbers.

Storing positive and negative numbers in variables:

To store positive and negative numbers in C, you can use signed integer data types, such as int, short, and long. These types can hold both positive and negative values, with a specific range depending on the size of the data type.

For example, to declare and initialize an integer variable with a negative value:

int num = -42;

Performing arithmetic operations on positive and negative numbers:

You can perform arithmetic operations on positive and negative numbers in C, such as addition, subtraction, multiplication, and division, using the standard arithmetic operators (+, -, *, /). The operations will work as expected, taking into account the sign of the numbers involved.

#include <stdio.h>

int main() {
    int num1 = 10;
    int num2 = -5;

    int sum = num1 + num2;
    int difference = num1 - num2;
    int product = num1 * num2;
    int quotient = num1 / num2;

    printf("Sum: %d\nDifference: %d\nProduct: %d\nQuotient: %d\n",
           sum, difference, product, quotient);
    return 0;
}

Output:

Sum: 5
Difference: 15
Product: -50
Quotient: -2

Modulus operation on positive and negative numbers:

The modulus operation (%) can also be used with negative numbers in C. However, the sign of the result depends on the implementation, as the C standard does not dictate whether the result should have the same sign as the dividend or the divisor. In most implementations, the result will have the same sign as the dividend.

#include <stdio.h>

int main() {
    int num1 = 7;
    int num2 = -3;

    int modulus1 = num1 % num2;
    int modulus2 = -num1 % num2;

    printf("Modulus 1: %d\nModulus 2: %d\n", modulus1, modulus2);
    return 0;
}

Output:

Modulus 1: 1
Modulus 2: -1

In summary, working with positive and negative numbers in C programming language involves using signed integer data types (such as int, short, and long) and standard arithmetic operators for performing calculations. The two's complement system is used for the representation of positive and negative numbers, allowing for efficient storage and arithmetic operations.

  1. Data Types for Representing Positive and Negative Values in C:

    In C, the primary data types for representing integers are int, short, long, etc., and for floating-point numbers, it's float, double, and long double.

    #include <stdio.h>
    
    int main() {
        int integerNumber = 42;
        float floatingPointNumber = -3.14;
    
        printf("Integer: %d\n", integerNumber);
        printf("Floating Point: %.2f\n", floatingPointNumber);
    
        return 0;
    }
    

    Output:

    Integer: 42
    Floating Point: -3.14
    
  2. Using Signed and Unsigned Integers in C Language:

    Signed integers can represent positive and negative values, while unsigned integers represent only non-negative values.

    #include <stdio.h>
    
    int main() {
        signed int signedInteger = -10;
        unsigned int unsignedInteger = 20;
    
        printf("Signed Integer: %d\n", signedInteger);
        printf("Unsigned Integer: %u\n", unsignedInteger);
    
        return 0;
    }
    

    Output:

    Signed Integer: -10
    Unsigned Integer: 20
    

    signed is optional for int and is commonly omitted.

  3. Representation of Positive and Negative Integers in Binary:

    Positive integers are represented in binary as usual, while negative integers use Two's complement representation.

    #include <stdio.h>
    
    int main() {
        int positiveNumber = 42;
        int negativeNumber = -42;
    
        printf("Positive Binary: %d in binary is 0b%b\n", positiveNumber, positiveNumber);
        printf("Negative Binary: %d in binary is 0b%b\n", negativeNumber, negativeNumber);
    
        return 0;
    }
    

    Output:

    Positive Binary: 42 in binary is 0b101010
    Negative Binary: -42 in binary is 0b11111111111111111111111111010110
    
  4. Arithmetic Operations with Positive and Negative Numbers in C:

    #include <stdio.h>
    
    int main() {
        int a = 10, b = -5;
    
        printf("Sum: %d\n", a + b);
        printf("Difference: %d\n", a - b);
        printf("Product: %d\n", a * b);
        printf("Quotient: %d\n", a / b);
    
        return 0;
    }
    

    Output:

    Sum: 5
    Difference: 15
    Product: -50
    Quotient: -2
    

    Operations with positive and negative numbers yield expected results.

  5. Handling Overflow and Underflow with Integer Types in C Programming:

    Be cautious with large numbers; overflow can occur, leading to unexpected results.

    #include <stdio.h>
    #include <limits.h>
    
    int main() {
        int maxInt = INT_MAX;
        int minInt = INT_MIN;
    
        printf("Max Int: %d\n", maxInt);
        printf("Min Int: %d\n", minInt);
    
        printf("Overflow: %d\n", maxInt + 1);
        printf("Underflow: %d\n", minInt - 1);
    
        return 0;
    }
    

    Output:

    Max Int: 2147483647
    Min Int: -2147483648
    Overflow: -2147483648
    Underflow: 2147483647
    

    Overflow or underflow results in wrapping around the range.

  6. C Code Examples Demonstrating Positive and Negative Number Manipulation:

    #include <stdio.h>
    
    int main() {
        int positive = 10;
        int negative = -5;
    
        printf("Absolute Value: %d\n", positive > 0 ? positive : -positive);
        printf("Negation: %d\n", -negative);
    
        return 0;
    }
    

    Output:

    Absolute Value: 10
    Negation: 5
    

    Manipulate positive and negative numbers using conditional expressions and negation.

  7. Conversion Between Positive and Negative Numbers in C:

    Use casting to convert between signed and unsigned integers.

    #include <stdio.h>
    
    int main() {
        int positiveNumber = 42;
        unsigned int converted = (unsigned int)positiveNumber;
    
        printf("Converted to Unsigned: %u\n", converted);
    
        return 0;
    }
    

    Output:

    Converted to Unsigned: 42
    

    Be cautious with loss of information during conversion.

  8. Floating-Point Numbers and Sign Representation in C:

    Floating-point numbers use the IEEE 754 standard, with a sign bit for the sign representation.

    #include <stdio.h>
    
    int main() {
        float positiveFloat = 3.14;
        float negativeFloat = -2.718;
    
        printf("Positive Float: %.2f\n", positiveFloat);
        printf("Negative Float: %.3f\n", negativeFloat);
    
        return 0;
    }
    

    Output:

    Positive Float: 3.14
    Negative Float: -2.718
    

    The sign bit in the floating-point representation indicates the sign.