Online C Compiler – Run and Test C Code in Your Browser
Use our free online C compiler to write, compile, and run C code right in your browser. Perfect for C learners and developers — no setup required.
💡 Learn C/C++ to upgrade your skills
Loading...
💡 C Basics Guide for Beginners
1. Declaring Variables and Constants
C requires you to declare the type of each variable. Use #define
or const
to define read-only values.
int age = 30;
double pi = 3.14159;
char grade = 'A';
char name[] = "Alice";
bool isActive = 1; // true
// Constants
#define MAX_USERS 100
const char* COMPANY = "CodeUtility";
2. Conditionals (if / switch)
Use if
, else if
, and switch
for decision making.
int x = 2;
if (x == 1) {
printf("One\n");
} else if (x == 2) {
printf("Two\n");
} else {
printf("Other\n");
}
switch (x) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Other\n");
}
3. Loops
Use for
, while
, and do-while
for repetition.
for (int i = 0; i < 3; i++) {
printf("%d\n", i);
}
int n = 3;
while (n > 0) {
printf("%d\n", n);
n--;
}
4. Arrays
Arrays store multiple elements of the same type.
int numbers[3] = {10, 20, 30};
printf("%d\n", numbers[1]);
5. Structs
struct
lets you group related data.
struct Person {
char name[50];
int age;
};
struct Person p = {"Alice", 30};
printf("%s is %d years old\n", p.name, p.age);
6. Console Input/Output
Use printf
and scanf
for console I/O.
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s\n", name);
7. Functions
Functions encapsulate reusable logic. Declare return type, name, and parameters.
int add(int a, int b) {
return a + b;
}
printf("%d\n", add(3, 4));
8. Pointers
Use pointers to store memory addresses and manipulate data indirectly.
int x = 10;
int* ptr = &x;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", ptr);
printf("Value from pointer: %d\n", *ptr);
*ptr = 20;
printf("Updated x: %d\n", x);
9. File I/O
Use fopen
, fprintf
, fscanf
, and fclose
for file operations.
FILE* file = fopen("file.txt", "w");
fprintf(file, "Hello File");
fclose(file);
char line[100];
file = fopen("file.txt", "r");
fgets(line, sizeof(line), file);
printf("%s", line);
fclose(file);
10. String Handling
Use functions from <string.h>
like strlen
, strcpy
, and strcmp
.
#include <string.h>
char text[] = "Hello";
char copy[10];
strcpy(copy, text);
printf("Length: %lu\n", strlen(copy));
printf("Compare: %d\n", strcmp(copy, "Hello"));
11. Dynamic Memory
Use malloc
and free
for heap allocation.
int* nums = (int*) malloc(3 * sizeof(int));
nums[0] = 1; nums[1] = 2; nums[2] = 3;
for (int i = 0; i < 3; i++) {
printf("%d ", nums[i]);
}
free(nums);