Skip to content

Instantly share code, notes, and snippets.

@mikaelhg
Created January 2, 2025 18:19
Show Gist options
  • Save mikaelhg/74756c83f48146ec3d310d84d461812f to your computer and use it in GitHub Desktop.
Save mikaelhg/74756c83f48146ec3d310d84d461812f to your computer and use it in GitHub Desktop.
3-shot IBM Granite 3.1 3B MoE A800M code formatting task
You are a C code formatter. Format the given C code according to the following rules:
1. Use 4 spaces for indentation.
2. Place opening braces '{' on the same line as the statement.
3. Place closing braces '}' on a new line, aligned with the start of the statement.
4. Add a space after keywords like `if`, `for`, `while`, `switch`, and before the opening parenthesis.
5. Add a space on either side of binary operators (e.g., +, -, =, <, >).
6. Do not add spaces inside function argument lists except after commas.
7. Add a newline after each semicolon, except for loop initialization and update parts.
8. Use a single newline between functions.
---
**Example 1**
**Input:**
```c
#include<stdio.h>
int main(){
int a=10;
int b=5;
if(a>b){
printf("a is greater than b");
}
return 0;}
```
**Output:**
```c
#include <stdio.h>
int main() {
int a = 10;
int b = 5;
if (a > b) {
printf("a is greater than b");
}
return 0;
}
```
---
**Example 2**
**Input:**
```c
void myFunction( int x,int y){
for(int i=0;i<x;i++){
y++;
}
return ;}
```
**Output:**
```c
void myFunction(int x, int y) {
for (int i = 0; i < x; i++) {
y++;
}
return;
}
```
---
**Example 3**
**Input:**
```c
int calculateSum(int arr[],int size){int sum=0;
for(int i=0;i<size; i++){sum += arr[i];}return sum;}
```
**Output:**
```c
int calculateSum(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
```
---
**Formatting Task**
**Input:**
```c
#include <stdio.h>
int add(int a,int b){
int result=a+b;return result;}
int main(){int x=5;
int y=10;int sum=add(x,y);
printf("Sum: %d",sum);
return 0;}
```
**Output:**
#include <stdio.h>
int main() {
int x = 10;
int y = 5;
if (x > y) {
printf("x is greater than y\n");
if (x > 2 * y) {
printf("x is greater than 2 times y\n");
for (int i = 0; i < x; i++) {
printf("%d ", i);
if (i % 2 == 0) {
printf("even\n");
} else {
printf("odd\n");
}
}
} else {
printf("x is not greater than 2 times y\n");
}
} else {
printf("x is not greater than y\n");
switch (y) {
case 1:
printf("y is 1\n");
break;
case 5:
printf("y is 5\n");
break;
default:
printf("y is neither 1 nor 5\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment