C basic cheat sheet

Basic C Reference

Here you’ll find some of the basic operators and variable types used in C with quick examples. Please note this section is not complete as things like pointers and structures aren’t listed and some of the code examples could be improved. Also missing is the const keyword, some logical operators and conversion specify -ers.

Basic Operators

Operator Explanation
== This means that for example, a == b, then a is equal to or equals b
!= This means, in the example a != b, a does not equal b
+= This operator is an easy way to add a value to a variable, a += 5, this increases a by 5
-= This operator as the above, does the opposite, a -= 5, will decrease a by 5
++ This operator is used to add 1 to a variable, a++, would increase a by 1
- - This is the above operators opposite, meaning a- -, would decrease a by 1 1)
> The greater than operator
< The less than operator
>= The greater than or equal to operator
< = The less than or equal to operator 2)

Basic Syntax and Correct Usage

/* ... */

Multiple line comments can be placed within these as the C compiler will skip all text and or code between them until it reaches a close comment.

//

Single line comment allowing you to comment out a line of code or add a quick comment. The comment must be all on one line.

#include <lib.h>

Includes the header file ‘lib.h’. This allows you to use all the data types and functions declared in lib.h. The included file can a standard C library file like stdio.h or a PSP one like pspkernel.h. You can also include your own header files. When the file is in another folder you include it like so:

#include "headerFile.h"
#define TRUE 1

Defines a macro to replace all occurrences of TRUE with 1, when compiled. The benefit of having this as a #define is that it makes you code much more readable and understandable. Also if the value of TRUE needs to change you only have to do it once rather than for each time it is used.

It can also allow you to define new function names for existing functions. You can also define macro functions.

// Use printf instead of pspDebugScreenPrintf
#define printf pspDebugScreenPrintf
 
// Returns the larger of the two values
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) 
void

The void type specifier is primarily used to declare void functions - functions that do not return values. Please note that main() must return an int otherwise it will cause a warning since the program has to return something with the new ISO standards.

int

Used to declare a new variable of type int, to specify the return value type of a function or the type of input for a function.

Pre-fix for returning a whole number variable (ex: int pi = 3).

float

Used to declare a new variable of type float, to specify the return value type of a function or the type of input for a function.

Pre-fix for returning either a whole or decimal integer (ex: float pi = 3.14)

char

Pre-fix for a single character. It can be used as a string, an array, or a buffer. To define a string in C you have to create an array of char’s as there is no string type in C.

char myChar = 'a';
 
char myString[32] = "abcde";

The string example above is for a fixed length string of 32 characters. One character is left for a terminating NULL character - \0 - so the string’ actual length is 31.

{ ... }

These are braces, they signal the beginning and end of functions and other code blocks such as loops and if statements.

;

The semi-colon is used to distinguish the EOL, with the exception of after multi-line arguments and do while loops, where the semi-colon appears after the while keyword and condition.

do {
    ...
}
while();

1) There is no space between the - and -, it’s like that to stop it appearing as –
2) There is no space between the < and =, it’s like that to stop it appearing as ⇐

你可能感兴趣的:(C++,c,C#)