LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2004, 08:57 AM   #1
vinods
LQ Newbie
 
Registered: Oct 2004
Location: Bangalore
Posts: 1

Rep: Reputation: 0
sizeof operator implementation


Dear Memebers,

I want to know how exactly the sizeof Operator is implemented in C?

In the thread id 236101 it is explained that the sizeof operator is implemented as follows:

#define sizeof(x) ((void *)(&x + 1) - (void *)(&x))

This will give the correct output when we pass a variable as a parameter, as explained in the following code

#include <stdio.h>
#define sizeof(x) ((void *)(&x + 1) - (void *)(&x))
void main()
{
int a;
printf("int size=%d",sizeof(a));
}
Output will be
int size=4



But it will fail to compile when we pass the data type as a parameter, as shown in the following code

#include <stdio.h>
#define sizeof(x) ((void *)(&x + 1) - (void *)(&x))
void main()
{
printf("int size=%d",sizeof(int));
}

And also as far as my knowledge goes the sizeof operator will be evaluated at the compile time and will be replaced by an integer value before the actual code generation. The above sizeof defination when works will give the value only at run time.

Someone please help me.

Thanks,

Regards,
Vinod.
 
Old 10-26-2004, 09:57 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
sizeof is a unary operator. It's not in a header file or library, it's part of the compiler just like - or ! are.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
when does sizeof ( ) operator return zero !! joeyBig Programming 3 10-01-2004 06:43 AM
How Sizeof Operator Implemented? hiren Linux - General 4 09-29-2004 05:12 AM
Sizeof Operator Implementation hiren Programming 4 09-28-2004 10:32 AM
Shell '>' operator implementation Lord CyKill Programming 1 10-14-2003 04:07 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration