在C两个数的加法 [英] Addition of two numbers in c

查看:144
本文介绍了在C两个数的加法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何的可能性来读取和使用单变量添加两种或三种不同的整数(int类型的)C语言?

我不想使用数组


解决方案

我不知道我要救你,但举例来说,如果你想添加2 16位整数与32位单整数,你可以这样做

 的#include<&stdio.h中GT;
#包括LT&;&stdint.h GT;
#包括LT&;&inttypes.h GT;诠释的main()
{
    uint32_t的一个;    的printf(请输入数字1:);
    scanf函数(%HD(uint16_t *)(安培; A));    的printf(请输入数字2:);
    scanf的(%HD,((uint16_t *)(&放大器;一))+ 1);    的printf(%X \\ n,一);    的printf(总和=%PRIu32\\ N,(uint32_t的)(*(uint16_t *)(&放大器;一))+ *(((uint16_t *)(&放大器;一))+ 1));    返回0;
}

的逻辑来思考变量等于字节数组,仅此而已。

本实施仍然有被很好地解释这里

Is there any possibilities to read and add two or three different integers by using single variable ( int a ) in C language?

I didn't want to use array

解决方案

I'm not sure I'm getting you, but for example, if you want to add 2 16 bits integers with a single 32bit integer, you could do

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main()
{
    uint32_t a;

    printf("Enter number 1: ");
    scanf("%hd", (uint16_t *)(&a));

    printf("Enter number 2: ");
    scanf("%hd", ((uint16_t *)(&a))+1);

    printf("%X\n", a);

    printf("Sum = %"PRIu32"\n", (uint32_t)(*(uint16_t *)(&a)) + *(((uint16_t *)(&a)) + 1));

    return 0;
}

The logic is to think about variable equals to arrays of bytes, and that's it.

This implementation still have problems that are well explained HERE

这篇关于在C两个数的加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