Home

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.

Visual Studio Marketplace Rating (Stars) Visual Studio Marketplace Installs Visual Studio Marketplace Downloads GitHub package.json version GitHub repo size GitHub

List of snippets:

Snippet DescriptionSnippet InputSnippet Code
Starter Templatesst<code>#include <stdio.h><br>int main (int argc, char *argv[]) { <br>   return 0;<br>}<code>
Starter template with stlib.hlibsst<code>#include <stdio.h><br> #include <stdlib.h> <br>int main (int argc, char *argv[]) { <br>   return 0;<br>}<code>
Conditionals and loops
If statementif<code>if (expression) {<br>   /* code here */<br>}</code>
Else if statementelif<code>elseif (expression) {<br>   /* code here */<br>}</code>
Else statementelse<code>else {<br>   /* code here */<br>}</code>
For loopfor<code>for (int i = 0; i < count; i++) {<br>   /* code here */<br>}</code>
While loopwhile<code>while (expression) {<br>   /* code here */<br>}</code>
Do...while loopdowhile<code>do {<br>   /* code here */<br>} while (expression)</code>
Header file include guardig<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 templateclist<code>typedef struct _node * Link;<br>typedef struct _node node;<br>struct _node {<br>   int value;<br>   Link next;<br>};<code>
Functions
Create int functionintfunc<code>int func_name () {<br>   int x;<br>   return x;<br>}<code>
Create float functionflfunc<code>float func_name () {<br>   float x;<br>   return x;<br>}<code>
Create string functionstrfunc<code>char[] func_name () {<br>   char[] x = {};<br>   return x;<br>}<code>
Create long functionlongfunc<code>long func_name () {<br>   long x;<br>   return x;<br>}<code>
Create definition for virtual tablevtdef<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 tablevtfunc<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 intpint<code>printf("var_name :>> %d\n", var_name);</code>
Print variable of type charpcha<code>printf("var_name :>> %c\n", var_name);</code>
Print variable of type pointerppoint<code>printf("var_name :>> %p\n", (void *) var_name);</code>
Print variable of type size_tpsiz<code>printf("var_name :>> %zu\n", var_name);</code>
Memory Allocation
Allocate memory using calloccal<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

Authors: Harry Ross and Luca Merzetti