Awesome
C Snippets
This extension provides a simple set of VSCode snippets for the C programming language. Can be found here on the Visual Studio Marketplace.
List of snippets:
Snippet Description | Snippet Input | Snippet Code |
---|---|---|
Starter Template | sst | <code>#include <stdio.h><br>int main (int argc, char *argv[]) { <br> return 0;<br>}<code> |
Starter template with stlib.h | libsst | <code>#include <stdio.h><br> #include <stdlib.h> <br>int main (int argc, char *argv[]) { <br> return 0;<br>}<code> |
Conditionals and loops | ||
If statement | if | <code>if (expression) {<br> /* code here */<br>}</code> |
Else if statement | elif | <code>elseif (expression) {<br> /* code here */<br>}</code> |
Else statement | else | <code>else {<br> /* code here */<br>}</code> |
For loop | for | <code>for (int i = 0; i < count; i++) {<br> /* code here */<br>}</code> |
While loop | while | <code>while (expression) {<br> /* code here */<br>}</code> |
Do...while loop | dowhile | <code>do {<br> /* code here */<br>} while (expression)</code> |
Header file include guard | ig | <code>#ifndef {transformed_file_name} <br>#define {transformed_file_name} <br><br> // Code for header body <br><br>#endif {transformed_file_name}</code> |
Linked lists | ||
Linked list template | clist | <code>typedef struct _node * Link;<br>typedef struct _node node;<br>struct _node {<br> int value;<br> Link next;<br>};<code> |
Functions | ||
Create int function | intfunc | <code>int func_name () {<br> int x;<br> return x;<br>}<code> |
Create float function | flfunc | <code>float func_name () {<br> float x;<br> return x;<br>}<code> |
Create string function | strfunc | <code>char[] func_name () {<br> char[] x = {};<br> return x;<br>}<code> |
Create long function | longfunc | <code>long func_name () {<br> long x;<br> return x;<br>}<code> |
Create definition for virtual table | vtdef | <code>typedef struct {ClassName}{ <br> struct {ClassNameVT}* vt; <br>}; <br><br>typedef struct {ClassNameVT} <br>{ <br> // Virtual Table Function definitions <br>} ${virtualTable Name}; <br><br>int {ClassNameInit}(struct {ClassName} *self); <br>int {ClassNameDestroy}(struct {ClassName} **self);<code> |
Create function for virtual table | vtfunc | <code>{ReturnType} (*{FunctionName})(struct {ClassName} *self)<code> |
Print statements | ||
Print variable of type float (2 decimal places) | pflo | <code>printf("var_name :>> %.2f\n", var_name);</code> |
Print variable of type int | pint | <code>printf("var_name :>> %d\n", var_name);</code> |
Print variable of type char | pcha | <code>printf("var_name :>> %c\n", var_name);</code> |
Print variable of type pointer | ppoint | <code>printf("var_name :>> %p\n", (void *) var_name);</code> |
Print variable of type size_t | psiz | <code>printf("var_name :>> %zu\n", var_name);</code> |
Memory Allocation | ||
Allocate memory using calloc | cal | <code>{type} ptr = ({type})calloc(, sizeof({type})); <br>if (ptr == NULL) { <br> printf("Memory allocation failed!\n"); <br> exit(0); <br> } <br> free(ptr); </code> |
If you would like to support development of the extension donate Bitcoin here: 1BnrjF49owBx7UjMaJt5crZw5DegUYG3mb