如何保存typeof的结果?

时间:2017-02-14 09:32:49

标签: c c99 typeof

我是一名新的程序员,主要使用Code :: Blocks for C 99 。 我最近发现了typeof(),因为它被隐藏为__typeof __() 我想知道你是否可以通过typeof保存类型。类似的东西:

type a = __typeof__(?); 

或者

#define typeof __typeof__
type a = typeof(?);

这可能吗?

2 个答案:

答案 0 :(得分:2)

您应该避免使用<!DOCTYPE html> <html> <head> <style> #map { height: 100%; } html, body { height: 100%; margin: 0; padding: 0; } #floating-panel { position: absolute; top: 60px; left: 5px; z-index: 5; background-color: transparent; padding: 5px; border: none; text-align: center; font-family: 'Roboto','sans-serif'; line-height: 30px; padding-left: 10px; } </style> </head> <body> <div id="floating-panel"> <button onclick="toggleHeatmap()">Toggle Heatmap</button><br> <button onclick="changeGradient()">Change gradient</button><br> <button onclick="changeRadius()">Change radius</button><br> <button onclick="changeOpacity()">Change opacity</button><br> </div> <div id="map"></div> <script> var map; function initMap() { map = new google.maps.Map(document.getElementById('map'), { zoom: 6, center: new google.maps.LatLng(54.378051, -3.435973), mapTypeId: 'terrain' }); var script = document.createElement('script'); script.src = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp'; document.getElementsByTagName('head')[0].appendChild(script); } function eqfeed_callback(results) { var heatmapData = []; for (var i = 0; i < results.features.length; i++) { var coords = results.features[i].geometry.coordinates; var latLng = new google.maps.LatLng(coords[1], coords[0]); heatmapData.push(latLng); } var heatmap = new google.maps.visualization.HeatmapLayer({ data: heatmapData, dissipating: true, map: map }); function toggleHeatmap() { heatmap.setMap(heatmap.getMap() ? null : map); } function changeGradient() { var gradient = [ 'rgba(0, 255, 255, 0)', 'rgba(0, 255, 255, 1)', 'rgba(0, 191, 255, 1)', 'rgba(0, 127, 255, 1)', 'rgba(0, 63, 255, 1)', 'rgba(0, 0, 255, 1)', 'rgba(0, 0, 223, 1)', 'rgba(0, 0, 191, 1)', 'rgba(0, 0, 159, 1)', 'rgba(0, 0, 127, 1)', 'rgba(63, 0, 91, 1)', 'rgba(127, 0, 63, 1)', 'rgba(191, 0, 31, 1)', 'rgba(255, 0, 0, 1)' ] heatmap.set('gradient', heatmap.get('gradient') ? null : gradient); } function changeRadius() { heatmap.set('radius', heatmap.get('radius') ? null : 20); } function changeOpacity() { heatmap.set('opacity', heatmap.get('opacity') ? null : 0.2); } } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization&callback=initMap"> </script> </body> </html> typeof,因为它们不是标准C.最新的C版本(C11)通过__typeof __()关键字支持此功能同样的方式。

没有&#34;类型&#34;在C中你可以自己轻松制作一个:

_Generic

答案 1 :(得分:1)

不,您不能使用typeoft = (typeof(x) == int) ? a : b;int t = typeof(x);

如果您在C11下,_Generic可以提供帮助:

#include <stdio.h>

enum {TYPE_UNKNOWN, TYPE_INT, TYPE_CHAR, TYPE_DOUBLE};

#define type_of(T) _Generic((T), int: TYPE_INT, char: TYPE_CHAR, double: TYPE_DOUBLE, default: 0)

int main(void)
{
    double a = 5.;
    int t = type_of(a);

    switch (t) {
        case TYPE_INT:
            puts("a is int");
            break;
        case TYPE_CHAR:
            puts("a is char");
            break;
        case TYPE_DOUBLE:
            puts("a is double");
            break;
        default:
            puts("a is unknown");
            break;
    }
    return 0;
}