"함수 type()"의 두 판 사이의 차이

 
(사용자 3명의 중간 판 48개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{다른뜻|리눅스 type}}
{{다른뜻|리눅스 type}}
[[category: type]]
[[category: 자료형]]
==개요==
;.getClass()
;.getClass()
;gettype
;gettype
6번째 줄: 7번째 줄:
;typeof
;typeof
;.class
;.class
* [[자료형]]을 확인하는 함수


==C==
==C==
[[분류: C]]
[[분류: C]]
{{참고|C gettype()}}
{{참고|C type()}}
<source lang='c'>
<syntaxhighlight lang='c' run>
#include <stdio.h>
#include <stdio.h>


#define gettype(x) _Generic((x), /* Get the name of a type */                    \
#define type(x) _Generic((x),                                                     \
         _Bool: "_Bool",                  unsigned char: "unsigned char",          \
         _Bool: "_Bool",                  unsigned char: "unsigned char",          \
         char: "char",                    signed char: "signed char",            \
         char: "char",                    signed char: "signed char",            \
21번째 줄: 23번째 줄:
long long int: "long long int", unsigned long long int: "unsigned long long int", \
long long int: "long long int", unsigned long long int: "unsigned long long int", \
         float: "float",                        double: "double",                \
         float: "float",                        double: "double",                \
   long double: "long double",                  char *: "pointer to char",       \
   long double: "long double",                  char *: "char *",                 \
       void *: "pointer to void",               int *: "pointer to int",         \
       void *: "void *",                         int *: "int *",                 \
       default: "other")
       default: "unknown")


int main() {
int main() {
     char ch;
     char ch;
     int a;
     int   a;
     long b;
     long b;
     char* str;
     char* str;
     printf( "%s\n", gettype(ch) ); // char
    char  str2[5];
     printf( "%s\n", gettype(a) ); // int
 
     printf( "%s\n", gettype(b) ); // long int
     printf( "%s\n", type(ch)   ); // char
     printf( "%s\n", gettype(str) ); // pointer to char
     printf( "%s\n", type(a)   ); // int
     return 0;
     printf( "%s\n", type(b)   ); // long int
     printf( "%s\n", type(str) ); // char *
     printf( "%s\n", type(str2) ); // char *
}
</syntaxhighlight>
 
==Go==
[[분류: Go]]
{{참고|Go TypeOf()}}
<syntaxhighlight lang='go' run>
package main
 
import (
    "fmt"
    "reflect"
)
 
func main() {
    fmt.Println(reflect.TypeOf(123))    // int
    fmt.Println(reflect.TypeOf(123.45))  // float64
    fmt.Println(reflect.TypeOf("hello")) // string
   
    fmt.Println(reflect.TypeOf(true))    // bool
    fmt.Println(reflect.TypeOf(false))  // bool
    fmt.Println(reflect.TypeOf(nil))    // <nil>
}
}
</source>
</syntaxhighlight>


==Java==
==Java==
[[분류: Java]]
{{참고|자바 getClass()}}
{{참고|자바 getClass()}}
<source lang='java'>
<syntaxhighlight lang='java' run>
System.out.println( "hello".getClass() );
import java.util.ArrayList;
// class java.lang.String
import java.util.HashMap;
class MyClass{public static void main(String[] args){
System.out.println( "hello".getClass() ); // class java.lang.String


Object obj1 = new ArrayList<Object>();
Object obj1 = new ArrayList<Object>();
Object obj2 = new HashMap<String,Object>();
Object obj2 = new HashMap<String,Object>();
System.out.println( obj1.getClass() );
System.out.println( obj1.getClass() );     // class java.util.ArrayList
System.out.println( obj2.getClass() );
System.out.println( obj2.getClass() );     // class java.util.HashMap
// class java.util.ArrayList
// class java.util.HashMap


System.out.println( obj1.getClass() == ArrayList.class );
System.out.println( obj1.getClass() == ArrayList.class ); // true
System.out.println( obj1.getClass() == HashMap.class );
System.out.println( obj1.getClass() == HashMap.class   ); // false
System.out.println( obj2.getClass() == ArrayList.class );
System.out.println( obj2.getClass() == ArrayList.class ); // false
System.out.println( obj2.getClass() == HashMap.class );
System.out.println( obj2.getClass() == HashMap.class   ); // true
// true
}}
// false
</syntaxhighlight>
// false
// true
</source>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='JavaScript'>
{{참고|JavaScript typeof}}
console.log( typeof [] ); // object
<syntaxhighlight lang='JavaScript' run>
console.log( typeof {} ); // object
console.log( typeof [] );           // object
console.log( typeof 42 ); // number
console.log( typeof {} );           // object
console.log( typeof "hello" ); // string
console.log( typeof 42 );           // number
console.log( typeof function(){} ); // function
console.log( typeof "hello" );       // string
console.log( typeof function(){} ); // function


