Wednesday, January 15, 2025
HomeTechWhat is C Programming and Examples

What is C Programming and Examples

C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. It is one of the oldest and most widely used programming languages, known for its simplicity, efficiency, and close relationship with hardware.

Key Features of C

  1. Simple Syntax: Easy to understand and implement.
  2. Portable: C code can be executed on different machines with minimal changes.
  3. Low-Level Access: Provides direct access to memory via pointers.
  4. Modularity: Supports functions for modular programming.
  5. Efficient: Suitable for system-level programming (e.g., operating systems, embedded systems).
  6. Rich Standard Library: Includes libraries for I/O, math, string manipulation, etc.

Applications of C

  1. Operating Systems: Linux, Windows kernels.
  2. Embedded Systems: Microcontrollers and IoT devices.
  3. Compilers: Many modern compilers are written in C.
  4. Database Management Systems: MySQL, Oracle.
  5. Gaming: High-performance gaming engines.

Basic Syntax

C programs are written in a structured format and consist of the following:

  • Preprocessor Directives: Includes necessary libraries.
  • Main Function: The entry point of the program.
  • Statements: Instructions executed by the program.
See also  What is Bit Masking in C?

Examples of C Programs

1. Hello World Program

c
#include <stdio.h> // Include standard I/O library

int main() {
printf("Hello, World!\n"); // Print output
return 0; // Indicate successful execution
}

Output:

Hello, World!

2. Program to Add Two Numbers

c
#include <stdio.h>

int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2); // Take input
sum = num1 + num2; // Calculate sum
printf("Sum: %d\n", sum); // Print result
return 0;
}

Output (Example):

mathematica
Enter two numbers: 5 7
Sum: 12

3. Find if a Number is Even or Odd

c
#include <stdio.h>

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

if (number % 2 == 0) {
printf("%d is Even\n", number);
} else {
printf("%d is Odd\n", number);
}
return 0;
}

Output (Example):

csharp
Enter a number: 9
9 is Odd

4. Factorial of a Number

c
#include <stdio.h>

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

for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply each number
}
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}

Output (Example):

mathematica
Enter a number: 5
Factorial of 5 is 120

5. Fibonacci Series

c
#include <stdio.h>

int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series: %d, %d", t1, t2);

for (int i = 3; i <= n; i++) {
nextTerm = t1 + t2;
printf(", %d", nextTerm);
t1 = t2;
t2 = nextTerm;
}
printf("\n");
return 0;
}

Output (Example):

mathematica
Enter the number of terms: 7
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8

Why Learn C?

  • C is the foundation for many modern programming languages like C++, Java, and Python.
  • It teaches essential programming concepts like loops, conditionals, and memory management.
  • Its versatility and speed make it ideal for system-level programming and performance-critical applications.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x