Number Literals
Integers |
|
binary |
|
binary |
|
octal |
|
decimal |
|
hexadecimal |
|
hexadecimal |
Real Numbers |
|
single precision float ( f
suffix ) |
88.0
/ 88.123456789012345
|
double precision float ( no f
suffix ) |
Signage |
|
positive |
|
negative |
Binary notation 0b...
/ 0B...
is available on GCC and most but not all C compilers.
Variables
Declaring |
|
A variable. |
|
A variable & initialising it. |
|
Multiple variables of the same type. |
|
A constant variable: can't assign to after declaration (compiler enforced.) |
Naming |
|
Alphanumeric, not a keyword, begins with a letter. |
|
Doesn't begin with a letter. |
|
Reserved keyword. |
|
Non-alphanumeric. |
iamaverylongvariablenameohmygoshyesiam;
|
Longer than 31 characters (C89 & C90 only) |
Constants are CAPITALISED
. Function names usually take the form of a verb eg. plotRobotUprising()
.
Primitive Variable Types
*applicable but not limited to most ARM, AVR, x86 & x64 installations |
[class] [qualifier] [unsigned] type/void name;
|
by ascending arithmetic conversion |
Integers |
Type |
Bytes |
Value Range |
|
1 |
|
|
1 |
0 to 28-1 |
|
1 |
-27 to 27-1 |
|
2 / 4 |
|
|
2 / 4 |
0 to 216-1 OR 231-1 |
|
2 / 4 |
-215 to 215-1 OR -231 to 232-1 |
|
2 |
|
|
2 |
0 to 216-1 |
|
2 |
-215 to 215-1 |
|
4 / 8 |
|
|
4 / 8 |
0 to 232-1 OR 264-1 |
|
4 / 8 |
-231 to 231-1 OR -263 to 263-1 |
|
8 |
|
|
8 |
0 to 264-1 |
|
8 |
-263 to 263-1 |
Floats |
Type |
Bytes |
Value Range (Normalized) |
|
4 |
±1.2×10-38 to ±3.4×1038 |
|
8 / 4 |
±2.3×10 -308 to ±1.7×10 308 OR alias to float
for AVR. |
|
ARM: 8, AVR: 4, x86: 10, x64: 16 |
Qualifiers |
|
Flags variable as read-only (compiler can optimise.) |
|
Flags variable as unpredictable (compiler cannot optimise.) |
Storage Classes |
|
Quick access required. May be stored in RAM OR a register. Maximum size is register size. |
|
Retained when out of scope. static
global variables are confined to the scope of the compiled object file they were declared in. |
|
Variable is declared by another file. |
Typecasting |
|
|
char x = 1, y = 2; float z = (float) x / y;
|
Some types (denoted with OR) are architecture dependant.
There is no primitive boolean type, only zero (false, 0
) and non-zero (true, usually 1
.)
Extended Variable Types
[class] [qualifier] type name;
|
by ascending arithmetic conversion |
From the stdint.h Library |
Type |
Bytes |
Value Range |
|
1 |
-27 to 27-1 |
|
1 |
0 to 28-1 |
|
2 |
-215 to 215-1 |
|
2 |
0 to 216-1 |
|
4 |
-231 to 231-1 |
|
4 |
0 to 232-1 |
|
8 |
-263 to 263-1 |
|
8 |
0 to 264-1 |
From the stdbool.h Library |
Type |
Bytes |
Value Range |
|
1 |
|
The stdint.h
library was introduced in C99 to give integer types architecture-independent lengths.
Structures
Defining |
struct strctName{ type x; type y; };
|
A structure type strctName
with two members, x
and y
. Note trailing semicolon |
struct item{ struct item *next; };
|
A structure with a recursive structure pointer inside. Useful for linked lists. |
Declaring |
struct strctName varName;
|
A variable varName
as structure type strctName
. |
struct strctName *ptrName;
|
A strctName
structure type pointer, ptrName
. |
struct strctName{ type a; type b; } varName;
|
Shorthand for defining strctName
and declaring varName
as that structure type. |
struct strctName varName = { a, b };
|
A variable varName
as structure type strctName
and initialising its members. |
Accessing |
|
Member x
of structure varName
. |
|
Value of structure pointer ptrName
member x
. |
Bit Fields |
|
Declares x
with two members a
and b
, both four bits in size (0 to 15.) |
Array members can't be assigned bit fields. |
Type Definitions
Defining |
typedef unsigned short uint16;
|
Abbreviating a longer type name to uint16
. |
typedef struct structName{int a, b;}newType;
|
Creating a newType
from a structure. |
typedef enum typeName{false, true}bool;
|
Creating an enumerated bool
type. |
Declaring |
|
Variable x
as type uint16
. |
|
Structure y
as type newType
. |
Unions
Defining |
union uName{int x; char y[8];}
|
A union type uName
with two members, x
& y
. Size is same as biggest member size. |
Declaring |
|
A variable vName
as union type uN
. |
Accessing |
|
Members cannot store values concurrently. Setting y
will corrupt x
. |
Unions are used for storing multiple data types in the same area of memory.
Enumeration
Defining |
enum bool { false, true };
|
A custom data type bool
with two possible states: false
or true
. |
Declaring |
|
A variable varName
of data type bool
. |
Assigning |
|
Variable varName
can only be assigned values of either false
or true
. |
Evaluating |
|
Testing the value of varName
. |
Pointers
Declaring |
|
Pointers have a data type
like normal variables. |
|
They can also have an incomplete type. Operators other than assignment cannot be applied as the length of the type is unknown. |
|
A data structure pointer. |
|
An array/string name can be used as a pointer to the first array element. |
Accessing |
|
A memory address. |
|
Value stored at that address. |
|
Value stored in structure pointer y
member a
. |
|
Memory address of normal variable varName
. |
|
Dereferencing a void
pointer as a type
pointer. |
A pointer is a variable that holds a memory location.
Arrays
Declaring |
|
You set array length. |
type name[int] = {x, y, z};
|
You set array length and initialise elements. |
|
You set array length and initialise all elements to x
. |
|
Compiler sets array length based on initial elements. |
Size cannot be changed after declaration. |
Dimensions |
|
One dimension array. |
|
Two dimensional array. |
Accessing |
|
Value of element int
in array name
. |
|
|
Elements are contiguously numbered ascending from 0 . |
|
Memory address of element int
in array name
. |
|
|
Elements are stored in contiguous memory. |
Measuring |
sizeof(array) / sizeof(arrayType)
|
Returns length of array
. (Unsafe) |
sizeof(array) / sizeof(array[0])
|
Returns length of array
. (Safe) |
Strings
|
Single quotes. |
|
Double quotes. |
|
Null terminator. |
|
|
is equivalent to |
char name[4] = {'A', 's', 'h', '\0'};
|
int i; for(i = 0; name[i]; i++){}
|
|
Strings must include a char
element for \0
.
Escape Characters
|
alarm (bell/beep) |
|
backspace |
|
formfeed |
|
newline |
|
carriage return |
|
horizontal tab |
|
vertical tab |
|
backslash |
|
single quote |
|
double quote |
|
question mark |
|
Any octal ANSI character code. |
|
Any hexadecimal ANSI character code. |
Functions
Declaring |
type/void funcName([args...]){ [return var;] }
|
Function names follow the same restrictions as variable names but must also be unique. |
|
Return value type ( void
if none.) |
|
Function name and argument parenthesis. |
|
Argument types & names ( void
if none.) |
|
Function content delimiters. |
|
Value to return to function call origin. Skip for void
type functions. Functions exit immediately after a return
. |
By Value vs By Pointer |
|
Passing variable y
to function f
argument x
(by value.) |
void f(type *x); f(array);
|
Passing an array/string to function f
argument x
(by pointer.) |
void f(type *x); f(structure);
|
Passing a structure to function f
argument x
(by pointer.) |
|
Passing variable y
to function f
argument x
(by pointer.) |
|
Returning by value. |
type f(){ type x; return &x; }
|
Returning a variable by pointer. |
type f(){ static type x[]; return &x; }
|
Returning an array/string/structure by pointer. The static
qualifier is necessary otherwise x
won't exist after the function exits. |
Passing by pointer allows you to change the originating variable within the function. |
Scope |
int f(){ int i = 0; } i++; |
i is declared inside f() , it doesn't exist outside that function. |
Prototyping |
|
Place before declaring or referencing respective function (usually before main .) |
|
Same type
, name
and args...
as respective function. |
|
Semicolon instead of function delimiters. |
main()
int main(int argc, char *argv[]){return int;}
|
Anatomy |
|
Program entry point. |
|
# of command line arguments. |
|
Command line arguments in an array of strings. #1 is always the program filename. |
|
Exit status ( integer
) returned to the OS upon program exit. |
Command Line Arguments |
|
Three arguments, "app"
, "two"
and "3"
. |
|
Two arguments, "app"
and "two 3"
. |
main
is the first function called when the program executes.
Conditional (Branching)
if, else if, else |
|
Evaluates b
if a
is true. |
|
Evaluates b
and c
if a
is true. |
|
Evaluates b
if a
is true, c
otherwise. |
if(a){ b; }else if(c){ d; }else{ e; }
|
Evaluates b
if a
is true, otherwise d
if c
is true, otherwise e
. |
switch, case, break |
|
Evaluates c
if a
equals b
. |
|
Evaluates b
if a
matches no other case. |
switch(a){ case b: case c: d; }
|
Evaluates d
if a
equals either b
or c
. |
switch(a){ case b: c; case d: e; default: f; }
|
Evaluates c
, e
and f
if a
equals b
, e
and f
if a
equals d
, otherwise f
. |
switch(a){ case b: c; break; case d: e; break; default: f; }
|
Evaluates c
if a
equals b
, e
if a
equals d
and e
otherwise. |
Iterative (Looping)
while |
int x = 0; while(x < 10){ x += 2; }
|
Loop skipped if test condition initially false. |
|
Declare and initialise integer x
. |
|
Loop keyword and condition parenthesis. |
|
Test condition. |
|
Loop delimiters. |
|
Loop contents. |
do while |
char c = 'A'; do { c++; } while(c != 'Z');
|
Always runs through loop at least once. |
|
Declare and initialise character c
. |
|
Loop keyword. |
|
Loop delimiters. |
|
Loop contents. |
|
Loop keyword and condition parenthesis. Note semicolon. |
|
Test condition. |
for |
int i; for(i = 0; n[i] != '\0'; i++){}
(C89) |
OR |
for(int i = 0; n[i] != '\0'; i++){}
(C99+) |
Compact increment/decrement based loop. |
|
|
|
Loop keyword. |
|
Initialises integer i
. Semicolon. |
|
Test condition. Semicolon. |
|
Increments i
. No semicolon. |
|
Loop delimiters. |
continue |
int i=0; while(i<10){ i++; continue; i--;}
|
Skips rest of loop contents and restarts at the beginning of the loop. |
break |
int i=0; while(1){ if(x==10){break;} i++; }
|
Skips rest of loop contents and exits loop. |
|
|
Console Input/Output
|
Characters |
|
Returns a single character's ANSI code from the input stream buffer as an integer. (safe) |
|
Prints a single character from an ANSI code integer to the output stream buffer. |
Strings |
|
Reads a line from the input stream into a string variable. (Unsafe, removed in C11.) |
Alternative |
fgets(strName, length, stdin);
|
Reads a line from the input stream into a string variable. (Safe) |
|
Prints a string to the output stream. |
Formatted Data |
|
Read value/s (type defined by format string) into variable/s (type must match) from the input stream. Stops reading at the first whitespace. & prefix not required for arrays (including strings.) (unsafe) |
printf("I love %c %d!", 'C', 99)
|
Prints data (formats defined by the format string) as a string to the output stream. |
Alternative |
fgets(strName, length, stdin); sscanf(strName, "%d", &x);
|
Uses fgets
to limit the input length, then uses sscanf
to read the resulting string in place of scanf
. (safe) |
The stream buffers must be flushed to reflect changes. String terminator characters can flush the output while newline characters can flush the input.
Safe functions are those that let you specify the length of the input. Unsafe functions do not, and carry the risk of memory overflow.
File Input/Output
|
Opening |
FILE *fptr = fopen(filename, mode);
|
|
Declares fptr
as a FILE type pointer (stores stream location instead of memory location.) |
|
Returns a stream location pointer if successful, 0
otherwise. |
|
String containing file's directory path & name. |
|
String specifying the file access mode. |
Modes |
|
Read existing text/binary file. |
|
Write new/over existing text/binary file. |
|
Write new/append to existing text/binary file. |
|
Read and write existing text/binary file. |
|
Read and write new/over existing text/binary file. |
|
Read and write new/append to existing text/binary file. |
Closing |
|
Flushes buffers and closes stream. Returns 0
if successful, EOF
otherwise. |
Random Access |
|
Return current file position as a long integer. |
fseek(fptr, offset, origin);
|
Sets current file position. Returns false is successful, true otherwise. The offset
is a long integer type. |
Origins |
|
Beginning of file. |
|
Current position in file. |
|
End of file. |
Utilities |
|
Tests end-of-file indicator. |
rename(strOldName, strNewName)
|
Renames a file. |
|
Deletes a file. |
Characters |
|
Returns character read or EOF if unsuccessful. (safe) |
|
Returns character written or EOF if unsuccessful. |
Strings |
fgets(char *s, int n, fptr)
|
Reads n-1
characters from file fptr
into string s
. Stops at EOF
and \n
. (safe) |
|
Writes string s
to file fptr
. Returns non-negative on success, EOF
otherwise. |
Formatted Data |
fscanf(fptr, format, [...])
|
Same as scanf
with additional file pointer parameter. (unsafe) |
fprintf(fptr, format, [...])
|
Same as printf
with additional file pointer parameter. |
Alternative |
fgets(strName, length, fptr); sscanf(strName, "%d", &x);
|
Uses fgets
to limit the input length, then uses sscanf
to read the resulting string in place of scanf
. (safe) |
Binary |
fread(void *ptr, sizeof(element), number, fptr)
|
Reads a number
of element
s from fptr
to array *ptr
. (safe) |
fwrite(void *ptr, sizeof(element), number, fptr)
|
Writes a number
of element
s to file fptr
from array *ptr
. |
Safe functions are those that let you specify the length of the input. Unsafe functions do not, and carry the risk of memory overflow.
Placeholder Types (f/printf And f/scanf)
printf("%d%d...", arg1, arg2...);
|
Type |
Example |
Description |
|
|
Signed decimal integer. |
|
|
Unsigned decimal integer. |
|
|
Unsigned octal integer. |
|
|
Unsigned hexadecimal integer. |
|
|
Signed decimal float. |
|
|
Signed decimal w/ scientific notation. |
|
|
Shortest representation of %f
/ %F
or %e
/ %E
. |
|
0x1.207c8ap+30
or 0X1.207C8AP+30
|
Signed hexadecimal float. |
|
|
A character. |
|
|
A character string. |
|
|
A pointer. |
|
|
A percent character. |
|
No output, saves # of characters printed so far. Respective printf argument must be an integer pointer. |
The pointer format is architecture and implementation dependant.
Placeholder Formatting (f/printf And f/scanf)
%[Flags][Width][.Precision][Length]Type
|
Flags |
|
Left justify instead of default right justify. |
|
Sign for both positive numbers and negative. |
|
Precede with 0
, 0x
or 0X
for %o
, %x
and %X
tokens. |
|
Left pad with spaces. |
|
Left pad with zeroes. |
Width |
|
Minimum number of characters to print: invokes padding if necessary. Will not truncate. |
|
Width specified by a preceding argument in printf
. |
Precision |
|
Minimum # of digits to print for %d
, %i
, %o
, %u
, %x
, %X
. Left pads with zeroes. Will not truncate. Skips values of 0. |
|
Minimum # of digits to print after decimal point for %a
, %A
, %e
, %E
, %f
, %F
(default of 6.) |
|
Minimum # of significant digits to print for %g
& %G
. |
|
Maximum # of characters to print from %s
(a string.) |
|
If no integer
is given, default of 0. |
|
Precision specified by a preceding argument in printf
. |
Length |
|
|
|
|
|
|
|
Display a long long
integer. |
|
Display a long double
float. |
|
Display a size_t
integer. |
|
Display a intmax_t
integer. |
|
Display a ptrdiff_t
integer. |
Preprocessor Directives
|
Replaces line with contents of a standard C header file. |
|
Replaces line with contents of a custom header file. Note dir path prefix & quotations. |
|
Replaces all occurrences of NAME
with value
. |
Comments
// We're single-line comments!
// Nothing compiled after // on these lines.
/* I'm a multi-line comment!
Nothing compiled between
these delimiters. */
|
C / POSIX Reserved Keywords
Heap Space
|
Allocating |
|
Returns a memory location if successful, NULL
otherwise. |
type *x; x = malloc(sizeof(type));
|
Memory for a variable. |
type *y; y = malloc(sizeof(type) * length );
|
Memory for an array/string. |
struct type *z; z = malloc(sizeof(struct type));
|
Memory for a structure. |
Deallocating |
|
Removes the memory allocated to ptrName
. |
Reallocating |
|
Attempts to resize the memory block assigned to ptrName
. |
The memory addresses you see are from virtual memory the operating system assigns to the program; they are not physical addresses.
Referencing memory that isn't assigned to the program will produce an OS segmentation fault.
The Standard Library
|
Randomicity |
|
Returns a (predictable) random integer between 0 and RAND_MAX based on the randomiser seed. |
|
The maximum value rand()
can generate. |
|
Seeds the randomiser with a positive integer. |
|
Returns the computer's tick-tock value. Updates every second. |
Sorting |
qsort(array, length, sizeof(type), compFunc);
|
|
Sort using the QuickSort algorithm. |
|
Array/string name. |
|
Length of the array/string. |
|
Byte size of each element. |
|
Comparison function name. |
compFunc |
int compFunc( const void *a, const void b* ){ return( *(int *)a - *(int *)b); }
|
|
Function name unimportant but must return an integer. |
const void *a, const void *b
|
Argument names unimportant but must identical otherwise. |
return( *(int *)a - *(int *)b);
|
Negative result swaps b
for a
, positive result swaps a
for b
, a result of 0
doesn't swap. |
C's inbuilt randomiser is cryptographically insecure: DO NOT use it for security applications.
The Character Type Library
|
|
|
|
|
|
True if char
is a letter of the alphabet, false otherwise. |
|
True if char
is a lowercase letter of the alphabet, false otherwise. |
|
True if char
is an uppercase letter of the alphabet, false otherwise. |
|
True if char
is numerical ( 0
to 9
) and false otherwise. |
|
True if char
is a whitespace character ( ' ', '\t', '\n'
) and false otherwise. |
The String Library
|
|
Returns # of char
in string a
as an integer. Excludes \0
. (unsafe) |
|
Copies strings. Copies string b
over string a
up to and including \0
. (unsafe) |
|
Concatenates strings. Copies string b
over string a
up to and including \0
, starting at the position of \0
in string a
. (unsafe) |
|
Compares strings. Returns false if string a
equals string b
, true otherwise. Ignores characters after \0
. (unsafe) |
|
Searches for string b
inside string a
. Returns a pointer if successful, NULL
otherwise. (unsafe) |
Alternatives |
|
Copies strings. Copies n
characters from string b
over string a
up to and including \0
. (safe) |
|
Concatenates strings. Copies n
characters from string b
over string a
up to and including \0
, starting at the position of \0
in string a
. (safe) |
|
Compares first n
characters of two strings. Returns false if string a
equals string b
, true otherwise. Ignores characters after \0
. (safe) |
Safe functions are those that let you specify the length of the input. Unsafe functions do not, and carry the risk of memory overflow.
The Time Library
|
Variable Types |
|
Stores the calendar time. |
|
Stores a time & date breakdown. |
tm structure members: |
|
Seconds, 0 to 59. |
|
Minutes, 0 to 59. |
|
Hours, 0 to 23. |
|
Day of the month, 1 to 31. |
|
Month, 0 to 11. |
|
Years since 1900. |
|
Day of the week, 0 to 6. |
|
Day of the year, 0 to 365. |
|
Daylight saving time. |
Functions |
|
Returns unix epoch time (seconds since 1/Jan/1970.) |
|
Stores the current time in a time_t
variable. |
|
Returns a time_t
variable as a string. |
|
Breaks time_t
down into struct tm
members. |
Unary Operators
by descending evaluation precedence |
|
Sum of 0
(zero) and a
. (0 + a) |
|
Difference of 0
(zero) and a
. (0 - a) |
|
Complement (logical NOT) of a
. (~a) |
|
Binary ones complement (bitwise NOT) of a
. (~a) |
|
Increment of a
by 1
. (a = a + 1) |
|
Decrement of a
by 1
. (a = a - 1) |
|
Returns a
then increments a
by 1
. (a = a + 1) |
|
Returns a
then decrements a
by 1
. (a = a - 1) |
|
|
|
|
|
Memory size of a
(or type
) in bytes. |
Binary Operators
by descending evaluation precedence |
|
Product of a
and b
. (a × b) |
|
Quotient of dividend a
and divisor b
. Ensure divisor is non-zero. (a ÷ b) |
|
Remainder of integers dividend a
and divisor b
. |
|
|
|
|
|
Left bitwise shift of a
by b
places. (a × 2 b) |
|
Right bitwise shift of a
by b
places. (a × 2 -b) |
|
Less than. True if a
is less than b
and false otherwise. |
|
Less than or equal to. True if a
is less than or equal to b
and false otherwise. (a ≤ b) |
|
Greater than. True if a
is greater than than b
and false otherwise. |
|
Greater than or equal to. True if a
is greater than or equal to b
and false otherwise. (a ≥ b) |
|
Equality. True if a
is equal to b
and false otherwise. (a ⇔ b) |
|
Inequality. True if a
is not equal to b
and false otherwise. (a ≠ b) |
|
Bitwise AND of a
and b
. (a ⋂ b) |
|
Bitwise exclusive-OR of a
and b
. (a ⊕ b) |
|
Bitwise inclusive-OR of a
and b
. (a ⋃ b) |
|
Logical AND. True if both a
and b
are non-zero. (Logical AND) (a ⋂ b) |
|
Logical OR. True if either a
or b
are non-zero. (Logical OR) (a ⋃ b) |
Ternary & Assignment Operators
by descending evaluation precedence |
|
Evaluates a
if x
evaluates as true or b
otherwise. (if(x){ a; } else { b; }) |
|
|
|
Assigns product of a
and b
to a
. (a = a × b) |
|
Assigns quotient of dividend a
and divisor b
to a
. (a = a ÷ b) |
|
Assigns remainder of integers dividend a
and divisor b
to a
. (a = a mod b) |
|
Assigns sum of a
and b
to a
. (a = a + b) |
|
Assigns difference of a
and b
to a
. (a = a - b) |
|
Assigns left bitwise shift of a
by b
places to a
. (a = a × 2 b) |
|
Assigns right bitwise shift of a
by b
places to a
. (a = a × 2 -b) |
|
Assigns bitwise AND of a
and b
to a
. (a = a ⋂ b) |
|
Assigns bitwise exclusive-OR of a
and b
to a
. (a = a ⊕ b) |
|
Assigns bitwise inclusive-OR of a
and b
to a
. (a = a ⋃ b) |
C Cheatsheet by Ashlyn Black
|
Created By
ashlynblack.com
Metadata
Favourited By
and 21 more ...
Comments
DaveChild, 09:58 28 Jan 15
Wow, this is awesome! Great cheat sheet, really well put together!
Ashlyn Black, 13:04 28 Jan 15
Thanks, Dave!
Brian 19:28 31 Jan 15
very nice. Few things- there's no such things as pass by reference in c, even pointers are passed by value. Also mention which sting/I/O functions are safe or not.
Ashlyn Black, 19:28 2 Feb 15
Good catch on the unsafe functions; I have tagged them as such and also included their safer alternatives.
I'm not quite clear on how passing a pointer to data differs from passing a reference to data? Most of the documentation I've seen so far calls the process of retrieving a value through the use of a pointer "dereferencing"?
Ashlyn Black, 19:28 2 Feb 15
Added enumeration.
Ashlyn Black, 16:59 3 Feb 15
Added literals.
Ashlyn Black, 09:49 20 Feb 15
Fixed declaration error in Enumeration.
Ashlyn Black, 13:43 21 Feb 15
Fixed include error in Preprocessing.
a 10:45 17 Apr 15
Placeholder types are incomplete: what about for example long integer "ul"
Ashlyn Black, 11:03 17 Apr 15
If you look under "Placeholder Formatting" you will find it. 'u' is the placeholder type, 'l' is the length modifier. :)
Mike Coulman 13:11 17 Apr 15
Re: Brian, 19:28 31 Jan 15
dereferencing != pass by reference. Commentor is correct, C has no pass by reference semantics. Better to phrase as 'pass by indirection.'
In C you receive a copy of the thing you pass. When this thing happens to be a pointer, one may indirectly modify the 'pointed at' thing.
In C++ when you pass by reference you receive an alias to the original thing, not a copy. If non-const, when you modify the alias, you modify the thing itself (contrast with C --you never modified the thing itself, only what it pointed at).
Ashlyn Black, 14:48 17 Apr 15
Yep, I fixed that up. :)
Jarkko Hietaniemi 20:29 20 May 15
In float/double, in addition to range, please also add the precision: how many decimal digits they can store. 7 and 15, IIRC.
Ashlyn Black, 01:37 5 Dec 15
As far as I know, the precision isn't fixed. That's why it's call "Floating Point." c:
Skaffen Amtiskaw 10:10 13 Jan 16
strncpy is somewhat unsafe: if the source string is too long to fit in the destination, the result is not NUL-terminated and is not a valid C string; attempting to do things with it later (e.g. print it) will overrun. Try this out:
#include <stdio.h>
typedef struct test {
char first[8];
char second[8];
} TEST;
int main (void)
{
TEST s;
strncpy(s.first, "01234567", 8);
strncpy(s.second, "private", 8);
printf(s.first);
return 0;
}
kiran kumar A, 18:13 8 Apr 17
In Naming, '_' is allowed along with AlphaNumeric. This is missing
Add a Comment