sizeof vs. strlen enum

来源:互联网 发布:渔趣网淘宝分店 编辑:程序博客网 时间:2024/04/27 18:04
#include <stdio.h>#include <stdlib.h>#include <stdint.h>enum node_states {NODE_STATE_UNKNOWN,/* node's initial state, unknown */NODE_STATE_DOWN,/* node in non-usable state */NODE_STATE_IDLE,/* node idle and available for use */NODE_STATE_ALLOCATED,/* node has been allocated to a job */NODE_STATE_ERROR,/* node is in an error state */NODE_STATE_MIXED,/* node has a mixed state */NODE_STATE_FUTURE,/* node slot reserved for future use */NODE_STATE_END/* last entry in table */};void enum_test(){uint16_t getValue = 2;switch(getValue){case NODE_STATE_UNKNOWN:puts("NODE_STATE_UNKNOWN");break;case NODE_STATE_DOWN:puts("NODE_STATE_DOWN");break;case NODE_STATE_IDLE:puts("NODE_STATE_IDLE");break;default:puts("I'm tired...");break;}}void print_sizeof_strlen(){int array[] = {4, 5, 3, 1, 2};printf("number of int: %d\n", sizeof(array)/sizeof(int));struct student{int age;char name[20];}stu_array[5];printf("number of student:%d\n", sizeof(stu_array)/sizeof(struct student));char str[20]= "0123456789"; //strlen's function, its result is runtime valueprintf("strlen(str) = %d, sizeof(str) = %d\n", strlen(str), sizeof(str));//10, 20char *str2 = "0123456789";//sizeof(str2), sizeof(char*) is the str2's length, it's 8 bytesprintf("str2 = %d\n", str2);printf("sizeof(str2) = %d, sizeof(char*) = %d\n", sizeof(str2), sizeof(char*));printf("sizeof(*str2) = %d\n", sizeof(*str2));  //1 printf("strlen(str2) = %d\n", strlen(str2));}int main(void) {enum_test();print_sizeof_strlen();return EXIT_SUCCESS;}

原创粉丝点击