Sage-Code Laboratory
index<--

C Data Types

To understand C code you must grasp idea of data type.  You can not declare anything without using a data type. However you can define your own types using keyword: typedef. C has very few basic data types named "primitive":

Primitive types:

C has very few primitive types that are predefined. All other types are derived from these basic types.

Type Name Description Size printf() placeholder
char character 1 byte %c
int default integer 4 bytes %d
short int short integer 2 bytes %hd
long int long integer 8 bytes %li
unsigned int unsigned integer 8 bytes %u
float single precision floating point 4 bytes %f
double double precision floating point 8 bytes %lf

To detect the real size of the data type you can use function: sizeof()

Example:

#include <stdio.h>
int main() {
    short a;
    long b;
    double d;
    printf("size of short = %d bytes\n", sizeof(a));
    printf("size of long = %d bytes\n", sizeof(b));
    printf("size of long double= %d bytes\n", sizeof(d));
    return 0;
}

Note: You can use following modifiers for integer type:

Derived types:

You can create new data types from basic types using a derived data types. There are different methods of derivation. One method is to group multiple elements into data collection or structure. These are called improperly user defined data types.

I will present derived types in future articles.

Identifiers

To make a C program you must declare names or identifiers for things that can be constants, variables, functions or data types. C has no "define" keyword: instead, a statement beginning with the name of a type. There is no "function" keyword: instead, a function is indicated by the parentheses following the function name. In parentheses you can enumerate formal parameters.

Variables

Next, you will learn about variables and rules for naming a variable.  A variable is like a labeled container or storage area in computer memory used to hold data for a short period of time. Variable names are symbolic representation of a memory location. Content of this location can be changed.

Example:

//declare variable ch with initial value 'a'
char ch = 'a';

//alter variable ch content using operator '='
ch = 'l';

Notes:

  1. A variable name can have only Latin letters, digits and underscore,
  2. The first letter of a variable should be either a letter or an underscore,
  3. Identifier name in C should not be longer than 31 characters.

Constants

You can define variables that are immutable. These area actually called constants. 

Example:

const double PI = 3.14; //protected
PI = 2.9; //Error: PI is a constant

Literals

Now is time to understand something more. C source code can contain numbers and characters after the symbol "=", for example: 3.14.  These elements of the language are also constants, but are different kind of constants named Literals. The literals can be changed only if you change the source code.

Literal examples:

Escape sequence:

Some characters are not printable. For representing these characters in string literals you can use escape notation. Use symbol "\" follow by a letter. This will be replaced by the compiler with the proper code point.

\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\’ Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character

Example:

"One line \nSecond line."

Boolean Type

Boolean algebra is a mathematic theory and is an essential part of any programming language. In C there is no Boolean type per say. {0 = False, 1 = True}. Any value = 0 is considered False. Any value ≠ 0 is considered True.

Boolean Operators:

Boolean operators can be used in C with any numeric value. There is no Boolean reserved type. So any numeric literal = 0, = 1 or numeric value >=0 will do. There is no other type checking so you may do it all wrong but C will not worn you about type mismatch.

There are 3 Boolean Operators:

Table of truth:

A B  !A   A && B A || B 
0 0 1 0 0
0 1 1 0 1
1 0 0 0 1
1 1 0 1 1

Relation Operators

In practice common logical expressions are comparison between two variables, constants or literals using comparison operators. In the next table you can analyze these operators and two examples: positive and negative.

Logical Expressions

Operator Description Positive = 1 Negative = 0
== Equal 1 == 1  1 == 0
>= Greater than or equal to 1 >= 0 1 >= 2
<= Less than or equal to 1 <= 1 1 <= 0
> Greater than 2 > 1 5 > 5 + 1
< Less than 0 < 1 1 < 0

Read next: C Pointers