Sure! Below is a list of some fundamental C language topics along with detailed examples for each topic:
1. Hello World Program:
Example:
```c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
```
This is a basic C program that prints "Hello, World!" to the console.
2. Variables and Data Types:
Example:
```c
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
```
This program declares variables of different data types (int, float, char) and prints their values.
3. Input and Output:
Example:
```c
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
```
This program takes user input (name) and prints a personalized greeting.
4. Conditional Statements (if-else):
Example:
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive number\n");
} else if (num < 0) {
printf("Negative number\n");
} else {
printf("Zero\n");
}
return 0;
}
```
This program checks if a number is positive, negative, or zero using if-else statements.
5. Loops (for, while, do-while):
Example (for loop):
```c
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
```
This program uses a for loop to print numbers from 1 to 5.
6. Arrays:
Example:
```c
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int i;
printf("Array elements: ");
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
```
This program declares and prints elements of an integer array.
7. Functions:
Example:
```c
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int num1 = 5, num2 = 3, result;
result = add(num1, num2);
printf("Sum: %d\n", result);
return 0;
}
```
This program defines a function to add two numbers and uses it in the main function.
8. Pointers:
Example:
```c
#include <stdio.h>
int main() {
int num = 10;
int *ptr;
ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value of ptr: %p\n", ptr);
printf("Value pointed by ptr: %d\n", *ptr);
return 0;
}
```
This program demonstrates pointers by declaring a variable, a pointer to that variable, and printing their values and addresses.
9. Structures:
Example:
```c
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s;
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter age: ");
scanf("%d", &s.age);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Student Details:\n");
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
printf("Marks: %.2f\n", s.marks);
return 0;
}
```
This program defines a structure for a student and stores and prints their details.
10. File Handling (Reading and Writing):
Example (writing to a file):
```c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file != NULL) {
fprintf(file, "This is a file handling example.\n");
fclose(file);
printf("Data written to the file successfully.\n");
} else {
printf("Failed to open the file.\n");
}
return 0;
}
```
This program demonstrates writing data to a file.
These examples cover various fundamental concepts of the C programming language. As you progress in your C programming journey, you will encounter more advanced topics and techniques to enhance your skills further.
1. Hello World Program:
Example:
```c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
```
This is a basic C program that prints "Hello, World!" to the console.
2. Variables and Data Types:
Example:
```c
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
```
This program declares variables of different data types (int, float, char) and prints their values.
3. Input and Output:
Example:
```c
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
```
This program takes user input (name) and prints a personalized greeting.
4. Conditional Statements (if-else):
Example:
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive number\n");
} else if (num < 0) {
printf("Negative number\n");
} else {
printf("Zero\n");
}
return 0;
}
```
This program checks if a number is positive, negative, or zero using if-else statements.
5. Loops (for, while, do-while):
Example (for loop):
```c
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
```
This program uses a for loop to print numbers from 1 to 5.
6. Arrays:
Example:
```c
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int i;
printf("Array elements: ");
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
```
This program declares and prints elements of an integer array.
7. Functions:
Example:
```c
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int num1 = 5, num2 = 3, result;
result = add(num1, num2);
printf("Sum: %d\n", result);
return 0;
}
```
This program defines a function to add two numbers and uses it in the main function.
8. Pointers:
Example:
```c
#include <stdio.h>
int main() {
int num = 10;
int *ptr;
ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value of ptr: %p\n", ptr);
printf("Value pointed by ptr: %d\n", *ptr);
return 0;
}
```
This program demonstrates pointers by declaring a variable, a pointer to that variable, and printing their values and addresses.
9. Structures:
Example:
```c
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s;
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter age: ");
scanf("%d", &s.age);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Student Details:\n");
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
printf("Marks: %.2f\n", s.marks);
return 0;
}
```
This program defines a structure for a student and stores and prints their details.
10. File Handling (Reading and Writing):
Example (writing to a file):
```c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file != NULL) {
fprintf(file, "This is a file handling example.\n");
fclose(file);
printf("Data written to the file successfully.\n");
} else {
printf("Failed to open the file.\n");
}
return 0;
}
```
This program demonstrates writing data to a file.
These examples cover various fundamental concepts of the C programming language. As you progress in your C programming journey, you will encounter more advanced topics and techniques to enhance your skills further.
Comments
Post a Comment