var func = function() {};
var func = function() {};
console.log( typeof func ); // function
console.log( typeof func );         // function
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
{{참고| PHP gettype()}}
{{참고| PHP gettype()}}
<source lang='php'>
<syntaxhighlight lang='php' run>
echo gettype(123); // integer
echo gettype(123) ."\n"; # integer
echo gettype('123'); // string
echo gettype('123')."\n"; # string
echo gettype(array()); // array
echo gettype([])   ."\n"; # array
</source>
</syntaxhighlight>


==Python==
==Python==
87번째 줄: 112번째 줄:
{{참고|Python type()}}
{{참고|Python type()}}
{{소스헤더|Python 3}}
{{소스헤더|Python 3}}
<source lang='Python'>
<syntaxhighlight lang='Python' run>
print(type(123))
print(type(123))     # <class 'int'>
print(type('123'))
print(type('123'))   # <class 'str'>
print(type({}))
print(type({}))     # <class 'dict'>
print(type([]))
print(type([]))     # <class 'list'>
# <class 'int'>
# <class 'str'>
# <class 'dict'>
# <class 'list'>


print(type('안녕'))
print(type('안녕'))   # <class 'str'>
print(type(u'안녕'))
print(type(u'안녕')) # <class 'str'>
# <class 'str'>
</syntaxhighlight>
# <class 'str'>
</source>
:→ Python 3는 항상 str = unicode
:→ Python 3는 항상 str = unicode


{{소스헤더|Python 2}}
{{소스헤더|Python 2}}
<source lang='Python'>
<syntaxhighlight lang='Python'>
print(type(123))
print(type(123))     # <type 'int'>
print(type('123'))
print(type('123'))   # <type 'str'>
print(type({}))
print(type({}))     # <type 'dict'>
print(type([]))
print(type([]))     # <type 'list'>
# <type 'int'>
 
# <type 'str'>
print(type('안녕'))  # <type 'str'>
# <type 'dict'>
print(type(u'안녕'))  # <type 'unicode'>
# <type 'list'>
</syntaxhighlight>
 
==Perl==
[[category: Perl]]
<syntaxhighlight lang='perl' run>
use Scalar::Util qw(reftype);
my $x = bless {}, 'Wiki::ZetaWiki';
print "type of x: "      . ref($x)    . "\n";  # type of x: Wiki::ZetaWiki
print "base type of x: " . reftype($x) . "\n";  # base type of x: HASH
</syntaxhighlight>


print(type('안녕'))
==R==
print(type(u'안녕'))
[[분류: R]]
# <type 'str'>
{{참고|R언어 class()}}
# <type 'unicode'>
<syntaxhighlight lang='r' run>
</source>
class(42)             #[1] "numeric"
class(TRUE)           #[1] "logical"
class("Hello World")  #[1] "character"
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang='Ruby'>
{{참고|Ruby .class}}
puts "123".class
<syntaxhighlight lang='Ruby' run>
# String
puts "123".class         # String
puts 123.class
puts 123.class           # Integer
# Fixnum
puts 123.45.class        # Float
puts [].class
 
# Array
puts [].class           # Array
print :hello.class
puts :hello.class       # Symbol
# Symbol
puts Proc.new {}.class   # Proc
print Proc.new {}.class
</syntaxhighlight>
# Proc
</source>


==같이 보기==
==같이 보기==
* [[자료형]]
* [[함수 get_class()]]
* [[함수 get_class()]]
* [[함수 is_object()]]
* [[함수 is_object()]]

