473,396 Members | 1,892 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,396 software developers and data experts.

printf errors

Hi there,
I have to write simple programm and test what heppens when I incorrectly identify types for output.

for example:
main(){
int a=5;
printf("value is %f",a);
}
outcome of the print statement is 0.000000 instead of 5. why?

- if use %d instead of %f
outcome of the print statement is -1610612736 instead of 5.567, why?

- if use %c instead of %d
outcome of the print statement is (sign) instead of 5. why?

-if use %d instead of %c
outcome of the print statement is 97 instead of "a". why?

- if use % instead of %s
outcome of the print statement is 2686746 instead of "Sean". why?

- if use %s instead of %d
the program stop working.

I know all types have a different lenght and location but would to know why the computer interpretation is wrong.

Thank you very much for help
Oct 7 '11 #1
4 2442
Banfa
9,065 Expert Mod 8TB
It looks like you need to read up on printf format strings.

Basically if you supply 1 data type but tell printf it is another data type then the data, whatever it might be will be interpreted as that type.

For example you supply the integer 5 and ask printf to interpret it as a float. The binary representation of int 5 is

0000 0000 0000 0000 0000 0000 0000 0101

When this binary representation is interpreted as a float then the float has a value of 0.0

The actual binary representation of 5.0f is

0100 0000 1010 0000 0000 0000 0000 0000

The bits are very different as you can see.

A similar thing happens for all the other examples you give. You supply data in 1 format and ask for it to be interpreted as another format without converting the data so the interpretation gives a result that you aren't expecting.
Oct 7 '11 #2
donbock
2,426 Expert 2GB
What is the meaning of this binary value?
0110 0001 0100 1101 0101 1010 0100 1100
  • interpreted as a hexadecimal number, it is 0x614D5A4C
  • interpreted as an unsigned 32-bit integer, it is 1,632,459,340
  • interpreted as unsigned 16-bit integers, it is 24,909 and 23,116
  • interpreted as ASCII characters is it aMZL
  • interpreted as EBCDIC characters it is /(!<
  • interpreted as floating point it is ... something else
  • interpreted as an instruction it is ... something else (depending on which processor it is for)
  • ...
  • and so on

Don't think of that binary number as a value, think of it as a code. What it truly means depends on which encoding system you use.

In your printf experiments you create a binary number based on one encoding system (int) and then interpret that value based on other encoding systems (as per the format specifier). You shouldn't be surprised that the meaning varies.

What do you think of when I say "Nova"?
It might be a new star;
It might be an old car;
In Spanish, it means "it does not go".
Oct 7 '11 #3
Thank you guys very much.
It will help.

I am waiting for other explanation, also.
Thanks
Kris
Oct 7 '11 #4
donbock
2,426 Expert 2GB
What else do you need an explanation for?
Oct 9 '11 #5

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

Similar topics

9
by: zolli | last post by:
Hi, I've been banging my head against this for a while now. Hoping someone here can shed some light on what's going on. On including stdlib.h in a file, I'm seeing the following errors: ...
6
by: ermanu | last post by:
Hello we don't know much about C. But we have to resolve this problem in order to work on our project. We're getting below errors whenever we try to build the code. We're trying to build the code...
2
by: jocobunshin | last post by:
Hi there! I'm new in Turbo C... uhmm our teacher gave us a project to make a program in Turbo C. Im from the Philippines, and our project is due on tuesday, februrary 13, 2007... can anyone check my...
2
by: menyki | last post by:
error c: ......line 3: unable to open include file 'stdio.h error c: ....... line 4: unable to open include file 'stdlib.h' error c: ........line 5: unable to open include file 'time.h' error c:...
1
by: menyki | last post by:
I HV DONE ALL THE CORRECTIONS. IS ONLY THE HEADER FILE ERRORS THAT ARE STILL REMAINING.I GUESS IS FROM COMPILER, I HV DONE ALL U SAID I SHOULD DO WITH HEATHER FILE BUT STILL IS NOT WORKING.MAY BE MY...
1
by: patilanjana | last post by:
Hi, I am getting above mentioned errors. Checked msdn, read the comments but I fail to implement it. Please help. Error is regarding using the new and delete operators. Although I do write...
4
by: momov4 | last post by:
I can't seem to resolve these errors. I have had people compile these codes on their computers with no problem, but I keep getting errors...can anyone help???? Here are the errors: cannot find...
3
by: thalinx | last post by:
Hi can ayone help me with this program, cause i dont know how to solve the compiling errors here. thanks # include<stdlib.h> # include<conio.h> # include<stdio.h> # define MAXCADENA 8 # define...
1
by: epilogue | last post by:
Hey guys Im pretty new to Java and while I am finding it enjoyable i am getting several errors!!! Do you think you could help me on this particular question of an Assignment im doing. I have to...
1
by: Oltmans | last post by:
Hi all, I've been writing C# code using Visual Studio 2005 for some time now but for the first time I've to write ANSI C code using Visual Studio 2005. I followed this article...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.