Advertisement
shiftdot515

sizesplus.sh

Apr 13th, 2020
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #!/sbin/mksh
  2. set -x
  3. #rm -i a.out
  4. gcc ${@} -x c - <<'EOF' && ./a.out
  5.  
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <limits.h>
  9. #include <sys/types.h>
  10.  
  11.  
  12. #define psizeof(tn) (printf("Size of " #tn " is %d\n",(sizeof (tn))))
  13.  
  14.  
  15. int f1(char * cp)
  16. {
  17.   return 1;
  18.  
  19. }
  20. double f2(int x, int y, double dx, int * ip)
  21. {
  22.   return 0.0;
  23.  
  24. }
  25.  
  26.  
  27. int main ( int argc, char * argv[] )
  28. {
  29.   printf("Size of CHAR_BIT is %d\n",CHAR_BIT);
  30.   psizeof(int);
  31.   psizeof(short);
  32.   psizeof(char);
  33.   psizeof(unsigned char);
  34.   psizeof(long);
  35.   psizeof(long long);
  36.   psizeof(double);
  37.   psizeof(float);
  38.   psizeof(long double);
  39.   psizeof(intmax_t);
  40.   psizeof(int *);
  41.   psizeof(long *);
  42.   psizeof(float *);
  43.   psizeof(double *);
  44.   psizeof(&f1);
  45.   psizeof(&f2);
  46.   psizeof(off_t);
  47.   psizeof(quad_t);
  48.   psizeof(size_t);
  49.   return 0;
  50. }
  51.  
  52.  
  53.  
  54. EOF
  55.  
  56. #now shifttype.c */
  57. gcc ${@} -x c - <<'EOF' && ./a.out
  58. #include <stdio.h>
  59.  
  60. int main(int argc, char * argv[])
  61. {
  62.   signed int i=-1;
  63.   signed int j;
  64.   puts("*************************");
  65.   printf("i=%d\n",i);
  66.   j= i>>1 ; /* right shift */
  67.   printf("after right shift 1=%d, %X hex\n",j,j);
  68.   if ( -1 == j ) { /* unspesified in C */
  69.     /* arithmetic shift, sign bit kept */
  70.     puts("right shift, >> , is arithmetic, keeps sign bit");
  71.   } else {
  72.     puts("right shift, >> , is logical");
  73.   }
  74.   puts("*************************");
  75.   unsigned int x=0;   /* strangely, this works */
  76.   unsigned int y;     /* c99 feature? gcc feature?,
  77.                        * I don't need -x c, for this to work, when gcc==
  78.                        * gcc version 4.0.1 (Apple Computer, Inc. build 5370)
  79.                       */
  80.   printf("x=%d\n",x);
  81.   y=x<<1;
  82.   printf("after left shift, <<, y=x<<1, %d, %X hex\n",y,y);
  83.   puts("*************************");
  84.   x=1;
  85.   printf("x=%d\n",x);
  86.   y=x<<3;
  87.   printf("after left shift, << ,y=x<<3, %d, %X hex\n",y,y);
  88.   puts("*************************");
  89. }
  90. /*
  91. Checking spelling of ARITHMETIC...
  92. ARITHMETIC is correct
  93. * kinda rhymes with Arabic
  94. * Why do I feel arithmetic shift, when not part of a, ones-complement
  95. * dinosaur, was only there should the programmer should'nt use it?
  96. * Was it essential to provide, no way, to determine ones-complement?
  97. * with a C program? Since Architecture is Compiler + machine,
  98. * was C a scientific success, when, some company, did that?
  99. * No, not that, what I was thinking, naturally, essentially,
  100. * make the dinosaur live again, with the compiler...
  101. */
  102. EOF
  103. #rm -i a.out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement