473,491 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What does this Code Mean

1 New Member
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     printf("%d, %d\n", sizeof(NULL), sizeof(""));
  6.     return 0;
  7. }
Apr 19 '11 #1
4 2083
Meetee
931 Recognized Expert Moderator Contributor
What do you want us to explain?

If you are not clear with output then sizeof("") is equivalent to sizeof(char) where char value = 0, so it would be 1. However, sizeof(NULL) is implementation defined. So if size(int) then 4.
Apr 19 '11 #2
Banfa
9,065 Recognized Expert Moderator Expert
Unfortunately Meetee you are not quite right.

The question to ask is what is the type of "" and what is the type of NULL.

Let's start with NULL it is easier. NULL is defined in C++ to 0 which is where I assume you got it as equivalent to int. That would be correct for C++ but this is a C program, note use of printf and headers.

In C NULL is normally defined to ((void*)0), that is 0 cast to a void pointer. This works in C because C will automatically convert a void pointer to any other pointer type. C++ wont do this which is why it uses plan 0.

So for this program sizeof(NULL) == sizeof(void *) which for a 32 bit system might very well also be sizeof(int).

Onto "". So what is "", it is a string constant, that means it is an array of characters to which the compiler automatically adds a zero terminator '\0', something like
Expand|Select|Wrap|Line Numbers
  1. [const] char __Name__[1] = { '\0' };
  2.  
Note that the const is a bit platform dependent but generally it is best practice to assume it is there.

What type would __Name__? The name of an array variable...

char * of course as any other array variable name used in isolation.

so sizeof("") == sizeof(char*) not sizeof(char).
Apr 19 '11 #3
Meetee
931 Recognized Expert Moderator Contributor
Yeah... thanks Ban for heads up .
Apr 19 '11 #4
donbock
2,426 Recognized Expert Top Contributor
sizeof() returns a value of type size_t, which might be bigger than int. You can be pretty sure that these particular sizeof calls will return values that fit in an int, so it might be safest and most portable to cast the sizeof's to insure the values are consistent with the format specifier "%d".
Expand|Select|Wrap|Line Numbers
  1.     printf("%d, %d\n", (int)sizeof(NULL), (int)sizeof(""));
It could be argued that this suggestion is overly paranoid. The function prototype for printf is
Expand|Select|Wrap|Line Numbers
  1. int printf(const char *fmt, ...);
The prototype does not tell the compiler what types to expect after the format string, so the default integer conversions will be used. That is, all integer values are automatically cast to int anyway. Personally, I prefer not to rely on my recollection of such arcane behavior.
Apr 19 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1933
by: Abhish | last post by:
HI All ,:) I am a VC++ programmer,and some time My <b>Acumen</b> ask Microsofts VC++ (Visual Studio VC++ 6.0 )complier to complile my <b>senseless programs </b> ! :) <i><b> See what I have...
3
2041
by: Yang Zhang | last post by:
Here is a program: ///////////////////////////////////////////////// #include <iostream> using namespace std ; class A { int a ; A(const A& aA) { a=aA.a ; cout<<"copy constructor...
8
2024
by: code break | last post by:
Can Any one tell me why this program is crashing . testFunc() { int a1, *ptr ; f=&a1; f=f+1; return 0; }
24
1763
by: muttaa | last post by:
i've a code snippet below: for(;0;) printf("Gud Morning"); when this is run,i get the "gud morning" message printed,eventhough the
11
1880
by: kartheeknagasuri | last post by:
my friend gave this code to me..... it is working fine....how come? please explain me if u can understand..... #include <stdio.h> #include <conio.h> #define _(__,___)\ ___##__
2
1415
by: Shana | last post by:
#include<iostream.h> class Room { int length; int width; public: Room() {} Room(int l,int w=0) : width(w),length(l)
6
2592
by: khajeddin | last post by:
this program print its own source code without opening the source file. would some one please explain me how does it work ,and what what process takes place in this program???? #include <stdio.h>...
2
1193
by: gayathriram | last post by:
class test { static void show(params object b) { console.WriteLine(b.GetType().FullName); console.WriteLine(b.length); console.WriteLine(b); }...
1
1065
by: ankitamca85 | last post by:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; ...
1
1407
rampraveen
by: rampraveen | last post by:
****************************************************** program for sharing memory ****************************************************** #include"stdio.h" #include"conio.h" #include"file12.c"...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.