473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

printing an int in C

ok.. simple question here: if you're to print out an integer before giving it a value, it prints out some huge absurd value. Where is this value coming from?

For example, if I were to do:

int i;
print ("i = %d", i);

it would print out some huge value like 1073828704. where is this value coming from? I'm not printing out the address of the variable or anything, so...?
Jan 18 '08 #1
5 26784
gpraghuram
1,275 Expert 1GB
ok.. simple question here: if you're to print out an integer before giving it a value, it prints out some huge absurd value. Where is this value coming from?

For example, if I were to do:

int i;
print ("i = %d", i);

it would print out some huge value like 1073828704. where is this value coming from? I'm not printing out the address of the variable or anything, so...?

If the integer you are printing is inside a function then it is a auto variable and it contains always garbage as the variable gets loaded to stack every time the functions gets into the stack.
For global and static variables it will be default initialized to 0.

Raghuram
Jan 18 '08 #2
Ganon11
3,652 Expert 2GB
Basically, here's what happens when you say:

Expand|Select|Wrap|Line Numbers
  1. int x;
inside your C program. Your computer will see that you need space for an integer. Let's say you need 8 bits (1 byte) to hold an integer. The computer finds a place in main memory that can hold your integer x. Let's say it picks a position 13 of a byte to store x in. Memory position 13 might have had some random bits already stored there, such as:

0110 1101

When the computer allocates memory, it leaves the data there unchanged. It merely says, "This position (in this case, position 13) is now the value of x!" And it is so. I'll repeat; during allocation, the pre-existing data still remains.

When you print x before initializing it yourself, you get the previous value, which wouldn't make sense in the context of your program. It might be 109, as above, it might be 1073828704, it might be 9382174, it might be -3284721341, who knows?

It is this reason that you should always initialize your variables, either to some convenient constant (0 or 1), or immediately use input to assign a variable. Any way you can, get some value into your variable before you use it.

The simplest way to do this is by replacing the above code with:

Expand|Select|Wrap|Line Numbers
  1. int x = 0;
With this statement, the computer first allocates memory for 6 ("I declare position 42 to hold the value of x!"), and then assigns 0 to that memory location. Now, no matter what was previously held in position 42, the bits are now:

0000 0000
Jan 18 '08 #3
Thanks, make sense. But to follow up on this concept:

So for example, let's say we've got a local variable declared as an int (not yet assigned any value to it). So now we have 4 bytes allocated on the stack (on most machines). Actually let's assume it's an unsigned int. So I'm assuming whatever garbage value is in those 4 bytes before initialization must still be constrained to the 2^32 limit for the unsigned int, correct?
Jan 18 '08 #4
Ganon11
3,652 Expert 2GB
Correct. This is because, whatever garbage information is there, your computer will still interpret it as an unsigned int, because you told it that it would be an unsigned int. So the 'smallest' possible value is

0000 0000 0000 0000 0000 0000 0000 0000

a.k.a. 0, and the 'largest' possible value is

1111 1111 1111 1111 1111 1111 1111 1111

a.k.a. (2^32)-1.
Jan 18 '08 #5
Got it, thanks for the clarification
Jan 18 '08 #6

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

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
4
by: Suzanka | last post by:
Hello, I have an application written in C# on visual studio .NET. It is a web aplication. The application consists of many different forms, that users occassionaly want to print out for filing....
4
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
0
by: nikhilgargi | last post by:
Requirement: I need to provide printing capability in a C# desktop application that I am developing The documents that need to be printed can be in Rich Text Format (RTF) or HTML. Custom...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.