プログラミング言語C++のヘンテコな演算子。💘(U+1F498) #include <iostream> int main() { int n = 100; while ( 0 <-------------------- n) { // !? std::cout << n << ' '; } } 実行結果: 90 80 70 60 50 40 30 20 10 タネ明かし 前掲C++ソースコードの振る舞いを括弧を用いて明確化する。int型の変数nに対し前置デクリメント演算子を10回適用し、その結果を値0と比較している。C++では組込み前置デクリメント演算子の戻り値は左辺値(lvalue)となるため、該当ソースコードはwell-definedとなる。 int n = 100; while ( 0 < --(--(--(--(--(--(--(--(--(--n))))))))) ) { s
Why does the C preprocessor in GCC interpret the word linux (small letters) as the constant 1? test.c: #include <stdio.h> int main(void) { int linux = 5; return 0; } Result of $ gcc -E test.c (stop after the preprocessing stage): .... int main(void) { int 1 = 5; return 0; } Which of course yields an error. (BTW: There is no #define linux in the stdio.h file.)
普通のファイルを画像にしたり、その画像からファイルに戻したりしたら面白いだろうなぁ と思っていたので、今BMPにするものを書いてみました。 #include <stdio.h> #include <stdlib.h> #include <math.h> #include "bmp.h" main(int argc, char** argv) { FILE *fp; fpos_t sz; int v, cnt; if(argc < 2) { exit(1); } if((fp = fopen(argv[1],"r")) == NULL) { exit(1); } cnt = 0; //Get File size fseek( fp, 0, SEEK_END ); fgetpos( fp, &sz ); printf("file size: %d\n",sz); fseek( fp, 0, S
最近、Robert Love先生の本を暇な時にダラーと読んでいたりするわけですが、それの中にLinux Kernel内部で使われているLinked Listの実装が書いてあって面白かったので共有。 まず、Linked Listの一個一個のエントリを表すstructを定義します。 struct list_head { struct list_head *next, *prev; }; いやいやいやいや。いかにC力の低い僕でも流石にこれはあきません。騙されませんよ。前後のエントリへのポインタは確かにあるけれども、これにはデータを指すためのポインタがないじゃないの。おじいちゃんまたデータ忘れてきちゃったの?いやあねえ。 おじいちゃんは言った。「それはお前の短見というものじゃ。このLinked Listは以下のコードのようにデータ構造に埋め込んで使うものなんじゃよ。」そしてそれは正しかった。 st
変な結果が出るサンプル 以下の例では、二つのテストを行っています。 テストの中ではwww.yahoo.co.jpとwww.google.co.jpの両方に対してgethostbynameを連続的に行っています。 本当はwww.yahoo.co.jpとwww.google.co.jpの両方のIPアドレスが表示されて欲しいのですが、実際には片方しか表示されません。 #include <stdio.h> #include <winsock2.h> int main() { WSADATA wsaData; struct hostent *hostent1, *hostent2; struct in_addr inaddr1, inaddr2; WSAStartup(MAKEWORD(2,0), &wsaData); // // テスト1 hostent1 = gethostbyname("www
/* ** Example C+ Program */ #include "C+.h" int main(int argc, char** argv) { /* Stack objects are created using "$" */ var int_item = $(Int, 5); var float_item = $(Real, 2.4); var string_item = $(String, "Hello"); /* Heap objects are created using "new" */ var items = new(List, 3, int_item, float_item, string_item); /* Collections can be looped over */ foreach(items, item) { /* Types are also obj
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く