Some usage of sizeof

Discussion in 'C' started by wdliming, Jan 1, 2011.

  1. wdliming

    wdliming New Member

    Joined:
    Sep 26, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<string.h>
    char foo()
    {
    	printf("foo() has been called.\n");
    	return 'a';
    }
    /*void foo3(char a3[3])
    {
    	int c3 = sizeof( a3 ); // c3 == 
    	printf("c3=sizeof( a3 )=%d\n",c3);
    }
    void foo4(char a4[])
    {
    	int c4 = sizeof( a4 ); // c4 == 
    	printf("c4=sizeof( a4 )=%d\n",c4);
    }*/
    
    /*mian function*/
    int main()
    {
    	int n;
    	n = 10; 
    	char ary[n];		   
    	printf("sizeof(ary)       =%d\n", sizeof(ary)); // ok. output: 10
    	
    	size_t sz = sizeof( foo() );	// The type return value of the foo()is char,
    									//besides sz=sizeof(char),foo()Will not be called
    	printf("sizeof( foo() )   =%d\n", sz);
    	
    	/*****************************Data Type*****************************************************/
    	printf("sizeof(char)         =%d\n",sizeof(char));
    	printf("sizeof(signed char)  =%d\n",sizeof(signed char));
    	printf("sizeof(unsigned char)=%d\n",sizeof(unsigned char));
    	printf("sizeof(int)          =%d\n",sizeof(int));
    	printf("sizeof(signed int)   =%d\n",sizeof(signed int));
    	printf("sizeof(unsigned int) =%d\n",sizeof(unsigned int));
    	printf("sizeof(float)        =%d\n",sizeof(float));
    	printf("sizeof(double)       =%d\n",sizeof(double));
    	
    	/******************************point**********************************************************/
    	char *pc = "abc";
    	int *pi;
    	//string * ps;
    	char **ppc = &pc;
    	void (*pf)();// 
    	printf("sizeof(pc)          =%d\n",sizeof(pc)); //4
    	printf("sizeof(pi)          =%d\n",sizeof(pi)); //4
    	//sizeof( ps ); // 
    	printf("sizeof(ppc)         =%d\n",sizeof(ppc)); //4
    	printf("sizeof(pf)          =%d\n",sizeof(pf));// 4
    	
       /***********************Array*****************************************************************/ 
    	char a1[] = "abc";
    	int a2[3];
    	printf("sizeof(a1[])     =%d\n",sizeof(a1));// 4
    	
    	printf("sizeof(a2[])     =%d\n",sizeof(a2[3]));//3*4=12(attach to int)
    	
    	/*Calculate the numbers of the array*/
    	
    	int c1; //  Total length / length of a single element
    	printf("c1= sizeof( a1 ) / sizeof(char) =%d\n",c1= sizeof( a1 ) / sizeof( char )); 
    	int c2;//   Total length / length of the first element
    	printf("c2= sizeof( a1 ) / sizeof(a1[0])=%d\n",c2 = sizeof( a1 ) / sizeof( a1[0] ));
    	//printf("c4=sizeof( a2 )=%d\n",foo4(a2));
    	
    	/******************************Structure*****************************************************/
    	struct S1
    	{
    		char c;
    		int i;
    	};
    	printf("sizeof(S1)=%d\n",sizeof(struct S1));
    	struct S2
    	{
    		int i;
    		char c;
    	};
    	printf("sizeof(S2)=%d\n",sizeof(struct S2));
    	struct S3
    	{
    		char c1;
    		S1 s;
    		char c2;
    	};
    	printf("sizeof(S3)=%d\n",sizeof(struct S3));
    	
    	/*************************************Bit field****************************************************/
    	struct BF1
    	{
    		char f1 : 3;
    		char f2 : 4;
    		char f3 : 5;
    	};
    	printf("sizeof(BF1)=%d\n",sizeof(struct BF1));
    	union U
    	{
    		int i;
    		char c;
    		S1 s;
    	};
    	printf("sizeof(U)=%d\n",sizeof(U));
    	return 0;
    }
    Welcome to give some constructive suggestions!
    Thank you!
     
    Last edited by a moderator: Jan 2, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Please use code tags when posting code. It preserves the formatting.

    Also please read the posting guidelines.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    As of Now I have done it for you and so read the link in the reasons about what is code blocks and how you can use them
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice