IT 관련 분야의 어떤 사항에 대해 공부할 때 '주제 + cheat sheet'라고 검색을 한다. cheatsheet란? 치트 시트란 말 그대로 커닝 페이지란 뜻인데, 답안지를 훔쳐본다는 의미가 아니라 자주 사용하는 명령이나 기능들을 찾기 쉽게 잘 정리해서 요약한 적은 분량의 문서를 의미한다. 여러 치트시트를 잘 정리한 사이트를 참고한다.
아래는 아두이노 프로그래밍에 필요한 치트 시트다. 아두이노 C언어 요약 페이지 정도로 이해하면 된다. 인쇄해서 책상에 붙여놓고 참고하면 좋은 문서다. pdf 파일과 jpg 파일을 올려둔다. 소스 링크에는 업데이트될 때 자료가 올라오니 참고하면 좋겠다. 종류도 여러 가지 있으니 보기 편한 문서로 참고한다.
아두이노 프로그래밍 C 언어 요약 페이지는 아래를 참고한다. 이 문서 출처는 코드위드 해리라는 프로그래밍 사이트다.
Basics
Basic syntax and functions from the C programming language.
Boilerplate Code
#include<stdio.h> int main()
{
return(0);
}
printf function
It is used to show output on the screen
printf("Hello World!")
scanf function
It is used to take input from the user
scanf("placeholder", variables)
Comments
A comment is the code that is not executed by the compiler, and the programmer uses it to keep track of the code.
Single line comment
// It's a single line comment
Multi-line comment
/* It's a multi-line comment
*/
Data types
The data type is the type of data
Character type
Typically a single octet(one byte). It is an integer type
char variable_name;
Integer type
The most natural size of integer for the machine
int variable_name;
Float type
A single-precision floating-point value
float variable_name;
Double type
A double-precision floating-point value
double variable_name;
Void type
Represents the absence of the type
void
Escape Sequences
It is a sequence of characters starting with a backslash, and it doesn't represent itself when used inside string literal.
Alarm or Beep
It produces a beep sound
\a
Backspace
It adds a backspace
\b
Form feed
\f
Newline
Newline Character
\n
Carriage return
\r
Tab
It gives a tab space
\t
Backslash
It adds a backslash
\\
Single quote
It adds a single quotation mark
\'
Question mark
It adds a question mark
\?
Octal No.
It represents the value of an octal number
\nnn
Hexadecimal No.
It represents the value of a hexadecimal number
\xhh
Null
The null character is usually used to terminate a string
\0
Conditional Instructions
Conditional statements are used to perform operations based on some condition.
If Statement
if (/* condition */)
{
/* code */
}
If-else Statement
if (/* condition */)
{
/* code */
}
else{
/* Code */
}
if else-if Statement
if (condition) {
// Statements;
}
else if (condition){
// Statements;
}
else{
// Statements
}
Switch Case Statement
It allows a variable to be tested for equality against a list of values (cases).
switch (expression)
{
case constant-expression: statement1;
statement2; break;
case constant-expression: statement;
break;
...
default: statement;
}
Iterative Statements
Iterative statements facilitate programmers to execute any block of code lines repeatedly and can be controlled as per conditions added by the programmer.
while Loop
It allows execution of statement inside the block of the loop until the condition of loop succeeds.
while (/* condition */)
{
/* code */
}
do-while loop
It is an exit controlled loop. It is very similar to the while loop with one difference, i.e., the body of the do-while loop is executed at least once even if the expression is false
do
{
/* code */
} while (/* condition */);
for loop
It is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.
for (int i = 0; i < count; i++)
{
/* code */
}
Break Statement
break keyword inside the loop is used to terminate the loop
break;
Continue Statement
continue keyword skips the rest of the current iteration of the loop and returns to the starting point of the loop
continue;
Functions & Recursion
Functions are used to divide an extensive program into smaller pieces. It can be called multiple times to provide reusability and modularity to the C program.
Function Definition
return_type function_name(data_type parameter...){
//code to be executed
}
Recursion
Recursion is when a function calls a copy of itself to work on a minor problem. And the function that calls itself is known as the Recursive function.
void recurse()
{
... .. ...
recurse();
... .. ...
}
Pointers
Pointer is a variable that contains the address of another variable,
Declaration
datatype *var_name;
Arrays
An array is a collection of data items of the same type.
Declaration
data_type array_name[array_size];
Accessing element
int variable_name = array[index];
Strings
A string is a 1-D character array terminated by a null character ('\0')
Declaration
char str_name[size];
gets() function
It allows you to enter multi-word string
gets("string");
puts() function
It is used to show string output
puts("string");
String Functions strlen()
It is used to calculate the length of the string
strlen(string_name);
strcpy() function
It is used to copy the content of second-string into the first string passed to it
strcpy(destination, source);
strcat() function
It is used to concatenate two strings
strcat(first_string, second_string);
strcmp() function
It is used to compare two strings
strcmp(first_string, second_string);
Structures
The structure is a collection of variables of different types under a single name. Defining structure means creating a new data type.
Structure syntax
struct structureName
{
dataType member1; dataType member2;
...
};
typedef keyword
typedef function allows users to provide alternative names for the primitive and user-defined data types.
typedef struct structureName
{
dataType member1; dataType member2;
...
}new_name;
File Handling
A set of methods for handling File IO (read/write/append) in C language
FILE pointer
FILE *filePointer;
Opening a file
It is used to open file in C.
filePointer = fopen(fileName.txt, w)
fscanf() function
It is used to read the content of file.
fscanf(FILE *stream, const char *format, ...)
fprintf() function
It is used to write content into the file.
fprintf(FILE *fptr, const char *str, ...);
fgetc() function
It reads a character from a file opened in read mode. It returns EOF on reaching the end of file.
fgetc(FILE *pointer);
fputc() function
It writes a character to a file opened in write mode
fputc(char, FILE *pointer);
Closing a file
It closes the file.
fclose(filePointer);
Dynamic Memory Allocation
A set of functions for dynamic memory allocation from the heap. These methods are used to use the dynamic memory which makes our C programs more efficient
malloc() function
Stands for 'Memory allocation' and reserves a block of memory with the given amount of bytes.
ptr = (castType*) malloc(size);
calloc() function
Stands for 'Contiguous allocation' and reserves n blocks of memory with the given amount of bytes.
ptr = (castType*)calloc(n, size);
free function
It is used to free the allocated memory.
free(ptr);
realloc() function
If the allocated memory is insufficient, then we can change the size of previously allocated memory using this function for efficiency purposes
ptr = realloc(ptr, x);
'개발자 > Arduino' 카테고리의 다른 글
ADXL335 Accelerometer Module Arduino (0) | 2023.04.11 |
---|---|
아두이노 압력센서 FSR 406 Solder Tabs (1) | 2022.12.12 |
아두이노 금속감지 센서 LJ12A3 (1) | 2022.12.08 |
아두이노 RC522 RFID Module 사용하기 (0) | 2022.11.11 |
아두이노 IDE 통합 개발환경 상세 설명 자료 무료 다운로드 (1) | 2022.10.25 |
아두이노 I2C LCD로 문자 출력하기 (1) | 2022.10.25 |
아두이노 서보모터 제어 SG90 기초부터 전문가 까지 (1) | 2022.10.21 |
아두이노 전체 라이브러리 5103개 (1) | 2022.10.01 |
더욱 좋은 정보를 제공하겠습니다.~ ^^