For the complete list of the tutorial on this blog, you can check these following links:
1. Introduction
2. First C-Program
3. Types and Variables
4. Conditional Statements
5. Function and Math Operations
6. Arrays
7. Pointers
8. I/O in C
As we start working on every programming languages, we will encounter datas. Each programming languages have different approach on how to store and manipulate datas. C for example, it is known for quite strict on data types, due to the native environment of C that is often considered low-level compared to Python
, Ruby
or even Java
. Without understanding data types on C, it can lead into a situation where ‘supposed-to-be-efficient’ code becomes ‘resource-consuming’ code. It is certainly an opposite of the motivation people use C at the beginning, to build fast, small and efficient program. Even, unwanted issues that should be hindered, e.g., buffer overrun, memory leaks, or segmentation fault could be there causing potential bugs.
Variables
Variables or the basic data objects in a program, allow programmers to store datas such as integer values, string characters and floating points (real numbers). A variable tells the compiler how much the storage should be created in memory to store datas. A variable can be composed of letters, no matter whether it is in uppercase or lowercase letters. Also digits and underscore can be put upon a variable name. In C-programming, variables have to be declared first before you can using it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* declarations */
int numX;
int numY;
float numA, numB;
/* declaration with initiation */
char text = "X";
/* definition */
numX = 10;
numY = numX;
numA = 3.14159625;
numB = numA;
Variable name can be composed of letters (both uppercase and lowercase letters), digits and underscore ‘_’ only. However, it is discouraged to use underscore as first letter on variable name even though it is permittable and legal. Just keep in mind, that some system and compiler specific names also uses variable with underscore as the first letter, thus it can become an unwanted conflict.
line:01 and line:02 show two instances of variable declaration. Both variables are integer
type. This means each of numX
and numY
will use 4 bytes in memory, as once those variables are declared, 32 bits (for each) is allocated to store the value. In integer, with 32 bits of length, there are 4,294,967,296 decimal values that can be computed. The minimal and maximal value however, will start from -2,147,483,648 and end at 2,147,483,647. If you are confident enough that your data values will never become negative integer, then you can initiate an unsigned integer
to make the value start from zero and end at 4,294,967,295.
float
variable in line:04 are the numeric constants that has either fractional form or exponent form. This type of data store a floating point value in single precision (32 bits). An example, float pi = 3.14159265;
, will let the system to have Phi value stored as floating point. The accuracy of floating point however, is something that still be quite problematic for some programmers. Some certain value will be rounded here and there to get closest representation. The fact about floating point is that it can’t represent precisely all real numbers, thus for a highly critical computation that requires very precise numbers, this problem can be circumvented by utilising double
precision (64 bits) data type or with help from long
type modifier.
line:05 shows char
variable that store single character which is typically 8 bits in size. A char can be assigned a signed value between -128 to 127, an unsigned value between 0 to 255 or a single quoted ASCII character, e.g. char text = "X"
.
More about floating points…
Type Modifiers
A compiler defines whether the default is signed or unsigned, so to be safe you should declare which type you want to use. Still, using signed or unsigned modifiers to a type data, e.g. integer, will not change the size of the variable inside the memory. An integer stays 32 bit no matter it is signed or unsigned. Since introduction of C99, the newest C-programming language standard, modifier long
is supported to enhance the precision of integer, float and double.
/* OK */
unsigned int A = 4294967295;
/* overflow problem */
unsigned int B = 4294967296;
/* OK */
unsigned long int C = 4294967296;
Another modifier, const
will let the corresponding variable fixed throughout the runtime.
/* declare A and init the value */
const int A = 5;
/* try to overwrite A with new value */
A = 3;
/* check the result */
printf("%d", A);
Still there are more modifiers in C99 standard, but we are not gonna look for it too deep for now. However, C can be quite demanding on how it stores the datas. This required more knowledge in order to master for ones to get the most of the C.
A Good Example
Here, an example that contains everything, from comments, macro variables, signed versus unsigned integers, and simple implementation of format tag inside printf
.
Basically, the code tells you to assign maximum signed integer value to a
, and b
will have the minimum signed integer value. Afterward, by adding one decimal to a
is enough to let an overflow happens. When an overflow happens, the value of a
will be wrapped around to the minimum one, or equal to current b
value. The workaround for this is to use unsigned integer, demonstrated by variable c
. You can expect the output of code above to be shown like this,
A value is 2147483647
B value is -2147483648
now, add one decimal to A
A value now is -2147483648
C value using signed decimal tag is: -2147483648
C value using unsigned decimal tag is: 2147483648
If you haven’t understand the idea of comment or printf
, you can check my previous tutorial. On the next tutorial, we will learn on how to perform conditional statements on C.