0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

c puzzle book basic type 1

Posted at
bash
$ gcc b51.c
bt1.c:11:24: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
   11 |         PRINT(d, string);           // ポインタを %d で表示 → 型不一致のため %p で表示
      |                  ~~~~~~^
      |                  |
      |                  char *
bt1.c:3:34: note: in definition of macro ‘PRINT’
    3 | #define PRINT(format, x) printf(#x " = %" #format "\n", x)
      |     
bt1.c
#include <stdio.h>

#define PRINT(format, x) printf(#x " = %" #format "\n", x)

int main()
{
	int integer = 5;
	char character = '5';       // ASCII 53
	char *string = "5";

	PRINT(p, string);           // ポインタを %d で表示 → 型不一致のため %p で表示 
	PRINT(d, character);        // character = 53
	PRINT(d, integer);          // integer = 5

	PRINT(s, string);           // string = 5
	PRINT(c, character);        // character = 5
	PRINT(c, integer = 53);     // integer = 5 (ASCII)

	PRINT(d, ('5' > 5));        // '5' (53) > 5 → 1

	int sx = -8;
	unsigned int ux = -8;

	PRINT(o, sx);               // sx = 37777777770(2の補数)
	PRINT(o, ux);               // ux = 37777777770(unsignedでも同じビット表現)

	PRINT(o, sx >> 3);          // 算術シフト(符号あり)
	PRINT(o, ux >> 3);          // 論理シフト(符号なし)

	PRINT(d, sx >> 3);
	PRINT(d, ux >> 3);

	return 0;
}

/*
nagai@compB05:~/Conpairu$ ./a.out
string = 0x564be7928004
character = 53
integer = 5
string = 5
character = 5
integer = 53 = 5
('5' > 5) = 1
sx = 37777777770
ux = 37777777770
sx >> 3 = 37777777777
ux >> 3 = 3777777777
sx >> 3 = -1
ux >> 3 = 536870911
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?