473,657 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C formatted Output using printf( Confused)

1 New Member
hi group,

I am new to C and using gcc compiler in Linux.

I am having the following problems when using the printf() funtion to have the formatted output.

Here r some things which i m trying :

The range of short int(which is signed by default) is -32768 to + 32767.

short i = 32767;
printf(" %d\n" , i);
It prints 32767. Good. At this point i was assuming that if I give variable i, the value greater than its range, it won't accept and it will give me an overflow message.

But when i assigned i(type short) the value 32768 or +32768 which is 1 more than its range, it didn't give me an error , rather it gave me the value with a -sign. -32768. And when i print it with %u conversion specifier (to omit the -ve sign)in printf , it gives me strange output which is 4294934528.

And when i assign the value 32769(2 more than the range of short) to i with the %d specifer, it goes in minus as well as the value changes to - 32767.

I really have no idea what is happening and why data types like short, int, accept more ranges than they are supposed to.

Please if anybody can clear these confusions or give me the web-site where i can read some clear explanations about this. I m referring K&R(second edition) and it does not give any clear explanation about this.

thanks.
Jul 21 '07 #1
2 2042
ravenspoint
111 New Member
Do you not get a compiler warning?

MSVC gives a warning on:

Expand|Select|Wrap|Line Numbers
  1.     short  s;
  2.     s = 32769;
  3.     printf("%d\n",s);
  4.  
Jul 21 '07 #2
ilikepython
844 Recognized Expert Contributor
hi group,

I am new to C and using gcc compiler in Linux.

I am having the following problems when using the printf() funtion to have the formatted output.

Here r some things which i m trying :

The range of short int(which is signed by default) is -32768 to + 32767.

short i = 32767;
printf(" %d\n" , i);
It prints 32767. Good. At this point i was assuming that if I give variable i, the value greater than its range, it won't accept and it will give me an overflow message.

But when i assigned i(type short) the value 32768 or +32768 which is 1 more than its range, it didn't give me an error , rather it gave me the value with a -sign. -32768. And when i print it with %u conversion specifier (to omit the -ve sign)in printf , it gives me strange output which is 4294934528.

And when i assign the value 32769(2 more than the range of short) to i with the %d specifer, it goes in minus as well as the value changes to - 32767.

I really have no idea what is happening and why data types like short, int, accept more ranges than they are supposed to.

Please if anybody can clear these confusions or give me the web-site where i can read some clear explanations about this. I m referring K&R(second edition) and it does not give any clear explanation about this.

thanks.
The variable overflows which means it goes back to its minimum value, which in your case is -32768:
Expand|Select|Wrap|Line Numbers
  1. short x = 32767;
  2. x++;                             // x = 32768
  3. x++;                             // x = -32768
  4. x++;                             // x = -32767
  5. x++;                             // x = -32766
  6.  
Jul 21 '07 #3

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

Similar topics

2
1693
by: Kevin C. | last post by:
Can someone explain why the file output produces all zeros? It seems to work fine in memory (e.g. passing char pointers to printf) but when I output the file, it comes out as zeros. Bookkeeping seems correct (counts, sizes, etc.) and the output file is the correct size, just all zeros. cd_entry *src = (cd_entry*)(tpd + tpd->cd_offset); cd_entry *dest = (cd_entry*)(cur + cur->cd_offset); for(int i = 0; i < tpd->num_columns; i++) {
9
2239
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to the first int and &arr is a pointer to the whole array of 10 ints. Then why does sizeof(arr) gives 40 while sizeof(&arr) gives 4. Shouldn't is be the other way around.
12
1672
by: unique | last post by:
I have written some programs in c lang yet but today I get confused with output i get. I have function educate(..) which i call in main program this way: J = educate(no_layers, no_neurons, input, output, lay, weights, 0.1,0); printf("J= %f\n",J); In the educate function i count J and i return it (but before I return it I output it with printf):
1
8501
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
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 we have to send another system

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.