Table of contents
C is a compiled, low-level programming language that is known for its speed and efficiency. It is widely used in operating systems, embedded systems, and scientific applications. In this post, we'll provide an introduction to programming in C, including its syntax, data types, control structures, and functions.
Syntax
C syntax is based on curly braces, semicolons, and parentheses. A C program starts with the main() function, which is where the program execution begins. Here's an example of a simple "Hello, World!" program in C:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
This program includes the standard input/output library (stdio.h) and uses the printf() function to print the message "Hello, World!" to the screen.
Data Types
C has several built-in data types, including:
int: integers (whole numbers)
float: floating-point numbers (numbers with decimal points)
char: characters (single letters or symbols)
double: double-precision floating-point numbers
void: an empty data type
Variables can be declared using these data types, like this:
int age = 30;
float weight = 65.5;
char initial = 'J';
Control Structures
C provides several control structures that allow you to control the flow of your program. These include:
if...else: executes a block of code if a condition is true, and another block of code if it's false
for: executes a block of code a specific number of times
while: executes a block of code as long as a condition is true
do...while: executes a block of code at least once, and then continues as long as a condition is true
switch: executes a block of code depending on the value of a variable
Here's an example of an if...else statement in C:
if (age >= 18)
{
printf("You are an adult.");
}
else
{
printf("You are a minor.");
}
Functions
Functions are blocks of code that can be called from other parts of your program. They allow you to reuse code and make your program more modular. In C, functions are declared like this:
add(int x, int y)
{
int result = x + y;
return result;
}
This function takes two integer arguments (x and y), adds them together, and returns the result.
Conclusion
C is a powerful programming language that is still widely used 5 decades after its inception. If you're interested in more, check out K & R book. Happy coding!