1. Introduction to C
History of C
The C programming language was developed in the early 1970s by Dennis Ritchie at Bell Labs. It was designed as a system programming language to write operating systems, specifically UNIX. The language evolved from two previous languages, B and BCPL (Basic Combined Programming Language). C quickly became popular due to its efficiency and control, allowing programmers to write system-level code with high performance.
Features of C
C is a general-purpose, procedural language that provides a wide range of features:
Portability: C programs can run on various types of computer systems with minimal or no modification.
Low-level Access: C provides low-level access to memory through pointers, which is essential for system programming.
Structured Language: C allows complex programs to be broken into simpler sub-programs (functions).
Rich Library: C comes with a standard library that provides numerous built-in functions for tasks like input/output, string handling, mathematical computations, etc.
Efficiency: C is highly efficient, making it suitable for developing operating systems, compilers, and embedded systems.
Modularity: Code can be separated into multiple files, allowing better organization and management
Structure of a C Program
A basic C program consists of one or more functions, the most important of which is the main() function. This function serves as the entry point of the program. Here's a simple structure of a C program:
#include <stdio.h> // Preprocessor directive to include standard I/O library
// Function prototype declarations
void function1();
void function2();
int main() {
// Code to be executed
printf("Hello, World!\n");
return 0; // Return statement to end the program
}
// Function definitions
void function1() {
// Code for function1
}
void function2() {
// Code for function2
}
Preprocessor Directives: These begin with a '#' and are processed before compilation. They include header files and define constants or macros.
Main Function: int main() is where the program execution starts. The return 0; statement indicates successful program termination.
Function Prototypes and Definitions: Functions can be declared before the main() function and defined after it. This allows the program to use them in a modular way.
Compiling and Executing a C Program
To run a C program, it needs to be compiled and then executed. Here are the steps involved:
Writing the Code: Write the C code in a text editor and save it with a .c extension, for example, hello.c.
Compiling the Code: Use a C compiler to convert the source code into an executable file. Common compilers include gcc (GNU Compiler Collection) and clang. For example, using gcc:
gcc hello.c -o hello
This command compiles hello.c and produces an executable named hello.
3. Running the Executable: Execute the compiled program by typing the name of the executable
./hello
This will run the program and display the output.
Example
Here's an example to illustrate the concepts:
#include <stdio.h>
int main() {
// This is a comment
printf("Hello, C Programming!\n"); // Print message to the console
return 0;
}
Comments: Lines beginning with // are comments and are ignored by the compiler. They are used to explain the code.
#include <stdio.h>: This preprocessor directive includes the standard input-output library, which is necessary for using the printf function.
printf Function: This function prints the specified message to the console.
By following these steps and understanding these concepts, you can write, compile, and execute basic C programs.
Comments