Data Types
Integer
int is typically 4 bytes.
_t
is types that are designed to be cross-platform compatible, which means they include the given number of bytes no matter what system they run on.
You can specify any byte size to int using:
uint32_t // This is an int that holds 32 bytes (0 to 4294967295)
// or
int32_t // -2147483648 to 2147483647
Boolean
Boolean data type for storing 0 or 1 values.
Used for on/off, yes/no, true/false situation (binary choices).
#include <stdbool.h>
bool var = true;
// Or we can do:
_Bool y = 1; // True
_Bool n = 0; // False
// Or use preprocessors (have to include stdbool header):
#define true 1
#define false 0
#define yes true
#define no false
size_t
size_t → Used to represent the size of objects in bytes and is therefore used as the return type by the sizeof operator.
size_t is a type guaranteed to hold any array index only for non-negative values.
size_t s1 = strlen(str1);
printf("%zu", s1);
wchar_t
wchar_t → Wide char is similar to char data type, except that wide char take up twice the space and can take on much larger values as a result.
Char can take 256 values which corresponds to entries in the ASCII table.
On the other hand, wide char can take on 65536 values which corresponds to UNICODE values which is a recent international standard which allows for the encoding of characters for virtually all languages and commonly used symbols.
Example:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
wchar_t *s;
s = (wchar_t *)malloc(sizeof(wchar_t) * 2);
s[0] = 42; // ascii code for *
s[1] = 115; // ascii code for 's'
printf("%ls\n", s);
free(s);
return 0;
}
char
Represents a single character such as the letter 'a'.
var = 't';
char grade = 65; → is valid for ASCII code but not recommended.
Escape Characters
\a → alert
\b → backspace
\f → form feed
\n → new line
\r → carriage return
\t → horizental tab
\v → vertical tab
\\ → backslash \
\' → single quote
\" → double quote
\? → question mark ?
Data Type Conversion Functions
double a = atof(s) → // Converts the string into a floating-point number, returning the result
int a = atoi(s) → // Converts the string into an int, returning the result
int a = atol(s) → // Converts the string into a long int, returns result
int a = atoll (s) → // Converts string into long long int, returns result
Printing Format Strings
Typedef
A keyword that allows us to create our own name for an existing data type.
Defines the name counter to be equal to the C data type int.
Now variables can be declared of type counter:
typedef int counter;
Operators
+ → add
- → subtract
* → multiply
/ → divide
% → remainder of division
++ → increment by one
-- → decrement by one
= → simple assign operator
+= → add and assign
-= → subtract and assign
*= → multiply and assign
/= → devide and assign
%= → modulus and assign
<< → left shift operator, i.e: 1 << 0 == 2 to the power of 0 == 1 == binary 0001
>> → right shift operator
>>= → right shift and assign
<<= → left shift and assign
&= → bitwise and assign
^= → bitwise exclusive or and assign
|= → bitwise inclusive or and assign
Example:
#include <stdio.h>
int main(){
unsigned int a = 60; // 0011 1100
unsigned int b = 13; // 0000 1101
int result = 0;
int c = a & b; // 0000 1100
printf("result: %d", c);
return 0;
}
Difference between x++ and ++x
++x happens prior to assignment (per-increment).
x++ happens after assignment (post-increment).
x++ executes the statement and then increments the value.
++x increments the value and then executes the statement.
Logical Operators
&& → AND operator, if both are non-zero the condition is true (A && B) is false.
|| → OR operator, if any or both are non-zero the condition is true (A || B) is true.
! → NOT operator, reverse the logical statement !(A && B) is true.
(A || B) ? printf("true") : printf("false");
(A && B) ? printf("true") : printf("false");
Last updated