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 ⇐
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
比较简短的C语言教程,英文资料 1.0 Introduction 1.1 The main() Function 1.2 Include Files 1.3 Whitespace 1.4 The Preprocessor 1.4.1 The #define Directive 1.4.2 Comments 1.4.3 The #include Directive 1.4.4 The #ifdef/#else/#endif Directives 1.4.5 More Directives 1.4.6 Predefined Macros 2.0 Basic Data Types 2.1 Signed Integer Types 2.2 Unsigned Integer Types 2.3 Integer Overflow 2.4 Real Data Types 2.5 BCC32 Implementation 2.6 The void Type 3.0 Control Flow 3.1 The if/else/endif Statements 3.2 Compound Statements 3.3 Nested if Statements 3.4 The switch/case Statement 3.5 The for Statement 3.6 The while Statement 3.7 The do/while Statement 3.8 The break Statement 3.9 The continue Statement 3.10 The goto Statement 3.11 The return Statement 4.0 Expressions and Operators 4.1 Basic Arithmetic Operators 4.2 Promotion and Casting 4.3 More Arithmetic Operators 4.4 Assignment Operators 4.5 Bitwise Operators 4.6 Relational Operators 4.7 Logical Operators 4.8 The Conditional Operator The C Cheat Sheet Revision 1 Copyright ? 2000 Andrew Sterian iv 4.9 The Comma Operator 4.10 Operator Precedence and Association 5.0 Program Structure 5.1 Declaring Functions 5.2 Calling Functions 5.3 Local Variables 5.4 Global Variables 6.0 Advanced Data Types 6.1 Arrays 6.2 Structures 6.3 Pointers 6.4 Strings 6.4.1 String Pointers 6.4.2 String Operations 6.5 Enumerated Types 6.6 Bitfields 6.7 Unions 7.0 Advanced Programming Concepts 7.1 Standard I/O 7.1.1 Opening Files 7.1.2 Writing to File Pointers 7.1.3 Reading from File Pointers 7.1.4 The stdin/stdout/stderr Streams 7.2 Dynamic Memory Allocation 7.3 Memory Manipulation 7.4 Declaring New Types 7.5 Pointers to Functions 7.6 Command-Line Parameters 8.0 Multi-File Programs 8.1 Basic Concepts 8.2 Include Files as Interfaces 8.3 Object Files and Linking 8.4 The Details of the Compilation Process 9.0 The Standard C Library 9.1 Assertion Checking 9.2 Character Classification 9.3 Error Reporting 9.4 Buffer Manipulation 9.5 Non-Local Jumps 9.6 Event Signalling 9.7 Variable-Length Argument Lists 9.8 Miscellaneous Functions 9.9 String Handling 9.10 Time Functions 9.11 Floating-Point Math 9.12 Standard I/O 10.0 Tips, Tricks, and Caveats 10.1 Infinite Loops 10.2 Unallocated Storage 10.3 The Null Statement 10.4 Extraneous Semicolons 10.5 strcmp is Backwards 10.6 Unterminated Comments 10.7 Equality and Assignment 10.8 Assertion Checking 10.9 Error Checking 10.10 Programming Style 11.0 Differences between Java and C
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值