main(){ short i = -1; printf(“%d,%o,%x,%u\n“, i, i, i, i); }

代码如下:

#include <stdio.h>
main(){
	short i = -1;
	printf("%d,%o,%x,%u\n", i, i, i, i);
}

1.-1用8进制或16进制表示都是-1;

2.但在计算机内部,占用1字节空间的情况下(字节型),-1的2进制表示为:原码[-1]=10000001;反码为:[-1]=11111110;补码为[-1]=11111111.而计算机内部数值都是以补码的形式进行标识,若以8进制表示就是3ff;16进制为ff 若将-1看成整型,那么-1的补码为1111111111111111;8进制为177777;16进制为ffff

3.所以short i=-1
原码:1000 0000 0000 0001
反码:1111 1111 1111 1110(符号位不变其余按位取反)
补码:1111 1111 1111 1111(反码+1)

八进制:1 111 111 111 111 111 (177777)
十六进制:1111 1111 1111 1111 (ffff)

dec=-1;oct=177777;hex=ffff;unsigned=2^16-1=65535;

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实验报告5 "题目"数据类型 " "目的" " "要求"深入理解C语言中数据类型的意义。 " " " " " "测试不同类型数值数据的存储空间大小、取值范围i、有效数字位数和精度。 " " "编写一个测试基本数据类型所占内存宽度的程序。 " "程序"该程序由函数头和函数体组成,运用printf()函数输出各基本数据类型所占内 " "说明"存宽度。 " " "编写程序来测试基本数据类型的取值的范围。 " " "该程序由函数头和函数体组成,定义不同数据类型变量并赋值,测试其取值范 " " "围。 " " "编写程序来测试数据类型的有效数字长度和精度。 " " "该程序由函数头和函数体组成,用printf()函数。 " " "编写程序来测试不同类型数据之间转换所出现的截去高位、丢失精度和变得不 " " "可知等问题。 " " "该程序由函数头和函数体组成,用赋值实现不同数据类型之间的转换,用print" " "f()函数输出测试。 " " "编写程序来测试字符型数据的算术特征。 " " "实现不同数据类型之间的转换,用printf()函数。 " " "编写程序来测试转义字符的用法。 " " "实现不同数据类型之间的转换,用printf()函数实现对转义字符用法的测试。 " " " " " "1)编写一个测试基本数据类型所占内存宽度的程序。 " "经调"#include<stdio.h> " "试的"int main(void) " "程序"{ " "清单"printf("char:%d bytes.\n",sizeof(char)); " " "printf("short:%d bytes.\n",sizeof(short)); " " "printf("int:%d bytes.\n",sizeof(int)); " " "printf("long:%d bytes.\n",sizeof(long int)); " " "printf("float:%d bytes.\n",sizeof(float)); " " "printf("double:%d bytes.\n",sizeof(double)); " " "printf("long double:%d bytes.\n",sizeof(long double)); " " "return 0; " " "} " " "2)编写程序来测试基本数据类型的取值的范围。 " " " " " "int main(void) " " "{ " " "char c0,c1,c2,c3,c4,c5; " " "c0=-129; " " "c1=-128; " " "c2=-127; " " "c3=128; " " "c4=127; " " "c5=126; " " "printf("c0=%d\n",c0); " " "printf("c1=%d\n",c1); " " "printf("c2=%d\n",c2); " " "printf("c3=%d\n",c3); " " "printf("c4=%d\n",c4); " " "printf("c5=%d\n",c5); " " " " " "} " " " " " "编写程序来测试数据类型的有效数字长度和精度。 " " "测试double类型的有效数字长度和精度如下: " " "int main(void) " " "{ " " "float d1,d2; " " "d1=0.123456789; " " "d2=1234567891234567890.123456789; " " "printf("%lf\n%lf\n",d1,d2); " " "} " " " " " "4)编写程序来测试不同类型数据之间转换所出现的截去高位、丢失精度和变得" " "不可知等问题。 " " "#include<stdio.h> " " "int main(void) " " "{ " " "double a=123456.7890987654; " " "float b; " " "long c; " " "short i,j; " " "b=a;c=a;i=a;j=c; " " "printf("a=%f, b=%f, c=%d, c=%d, i=%d, j=%d\n",a,b,c,i,j); " " "return 0; " " "} " " " " " "编写程序来测试字符型数据的算术特征。 " " "#include<stdio.h> " " "int main(void) " " "{ " " "char c1,c2,c3; " " "c1=35; " " "c2='A'; " " "c3=c1+c2; " " "printf("%d,
实验报告三数据的表示 ———————————————————————————————— 作者: ———————————————————————————————— 日期: 计算机系统基础实验报告 学院 信电学院 专业 计算机科学与技术 班级 计算机1401 学号 140210110 姓名 段登赢 实验时间: 1. 实验名称:数据的表示2 2. 实验目的和要求: (1)实验目的:熟悉数值数据在计算机内部的表示方式,掌握相关的处理语句。 (2)实验要求:说明你做实验的过程(重要步骤用屏幕截图表示);提交源程序;分析 并回答问题。 3. 实验环境(软、硬件): 要求:详细描述实验用的操作系统,源代码编辑软件,相关硬件环境及所使用的GCC 编译器的信息。 4. 实验内容: (1)请说明下列赋值语句执行后,各个变量对应的机器数和真值各是多少?编写一段程 序代码并进行编译,观察默认情况下,编译器是否报warning。如果有warning信息的话 ,分析为何会出现这种warning信息。 int a = 2147483648; int b = -2147483648; int c = 2147483649; unsigned short d = 65539; short e = -32790; (2)完成书上第二章习题中第40题,提交代码,并在程序中以十六进制形式打印变量u 的机器数。 (3)编译运行以下程序,并至少重复运行3次。 void main() { double x=23.001, y=24.001, z=1.0; for (int i=0; i<10; i++) { if ((y-x)==z) printf("equal\n"); else printf("not equal\n"); x += z; y += z; printf("%d, %f , %f\n", i, x, y); } } 要求: (1)给出每次运行的结果截图。 (2)每次运行过程中,是否每一次循环中的判等结果都一致?为什么? (3)每次运行过程中,每一次循环输出的i、x和y的结果分别是什么?为什么? 5. 实验结果及分析: (1) 实验分析: 机器数就是数字在计算机中的二进制表示形式,其特点一是符号数字化,二是其数的大 小受机器字长的限制。将带符号位的机器数对应的真正数值称为机器数的真值 int a = 2147483648的机器数是1000 0000 0000 0000 0000 0000 0000 0000B int b = -2147483648的机器数是 1000 0000 0000 0000 0000 0000 0000 0000B int c = 2147483649的机器数是 1000 0000 0000 0000 0000 0000 0000 0001B unsigned short d = 65539的机器数无法表示 short e = -32790的机器数无法表示 int类型在32位计算机中占4个字节,即32位。又因为正数的补码是其本身,所以int 类型能表示的所有正数为:0,000 0000 0000 0000 0000 0000 0000 0000B到0,111 1111 1111 1111 1111 1111 1111 1111B即0到+2147483647。而负数的补码是除符号位外各位取反最后加一而来。所以int 类型所能表示的所有负数为:0,000 0000 0000 0000 0000 0000 0000 0000B(- 0D的补码)到1,111 1111 1111 1111 1111 1111 1111 1111B即0到- 2147483647D。而32位二进制数能表示的所有值为2的32次方个,而从- 2147483647D到+2147483647D总共是2的32次方减一个数,而少的这个数就是1000 0000 0000 0000 0000 0000 0000 0000B(- 0D的补码),而任何数的原码都不能在转换成补码时成为这个数,我们人为的把他规定 为-2147483648所以int 类型的取值范围为- 2147483648到+2147483647,所以题目中的b是正确的,并且不会发生溢出。而题目中的 a=2147483648其实已经超出int类型的最大范围,但是a=2147483648=2147483647+1=0,1 11 1111 1111 1111 1111 1111 1111 1111B+1B=1000 0000 0000 0000 0000 0000 0000 0000B= -2147483648,而- 2147483648又在int类型的取值范围内,所以也不会溢出,如果此时打印输出a的十进制 就是-2147
/* * linux/init/main.c * * (C) 1991 Linus Torvalds */ #define __LIBRARY__ #include <unistd.h> #include <time.h> /* * we need this inline - forking from kernel space will result * in NO COPY ON WRITE (!!!), until an execve is executed. This * is no problem, but for the stack. This is handled by not letting * main() use the stack at all after fork(). Thus, no function * calls - which means inline code for fork too, as otherwise we * would use the stack upon exit from 'fork()'. * * Actually only pause and fork are needed inline, so that there * won't be any messing with the stack from main(), but we define * some others too. */ static inline _syscall0(int,fork) static inline _syscall0(int,pause) static inline _syscall1(int,setup,void *,BIOS) static inline _syscall0(int,sync) #include <linux/tty.h> #include <linux/sched.h> #include <linux/head.h> #include <asm/system.h> #include <asm/io.h> #include <stddef.h> #include <stdarg.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <linux/fs.h> static char printbuf[1024]; extern int vsprintf(); extern void init(void); extern void blk_dev_init(void); extern void chr_dev_init(void); extern void hd_init(void); extern void floppy_init(void); extern void mem_init(long start, long end); extern long rd_init(long mem_start, int length); extern long kernel_mktime(struct tm * tm); extern long startup_time; /* * This is set up by the setup-routine at boot-time */ #define EXT_MEM_K (*(unsigned short *)0x90002) #define DRIVE_INFO (*(struct drive_info *)0x90080) #define ORIG_ROOT_DEV (*(unsigned short *)0x901FC) /* * Yeah, yeah, it's ugly, but I cannot find how to do this correctly * and this seems to work. I anybody has more info on the real-time * clock I'd be interested. Most of this was trial and error, and some * bios-listing reading. Urghh. */ #define CMOS_READ(addr) ({ \ outb_p(0x80|addr,0x70); \ inb_p(0x71); \ }) #define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10) static void time_init(void) { struct tm time; do { time.tm_sec = CMOS_READ(0); time.tm_min = CMOS_READ(2); time.tm_hour = CMOS_READ(4); time.tm_mday = CMOS_READ(7); time.tm_mon = CMOS_READ(8); time.tm_year = CMOS_READ(9); } while (time.tm_sec != CMOS_READ(0)); BCD_TO_BIN(time.tm_sec); BCD_TO_BIN(time.tm_min); BCD_TO_BIN(time.tm_hour); BCD_TO_BIN(time.tm_mday); BCD_TO_BIN(time.tm_mon); BCD_TO_BIN(time.tm_year); time.tm_mon--; startup_time = kernel_mktime(&time); } static long memory_end = 0; static long buffer_memory_end = 0; static long main_memory_start = 0; struct drive_info { char dummy[32]; } drive_info; void main(void) /* This really IS void, no error here. */ { /* The startup routine assumes (well, ...) this */ /* * Interrupts are still disabled. Do necessary setups, then * enable them */ ROOT_DEV = ORIG_ROOT_DEV; drive_info = DRIVE_INFO; memory_end = (1<<20) + (EXT_MEM_K<<10); memory_end &= 0xfffff000; if (memory_end > 16*1024*1024) memory_end = 16*1024*1024; if (memory_end > 12*1024*1024) buffer_memory_end = 4*1024*1024; else if (memory_end > 6*1024*1024) buffer_memory_end = 2*1024*1024; else buffer_memory_end = 1*1024*1024; main_memory_start = buffer_memory_end; #ifdef RAMDISK main_memory_start += rd_init(main_memory_start, RAMDISK*1024); #endif mem_init(main_memory_start,memory_end); trap_init(); blk_dev_init(); chr_dev_init(); tty_init(); time_init(); sched_init(); buffer_init(buffer_memory_end); hd_init(); floppy_init(); sti(); move_to_user_mode(); if (!fork()) { /* we count on this going ok */ init(); } /* * NOTE!! For any other task 'pause()' would mean we have to get a * signal to awaken, but task0 is the sole exception (see 'schedule()') * as task 0 gets activated at every idle moment (when no other tasks * can run). For task0 'pause()' just means we go check if some other * task can run, and if not we return here. */ for(;;) pause(); } static int printf(const char *fmt, ...) { va_list args; int i; va_start(args, fmt); write(1,printbuf,i=vsprintf(printbuf, fmt, args)); va_end(args); return i; } static char * argv_rc[] = { "/bin/sh", NULL }; static char * envp_rc[] = { "HOME=/", NULL }; static char * argv[] = { "-/bin/sh",NULL }; static char * envp[] = { "HOME=/usr/root", NULL }; void init(void) { int pid,i; setup((void *) &drive_info); (void) open("/dev/tty0",O_RDWR,0); (void) dup(0); (void) dup(0); printf("%d buffers = %d bytes buffer space\n\r",NR_BUFFERS, NR_BUFFERS*BLOCK_SIZE); printf("Free mem: %d bytes\n\r",memory_end-main_memory_start); if (!(pid=fork())) { close(0); if (open("/etc/rc",O_RDONLY,0)) _exit(1); execve("/bin/sh",argv_rc,envp_rc); _exit(2); } if (pid>0) while (pid != wait(&i)) /* nothing */; while (1) { if ((pid=fork())<0) { printf("Fork failed in init\r\n"); continue; } if (!pid) { close(0);close(1);close(2); setsid(); (void) open("/dev/tty0",O_RDWR,0); (void) dup(0); (void) dup(0); _exit(execve("/bin/sh",argv,envp)); } while (1) if (pid == wait(&i)) break; printf("\n\rchild %d died with code %04x\n\r",pid,i); sync(); } _exit(0); /* NOTE! _exit, not exit() */ }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值