2022년 5월 18일 (수) 19:57 기준 최신판

  다른 뜻에 대해서는 리눅스 type 문서를 참조하십시오.

1 개요[ | ]

.getClass()
gettype
type
typeof
.class

2 C[ | ]

#include <stdio.h>

#define type(x) _Generic((x),                                                     \
        _Bool: "_Bool",                  unsigned char: "unsigned char",          \
         char: "char",                     signed char: "signed char",            \
    short int: "short int",         unsigned short int: "unsigned short int",     \
          int: "int",                     unsigned int: "unsigned int",           \
     long int: "long int",           unsigned long int: "unsigned long int",      \
long long int: "long long int", unsigned long long int: "unsigned long long int", \
        float: "float",                         double: "double",                 \
  long double: "long double",                   char *: "char *",                 \
       void *: "void *",                         int *: "int *",                  \
      default: "unknown")

int main() {
    char  ch;
    int   a;
    long  b;
    char* str;
    char  str2[5];

    printf( "%s\n", type(ch)   ); // char
    printf( "%s\n", type(a)    ); // int
    printf( "%s\n", type(b)    ); // long int
    printf( "%s\n", type(str)  ); // char *
    printf( "%s\n", type(str2) ); // char *
}

3 Go[ | ]

package main

import (
    "fmt"
    "reflect"
)

func main() {
    fmt.Println(reflect.TypeOf(123))     // int
    fmt.Println(reflect.TypeOf(123.45))  // float64
    fmt.Println(reflect.TypeOf("hello")) // string
    
    fmt.Println(reflect.TypeOf(true))    // bool
    fmt.Println(reflect.TypeOf(false))   // bool
    fmt.Println(reflect.TypeOf(nil))     // <nil>
}

4 Java[ | ]

import java.util.ArrayList;
import java.util.HashMap;
class MyClass{public static void main(String[] args){
	System.out.println( "hello".getClass() );  // class java.lang.String

	Object obj1 = new ArrayList<Object>();
	Object obj2 = new HashMap<String,Object>();
	System.out.println( obj1.getClass() );     // class java.util.ArrayList
	System.out.println( obj2.getClass() );     // class java.util.HashMap

	System.out.println( obj1.getClass() == ArrayList.class );  // true
	System.out.println( obj1.getClass() == HashMap.class   );  // false
	System.out.println( obj2.getClass() == ArrayList.class );  // false
	System.out.println( obj2.getClass() == HashMap.class   );  // true
}}

5 JavaScript[ | ]

console.log( typeof [] );            // object
console.log( typeof {} );            // object
console.log( typeof 42 );            // number
console.log( typeof "hello" );       // string
console.log( typeof function(){} );  // function

var func = function() {};
console.log( typeof func );          // function

6 PHP[ | ]

echo gettype(123)  ."\n";  # integer
echo gettype('123')."\n";  # string
echo gettype([])   ."\n";  # array

7 Python[ | ]

Python 3
print(type(123))     # <class 'int'>
print(type('123'))   # <class 'str'>
print(type({}))      # <class 'dict'>
print(type([]))      # <class 'list'>

print(type('안녕'))   # <class 'str'>
print(type(u'안녕'))  # <class 'str'>
→ Python 3는 항상 str = unicode
Python 2
print(type(123))     # <type 'int'>
print(type('123'))   # <type 'str'>
print(type({}))      # <type 'dict'>
print(type([]))      # <type 'list'>

print(type('안녕'))   # <type 'str'>
print(type(u'안녕'))  # <type 'unicode'>

8 Perl[ | ]

use Scalar::Util qw(reftype);
my $x = bless {}, 'Wiki::ZetaWiki';
print "type of x: "      . ref($x)     . "\n";  # type of x: Wiki::ZetaWiki
print "base type of x: " . reftype($x) . "\n";  # base type of x: HASH

9 R[ | ]

class(42)             #[1] "numeric"
class(TRUE)           #[1] "logical"
class("Hello World")  #[1] "character"

10 Ruby[ | ]

puts "123".class         # String
puts 123.class           # Integer
puts 123.45.class        # Float

puts [].class            # Array
puts :hello.class        # Symbol
puts Proc.new {}.class   # Proc

11 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}