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":
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()
#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:
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.
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.
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.
//declare variable ch with initial value 'a'
char ch = 'a';
//alter variable ch content using operator '='
ch = 'l';
Notes:
You can define variables that are immutable. These area actually called constants.
const double PI = 3.14; //protected
PI = 2.9; //Error: PI is a constant
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 |
"One line \nSecond line."
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 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:
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 |
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.
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