0% found this document useful (0 votes)
5 views4 pages

C_Language_Examples

This document provides a series of example programs in C, covering fundamental concepts such as basic syntax, arithmetic operations, conditionals, loops, functions, arrays, pointers, and structures. Each section includes a brief explanation followed by the corresponding code example. The examples aim to help learners build a strong foundation in C programming.

Uploaded by

22014154-053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

C_Language_Examples

This document provides a series of example programs in C, covering fundamental concepts such as basic syntax, arithmetic operations, conditionals, loops, functions, arrays, pointers, and structures. Each section includes a brief explanation followed by the corresponding code example. The examples aim to help learners build a strong foundation in C programming.

Uploaded by

22014154-053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C Language Examples

1. Introduction
C is a powerful and widely used programming language. This document provides various
example programs demonstrating different concepts in C programming.

2. Hello World Program


This is the simplest C program that prints 'Hello, World!' to the console.

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

3. Basic Arithmetic Operations


This program performs basic arithmetic operations like addition, subtraction,
multiplication, and division.

#include <stdio.h>

int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
return 0;
}

4. If-Else Condition
This program checks if a number is even or odd using an if-else condition.

#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}

return 0;
}

5. Loop Example (For Loop)


This program prints numbers from 1 to 10 using a for loop.

#include <stdio.h>

int main() {
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}

6. Functions in C
This program demonstrates the use of functions in C by calculating the square of a number.

#include <stdio.h>

// Function to calculate the square of a number


int square(int x) {
return x * x;
}

int main() {
int num = 5;
printf("Square of %d is %d\n", num, square(num));
return 0;
}
7. Arrays in C
This program initializes an array and prints its elements.

#include <stdio.h>

int main() {
int numbers[5] = {10, 20, 30, 40, 50};

for (int i = 0; i < 5; i++) {


printf("%d\n", numbers[i]);
}

return 0;
}

8. Pointers in C
This program demonstrates how to use pointers in C.

#include <stdio.h>

int main() {
int num = 10;
int *ptr = &num;

printf("Value of num: %d\n", num);


printf("Address of num: %p\n", &num);
printf("Value of num using pointer: %d\n", *ptr);

return 0;
}

9. Structure in C
This program demonstrates the use of structures in C.

#include <stdio.h>
#include <string.h>

struct Student {
char name[50];
int age;
};

int main() {
struct Student student1;
strcpy(student1.name, "John Doe");
student1.age = 20;

printf("Name: %s, Age: %d\n", student1.name, student1.age);

return 0;
}

10. Conclusion
These examples demonstrate fundamental concepts of C programming. Understanding
these programs will help in building a strong foundation in C.

You might also like