C is a powerful general-purpose programming language. It is fast, portable, and available on all platforms. If you are new to programming, C is a good choice to start your programming journey.
The basic syntax of C programs consists of functions and variables. A simple C program looks like this:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
C supports various data types including int, float, char, and more. Here are some examples:
int integer = 10;
float floating_point = 3.14;
char character = 'A';
C supports various control structures like if-else, for loop, while loop, etc. Here are some examples:
// If-Else
if (integer > 5) {
printf("Integer is greater than 5");
} else {
printf("Integer is less than or equal to 5");
}
// For Loop
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
Functions in C allow you to divide your code into modular blocks. Here is an example:
void printMessage() {
printf("Hello from a function!");
}
int main() {
printMessage();
return 0;
}
Arrays in C are used to store multiple values of the same type. Here is an example:
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
Pointers are a powerful feature in C that allows you to work with memory directly. Here is an example:
int value = 10;
int *pointer = &value;
printf("Value: %d, Pointer: %p", value, pointer);
Structures in C allow you to group different data types together. Here is an example:
struct Person {
char name[50];
int age;
};
struct Person person1;
person1.age = 30;
strcpy(person1.name, "John Doe");
printf("Name: %s, Age: %d", person1.name, person1.age);
C provides functions to work with files. Here is an example of reading a file:
FILE *file;
file = fopen("example.txt", "r");
if (file) {
char line[100];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
}