Home arrow C++ arrow Page 5 - First Steps in (C) Programming, conclusion
C++

First Steps in (C) Programming, conclusion


If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the third of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 63
December 01, 2005
TABLE OF CONTENTS:
  1. · First Steps in (C) Programming, conclusion
  2. · More Numeric Data Types
  3. · Try It Out: Character Building
  4. · Unsigned Integers: Using Positive Integers
  5. · Try It Out: Finding the Limits
  6. · Mathematical Functions
  7. · Designing a Program
  8. · The Solution
  9. · Summary

print this article
SEARCH DEVARTICLES

First Steps in (C) Programming, conclusion - Try It Out: Finding the Limits
(Page 5 of 9 )

This program just outputs the values corresponding to the symbols defined in the header files:

/* Program 2.15 Finding the limits  */ #include <stdio.h>
#include < limits.h >  /* For limits on integer types  */
#include <float.h>     /* For limits on floating-point types */
void main()
{
  printf("Variables of type char store values from %d to %d",
           CHAR_MIN, CHAR_MAX); 
  printf("\nVariables of type unsigned char store values from 0 to %u",
             UCHAR_MAX); 
  printf("\nVariables of type short store values from %d to %d",
       SHRT_MIN, SHRT_MAX);
  printf("\nVariables of type unsigned short store values from 0 to %u",
            USHRT_MAX);
  printf("\nVariables of type int store values from %d to %d", INT_MIN, INT_MAX); 
  printf("\nVariables of type unsigned int store values from 0 to %u", UINT_MAX); 
  printf("\nVariables of type long store values from %d to %d",
          
LONG_MIN, LONG_MAX);
  printf("\nVariables of type unsigned long store values from 0 to %u",
           ULONG_MAX);
  printf("\n\nThe size of the smallest non-zero value of type float is %.3e",
             
FLT_MIN);
  printf("\nThe size of the largest value of type float is %.3e", FLT_MAX);
  printf("\nThe size of the smallest non-zero value of type double is %.3e",
              DBL_MIN);
  printf("\nThe size of the largest value of type double is %.3e", DBL_MAX);
  printf("\nThe size of the smallest non-zero value of type long double is %.3e",
             
DBL_MIN);
 
printf("\nThe size of the largest value of type long double is %.3e\n",
          DBL_MAX);
}

Youíll get output somewhat similar to the following:

--------------------------------------------
Variables of type char store values from -128 to 127
Variables of type unsigned char store values from 0 to 255
Variables of type short store values from -32768 to 32767
Variables of type unsigned short store values from 0 to 65535
Variables of type int store values from -2147483648 to 2147483647
Variables of type unsigned int store values from 0 to 4294967295
Variables of type long store values from -2147483648 to 2147483647
Variables of type unsigned long store values from 0 to 4294967295
The magnitude of the smallest non-zero value of type float is 1.175e-038
The magnitude of the largest value of type float is 3.403e+038
The magnitude of the smallest non-zero value of type double is 2.225e-308
The magnitude of the largest value of type double is 1.798e+308
The magnitude of the smallest non-zero value of type long double is 2.225e-308
The magnitude of the largest value of type long double is 1.798e+308
--------------------------------------------

HOW IT WORKS

You just output the values of the symbols in a series ofprintf()function calls. You have used the%uspecifier to output the unsigned integer values. If you use%dfor the maximum value of an unsigned type, values that have the leftmost bit (the sign bit for signed types) as 1 wonít be interpreted correctly.

You use the%especifier for the floating-point limits, which presents the values in exponential form. You also specify just three digits precision, as you donít need the full accuracy in the output. The%fspecifier presents values without an exponent, so itís rather inconvenient for very large or very small values. If you try it in the example, youíll see what I mean.

.........................................................................................

Introducing the sizeof Operator

You can find out how many bytes are occupied by a given type by using thesizeofoperator. Of course,sizeofis a keyword in C. The expressionsizeof(int)will result in the number of bytes occupied by a variable of typeint. Thesizeof operator has other uses too, but for the moment letís just use it to find out how many bytes are occupied by each type.

Try It Out: Discovering the Number of Bytes Occupied by a Given Type

This program will output the number of bytes occupied by each numeric type:

/* Program 2.16 Finding the size of a type */
#include <stdio.h>
void main()
{
  printf("\nVariables of type char occupy %d bytes", sizeof(char));
  printf("\nVariables of type short occupy %d bytes", sizeof(short));
  printf("\nVariables of type int occupy %d bytes", sizeof(int));
  printf("\nVariables of type long occupy %d bytes", sizeof(long));
  printf("\nVariables of type float occupy %d bytes", sizeof(float));
  printf("\nVariables of type double occupy %d bytes", sizeof(double));
  printf("\nVariables of type long double occupy %d bytes", sizeof(long double));
}

On my system I get the following output:

--------------------------------------------Variables of type char occupy 1 bytes Variables of type short occupy 2 bytes Variables of type int occupy 4 bytes Variables of type long occupy 4 bytes Variables of type float occupy 4 bytes Variables of type double occupy 8 bytes Variables of type long double occupy 8 bytes
--------------------------------------------

HOW IT WORKS

Because thesizeofoperator results in an integer value, you can output it using the%dspecifier. Note that you can also obtain the number of bytes occupied by avariable,var_name, with the expressionsizeof var_name. Obviously, the space between thesizeofkeyword and the variable name in the expression is essential.

Now you know the range limits and the number of bytes occupied by each numeric type with your compiler.

..........................................................................................

The op= Form of Assignment

C is fundamentally a very concise language, so it provides you with abbreviated shortcuts for some operations. Consider the following line of code:

number = number + 10;

This sort of assignment, in which youíre incrementing or decrementing a variable by some amount, occurs very often, so thereís a shorthand version:

number += 10;

The+=operator after the variable name is one example of a family ofop=operators. This statement has exactly the same effect as the previous one and it saves a bit of typing. Theopinop=can be any of the following arithmetic operators:

+ - * / %

If you supposenumber has the value 10, then you can write the following statements:

number *= 3;      /* number will be set to number*3 which is 30 */
number /= 3;      /* number will be set to number/3 which is 3  */
number %= 3;      /* number will be set to number%3 which is 1  */

Theopinop=can also be a few other operators that you havenít encountered yet:

<< >> & ^ | 

Iíll defer discussion of these to Chapter 3, however.

Theop=set of operators always works in the same way. If you have a statement of the form

lhs op= rhs;

then the effect is the same as a statement of the form

lhs = lhs op (rhs);

Note the parentheses around therhsexpression. This means thatopapplies to the value that results from evaluating the entirerhsexpression, whatever it is. So just to reinforce your understanding of this, letís look at few more examples. The statement

variable *= 12;

is the same as

variable = variable * 12;

You now have two different ways of incrementing an integer variable by 1. Both of the following statements increment count by 1:

count = count+1;
count += 1;

Youíll learn about yet another way of doing this in the next chapter. This amazing level of choice tends to make it virtually impossible for indecisive individuals to write programs in C.

Because theop inop=applies to the result of evaluating therhsexpression, the statement

a /= b+1;

is the same as

a = a/(b+1);

Your computational facilities have been somewhat constrained so far. Youíve been able to use only a very basic set of arithmetic operators. You can get more power to your calculating elbow using standard library facilities, so before you come to the final example in this chapter, youíll take a look at some of the mathematical functions that the standard library offers.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials