|
C lang is easy to understand. Only what matters is the way you
see it.The majority of meaningful programs written in C
manipulate variables and constants. A variable is a symbolic
name for an object that changes or varies during a
program execution. In C, all variables must be declared
before they can be used. In other words, you should tell the
compiler to associate a type with each variable or constant
before its first use. let see the following program.
#include <stdio.h>
int main (void)
{
int m, d, c;
int const letter
= '69';
float x = 10;
float y = 3;
}
variables-
m, d, c, x, y, and the constant
-letter.
We also initialized the values of variables x
and z to 10 and 3 respectively, and the value of letter to 69. Declaration statement for a variable looks like
type_variable type_name;
To declare a constant you should
add the word const after or before the type of the constant.
The following declarations are identical.
variable_type
const variable_name;
const variable_type variable_name;
The most fundamental data types in C are:
-
integral types
-
floating types
-
arithmetic types
The integral types are designed for
working with the integer values. There are nine integral types
and their variations in C. They are designated by these
keywords:
-
char, signed char, unsigned char
-
short, int, long
-
unsigned short, unsigned int, unsigned
long
The data type int is the
principle working type of the C language. For example
int
num_of_students = 50;
Now you can perform arithmetic (and some
other) operations on this number. Let's assume that two
students dropped the class. The following example will
calculate and display the number 48, which is the number of
students left in the class (50 - 2).
int main (void)
{
int
num_of_students = 50;
int students_left
= 2;
num_of_students =
num_of_students - students_left;
printf ("%d,
num_of_students);
}
In the above example, the data type short can be used instead of int if memory storage
is of concern. You'll be able to keep track of less number of
students than with the int type but enough for
the whole group to count. In a similar fashion, the type long might be used in situations where larger integer values
are needed.
The char type is used to
represent characters. int and
char variables are used for the same purpose because
characters are stored in the computer as numbers. To
illustrate this idea let's take a look at the following code:
int i = 'a';
int k = 97;
printf ("%c", i);
/* a is printed */
printf ("%c", k );
/* a is printed */
Here the same letter "a" will be
printed twice because the integer value 97 corresponds to the
letter "a". Usage of %c
with printf() tells the computer to display a
character.
Finally, there are three floating types in
C:
|