Introduction to C language

C programming language is a powerful and widely used programming language that was developed in the early 1970s by Dennis Ritchie at Bell Laboratories. It is a procedural programming language that is known for its efficiency, flexibility, and portability. C has been used to develop operating systems, compilers, databases, and various applications ranging from small utilities to large-scale software.

Getting Started with C

To start programming in C, you need a compiler such as GCC (GNU Compiler Collection), which is freely available for most operating systems. You can write C programs using a simple text editor like Notepad or a more advanced Integrated Development Environment (IDE) like Visual Studio Code or Code::Blocks.

Let’s start with a simple “Hello, World!” program:

				
					#include <stdio.h>

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

				
			

This program displays “Hello, World!” on the screen. Now, let’s break it down:

  • #include <stdio.h>: This line includes the standard input-output header file, which contains the definition for the printf() function.
  • int main(): Every C program must have a main() function, which serves as the entry point for the program.
  • printf("Hello, World!\n");: This line prints the string “Hello, World!” followed by a newline character (\n) to the standard output (usually the console).
  • return 0;: This statement indicates that the program has executed successfully and returns an exit status of 0 to the operating system.

Basic Concepts in C

Variables and Data Types

In C, you can declare variables to store data. Each variable has a data type, which defines the type of data it can hold. Common data types in C include int, float, char, and double. Here’s how you declare and initialize variables

				
					int age = 25;
float height = 5.9;
char grade = 'A';
double salary = 50000.75;

				
			

Operators

C supports various operators such as arithmetic operators (+, -, *, /), relational operators (==, !=, <, >), and logical operators (&&, ||, !). Here’s an example

				
					int a = 10, b = 5;
int sum = a + b; // sum = 15
int difference = a - b; // difference = 5

				
			

Control Flow Statements

C provides control flow statements like if, else, while, for, and switch to control the flow of execution in a program. Here’s an example of an if statement

				
					int num = 10;
if (num > 0) {
    printf("Positive number\n");
} else {
    printf("Non-positive number\n");
}

				
			

Functions in C

Functions allow you to break down your program into smaller, manageable pieces. A function is a block of code that performs a specific task. Here’s an example of a function that calculates the factorial of a number

				
					#include <stdio.h>

int factorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    int num = 5;
    int fact = factorial(num);
    printf("Factorial of %d is %d\n", num, fact);
    return 0;
}

				
			

In this chapter, we covered the basics of C programming language, including its history, setup, basic syntax, data types, operators, control flow statements, and functions. With this foundation, you can start writing simple C programs and gradually explore more advanced topics such as arrays, pointers, structures, and dynamic memory allocation. C programming language provides a solid groundwork for understanding computer science concepts and developing efficient, high-performance software. Happy Coding! ❤️

Table of Contents