🔸Data Types
Integer
int → 4 float → 12.585, -.0001 ( stores less values than double) double → same as float but stores twice the precision
to explicitly express a float constant, append either an f or F to the end of the number: 12.54f
Boolean
boolean data type for storing 0 or 1 values. used for on/off, yes/no, true/false situation (binary choices)
<stdbool.h> → include it to use bool instead of _Bool
bool → substitute name for the basic _Bool data type:
true: 1
false: 0
_Bool → _Bool var = 1
or we can do: _Bool y = 1; _Bool n = 0;
Boolean in Conditional Loops
this definition of true and false in boolean types is also applicable when we are using it in loops like a while loop. for example with when we want to use an infinite loop we can set the statements in a while(1) block:
we can use any other positive or negative number instead of 1 in the loop. for example -255 or 10 or 735 are all the same and create a while loop. but using 0 will make the loop a false condition and it will always be 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
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:
Enum Type
They are arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program.
the compiler actually treats enumeration identifiers as integer constants. first name in list is 0
the value 1 is assigned to thisMonth and not the name February because it is the second identifier listed inside the enumeration list
if you want to have a specific integer value associated with an enumeration identifier the integer can be signed to the identifier when the data type is defined.
an enumerated data type direction is defined with the values up,down,left,right up = 0 because it appears first in the list down = 1 cause its next left = 10 cause we said so right = 11 cause it appears immdeiatly after left in the list
char
represents a single character such as the letter 'a'. always uses single quotes.
char var; → a single character
char grade = 65; → is valid for ASCII code but not recommended
Escape Characters
Data Type Conversion Functions
Printing Format Strings
Complex Numbers
a complex number is a number of the form A + Bi is is the square root of minus one (imaginary) a and b are real numbers
C11 doesn't support complex numbers so it implements the macro STDC_NO_COMPLEX in the header section
to test whether your compiler supports complex or not:
declaring complex numbers:
if we include the complex.h header we can do it this way:
Complex Functions
creal() → returns the real part of a value of type double complex that is passed as the argument
cimage() → returns the imaginary part
we append an f to these functions names when we are working with float complex values ( crealf() and cimagef()) and a lowercase l when using with long complex ( creall() and cmagl())
conj() → returns the complex conjugate of its double complex
conjf() and conjl() → return the complex conjugate for the other two types long and float
Imaginary Numbers
we can use _imaginary keyword to define variables that store purely imaginary numbers.
if we include the complex.h header we can do it this way:
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:
Operators
example:
Difference between x++ and ++x
++x happens prior to assignment (per-increment), but 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
Last updated