article

Data Types In C

Email
Submitted on: 1/1/2015 2:22:00 PM
By: Indee (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, C++ (general)
Views: 878
 
     As I discussed in an earlier article, I am writing a series of articles explaining C for beginners. I am very careful to avoid the use of technical jargon so that it is simpler for beginners. And also I will be writing very simple codes and then explain about it. If you are an expert please skip this article.

 
				




Data Types in C



DATA TYPES IN C


Introduction

As I discussed in an earlier article, I am writing a series of articles explaining C for beginners. I am very careful to avoid the use of technical jargon so that it is simpler for beginners. And also I will be writing very simple codes and then explain about it. If you are an expert please skip this article.

Ok lets start with the real business. Here is another sample code for you.


Another Example of C Program

#Include < stdio.h >

/* Sample Program - feet to metres. */

void main(void)

{
    int feet;
    float metres;
   printf("Enter number of feet: ");
   scanf("%d", &feet);
   metres = feet * 0.3048; /* feet to metre conversion */
   printf ("%d feet is %f metres\n", feet, metres);

}


Explanation

Some important new concepts are introduced. First, two variables are declared:feet is an integer and metres is of type float, which means that it can have a fractional component.

The library function scanf() is used to read an integer entered at the keyboard. The %d in the first argument causes scanf() to read an integer and to place the results in the variable that follows. The & in front of feet is necessary for scanf() to work properly.

The next statement converts the number of feet to metres. Notice that even though feet is an integer, it may be divided by floating-point number and assigned to a floating-pont variable. C allows different types of data to be used in an expresssion.

The conversion is displayed using a call to printf(). In this statement, printf() takes three arguments: the control string, and the variables feet and metres. The general rule for printf() is that there are as many arguments following the control string as there are format codes in the control string. Since there are two format codes, two additional arguments are needed. These arguments are matched in order, from left to right, with the format codes. If you look closely, you will notice that %f is used to print metres, not %d. This is because printf() must know precisely what type of data it is going to display.

One limitations to this sample program is that it can convert only whole numbers of feet into metres. A more flexible program would also be able to convert floating-point values into integers. That I will leave it to you guys to think and convert this program. Ok!!

Choosing Data Types

There are six basic data types in C:

            chararcter
            short integer
            long integer
            floting point
            double floting point

The keywords used to declare variables of these types are char, short,, int, long, float and double.

The following table summarises the sizes for the C data types on an IBM PC or compatible.

      
TypeNumber of Bits
char 8
short 16
int 32
long 32
float 32
double 64

The int, short and long types are signed, with one bit used to indicate the sign. An unsigned integer has the full number of bits available to store the number itself. If storage is concern, you can take advantage of the size differences among the types.

Type char may be used for very small values (0 to 255) because it requires less storage space - 8 bits only rather than 16 bits for short or 32 bits for type int. For example ASCII ( American Standard Code for Information Interchange) codes are usually stored in char variables. Type char may be used for true/false and yes/no situations, where true or yes is represented by a value 1, and false or no is represented by a value of 0. Finallly, type char is used to store character strings.

Floating-point variables are used to store real numbers (numbers involving decimals). The above table showed type float provides 32 bits while type double provides 64 bits. Size is not the only factor when choosing between type float and type double. You must also consider the precision required. Type float provides approx. 7 digits of precision while double provides 15 digits of precision.(Precision is the number of digits storef before the value is rounded off.)

All types of variables are declared in the same way. Simply enter the variable type followed by one or more variables names and semi-colon. The following are examples:

char keypress;
int length, width, area;
long distance;
float average;
double mass;

A variables can be initialise by giving it an initial value. Constants are often used for initialisation. An integer constant is any number without a decimal point or an exponent. A character constant must be enclosed be enclosed by single quotes unless it is part of a string of characters encloed by double quotes. Types int, char and long can all be initialised with character constants.

A floating point constant is a signed series of digits including a decimal p oint and/or an E oe e(for exponent) followed by a signed exponent indicating the power of 10 to be used. It cannot contains spaces. Below are two formats that can be used to initialise a variable with a constant:

int kilograms = 21;

or

int kilograms;
kilograms = 21;

Be sure the constants used meet the various type requirements.

Ok guys! thats enough for this tutorial next tutorial will be about Characters And Strings.

Bye for now!


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.