文字列リテラルの大きさを表示する

明解C言語 入門編 > 9. 文字列の基本 >

文字列リテラルの大きさを表示する

C


#include <stdio.h>


int main(int argc, char* argv[])
{
printf("sizeof(\"123\") = %u\n", (unsigned)sizeof("123"));
printf("sizeof(\"AB\\tC\") = %u\n", (unsigned)sizeof("AB\tC"));
printf("sizeof(\"abc\\0def\") = %u\n", (unsigned)sizeof("abc\0def"));

return 0;
}

実行結果


R:\>lesson068\Project1.exe
sizeof("123") = 4
sizeof("AB\tC") = 5
sizeof("abc\0def") = 8

Delphi


program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

procedure main();
begin
Writeln(Format('sizeof("123") = %d', [SizeOf('123')]));
Writeln(Format('sizeof("AB\tC") = %d', [SizeOf('AB'#9'C')]));
Writeln(Format('sizeof("abc\0def") = %d', [SizeOf('abc'#0'def')]));
Writeln('');

Writeln(Format('sizeof("123") = %d', [Length('123')]));
Writeln(Format('sizeof("AB\tC") = %d', [Length('AB'#9'C')]));
Writeln(Format('sizeof("abc\0def") = %d', [Length('abc'#0'def')]));
end;

begin
main;
end.

実行結果


S:\>lesson068\Project1.exe
sizeof("123") = 4
sizeof("AB\tC") = 4
sizeof("abc\0def") = 4

sizeof("123") = 3
sizeof("AB\tC") = 4
sizeof("abc\0def") = 7

Perl
$n = length("123");
print "length('123') = $n \n";

実行結果

L:\>perl lesson_09_068.pl
length('123') = 3

Ruby
n = "123".length
print "length('123') = #{n} \n"

n = "123".size
print "size('123') = #{n} \n"

実行結果

L:\>ruby l:\lesson_09_068.rb
length('123') = 3
size('123') = 3