White Arrow Pointing To The Left Of The Screen
Blog
By
Manuel Aparicio
|
Related
Content
Back to Blog

Intro to C and C++ Development

19
Oct
2023

C and C++ are probably the most legendary and popular programming languages you may have heard of. However, even today, you’ll probably hear things like “they are old-fashioned languages” or “avoid them at all costs.” Some other jokes I’ve heard include, “Only learn them if you like to suffer” or “Some people surely use C and C++ in torture programs.” As a matter of fact, they’re nowhere near the most loved programming languages. Despite people turning them down, the truth is that they make the world go around in more ways than they can imagine.

C, often called the mother of all modern programming languages, inspired the syntax of C#, Java, JavaScript, and Swift. C++, on the other hand, brought us the power of C with some powerful features like OOP and Open-Source libraries. Additionally, it’s compatible with C, which explains why people describe it as “C on steroids” or “C’s evolution.” Let’s learn more about these powerful programming languages.

The Origin of C and C++ Development

Dennis Ritchie created C, the queen of Software Development, in the early 1970s at AT&T Bell Labs. She used it to build the famous UNIX operating system, which played a pivotal role in its widespread adoption, and it became standardized by ANSI in 1983. C is also responsible for some of the most important tools for software development, like VS code, Apache HTTP Server, Nginx, OpenSSL, Git, OpenGL, Vim, and MySQL. On top of that, they also used it to create Windows, Mac, and Linux Operating Systems. That should give you a rough idea of C's power and influence. Let’s move on to C++.

Like TypeScript and JavaScript, Bjarne Stroustrup created C++ as a superset of C. Also similar to C, C++ quickly gained popularity and massive adoption by becoming part of the foundation of some of the most important tools for Software Development, including MongoDB, Oracle, Google Chrome, Mozilla Firefox, Embedded Systems, and most Abode tools like Photoshop, InDesign, and After Effects.

What is C Programming Language?

C is a robust general-purpose, compiled, statically typed Programming Language popular for being a high-performance and procedural language. That means it’s extremely fast and perfect for precise memory control tasks, which also explains why they used it to build the world's most popular Operating Systems. In terms of abstraction and control, people categorize C as a mid-level language.

Its syntax is relatively easy to read and understand, and it doesn’t have a steep learning curve. Yet, mastering takes some time since it has few libraries and doesn’t support garbage collection and OOP. Besides, it also forces you to learn pointers, which you probably don’t know if you have been working with other Programming Languages.

C Development Main Features

C Development Pointers

As you may know, when you declare a variable, it is stored in database memory. A pointer is another variable that "points" to or holds the address of that variable or object. This way, pointers allow you to access and manipulate the specific places in memory that store your variables and objects. They're especially useful when working with functions because you can pass them pointers as parameters. That prevents you from having to copy the variable's value you want to pass as an argument. In other words, thanks to pointers, you can modify a variable by "reference" without copying it. It may take time to grasp this concept if you're new to programming.

Here's a simple way to think of them. Imagine memory as a collection of mailboxes, where each mailbox works as a variable and contains a number (address) you want to keep track of. A pointer would be like a Post-it note with the mailbox number written on it. Thus, the pointer doesn't store the mailbox; it tells you where to find it.

C Development Common Convention for Functions

In C programming, it’s a common practice for developers to write functions that return 1 when they failed deliberately and 0 if they succeeded. This convention is not mandatory. However, it helps check for errors and make the code more readable and maintainable. By following this convention, it becomes easy for developers to understand the return values of a function and handle any errors that may arise

Let’s take C pointers an example of how this convention works. Suppose there’s a function that takes a pointer as a parameter; if it returns 0, the operation is successful, and 1 means that it failed. In case of failure, the function can return an error code, which the calling function can then handle appropriately, which helps in making the code more robust and reliable.

C Development Basic Syntax

Please note that you’ll need an IDE or a text editor like VS Code and a C compiler like GCC to run the following code. Then, you’ll have to set your compiler up, specifying the compiler executable path and compiler flags. The last thing is creating a file with the extension “.c ”, such as myfile.c

#include <stdio.h>
// Define a struct to represent a coffee order

struct CoffeeOrder {
 char size; // 'S' for Small, 'M' for Medium, 'L' for Large
 int cream; // Amount of cream in milliliters
};

int main() {
 struct CoffeeOrder myCoffee; // Declare a variable of type CoffeeOrder to store the coffee order
 printf("Welcome to the Coffee Shop!\n");
 struct CoffeeOrder *ptr = &myCoffee; // Declare a pointer to CoffeeOrder and initialize it to point to myCoffee
// Customize the coffee order

 printf("Size (S/M/L): ");
 scanf(" %c", &ptr->size); // Read the coffee size and store it using the pointer
 printf("Cream (milliliters): ");
  scanf("%d", &ptr->cream); // Read the cream amount and store it using the pointer // Display the customized coffee order

 printf("\nHere's your coffee order:\n");
 printf("Size: %c\n", ptr->size); // Access and print the size using the pointer
  printf("Cream: %d milliliters\n", ptr->cream); // Access and print the cream amount using the pointer
 return 0;
}

The previous code uses the <stdio.h> header so you can access functions like “printf” and “scanf”, which you need for input/output operations. The keyword struct, which stands for structure, allows you to group variables of multiple data types. Some other functions, like “malloc”, will enable you to allocate memory dynamically. Note that since C involves manual memory management, you’ll have to free that space when you don’t need it with the free function. Otherwise, your code will cause memory leaks. Let’s move on to C++.

What is C++ Programming Language?

As mentioned above, C++ (C Plus Plus) is an extension of C, which means that any C app is technically a valid C++ program. It introduces many powerful tools and concepts related to OOP, like classes, inheritance, polymorphism, and encapsulation. In essence, it follows the foundation of C but with OOP capabilities. Its vast set of standard libraries and out-of-the-box tools make it relatively easy to start with. Nonetheless, it still has a very steep learning curve, according to most programmers. That’s why they don’t normally recommend it for beginners.

C++ Development Pointers and Smart Pointers

Pointers are a core aspect of C++ that works very similarly to C’s pointers. Yet, C++ introduces some core differences, like using a simpler syntax. You can use “references to variables” or “aliases” to access variables indirectly. Here’s a simple example.

int coffeeTemperature = 85;
// Declare an integer variable 'coffeeTemperature' and initialize it to 85

int &coffeeTemperatureRef = coffeeTemperature;
// 'coffeeTemperatureRef' is a reference to 'coffeeTemperature'

In addition, C++ brings more operators normally used for managing memory automatically during object creation and destruction. A perfect example is the “new” and “delete” operators that you can use for dynamic memory allocation and deallocation. Here, you can check a simple example.

int *coffeeBeans = new int;
// Allocate coffee beans (memory)
delete coffeeBeans;
// Discard coffee beans (deallocate memory)

Manual memory management can lead to potential memory leaks, which compromises performance. C++ Modern C++ (version 11 and above) uses Smart Pointers for automatic memory management. So, you no longer need to worry about freeing memory to avoid leaks. This is how a Smart Pointer looks in C++.

std::unique_ptr<int> ptr = std::make_unique<int>(42);
// Automatic Memory Mnagement

C++ Development Object-Oriented Programming

Object-Oriented Programming Language is one of the most common paradigms in high-level languages like Python, Ruby, Java, and C#. It involves objects encompassing several attributes and classes, which are blueprints of these objects. With OPP, C++ promotes code scalability, maintainability, and reusability. Take a look at a basic example.

#include <iostream>
// Define a class named 'Coffee'

class Coffee {
public:
 // Constructor to initialize the 'coffeeType' attribute
 Coffee(const std::string& type) : coffeeType(type) {
 }

 // Method to brew the coffee
 void brew() {
 std::cout << "Brewing a cup of " << coffeeType << " coffee..." << std::endl;
 }

private:
 std::string coffeeType; // Attribute to store the type of coffee
 };

int main() {
 // Create an instance of the 'Coffee' class (an object)
 Coffee espresso("Espresso");
 // Call the 'brew' method on the 'espresso' object
 espresso.brew();
 return 0;
 }

C++ Development Function Overloading

Function overloading implies creating functions with the same name but taking different parameters that can differ in order, data types, and even numbers. That sounds pretty cool, but when is it useful? Why is it so powerful? Function overloading can help improve your code's adaptability and reusability and simplify working with APIs.

#include <iostream>
#include <string>
// Function to prepare a cup of coffee with default sugar

 void prepareCoffee(const std::string& coffeeType) {
 std::cout << "Preparing a cup of " << coffeeType << " coffee without sugar." << std::endl;
 }

// Overloaded function to prepare a cup of coffee with a specific amount of sugar
 void prepareCoffee(const std::string&coffeeType, int sugarSpoons) {  std::cout << "Preparing a cup of " << coffeeType << " coffee with " << sugarSpoons << " sugar spoon(s)." << std::endl;
 }

int main() {  // Call the overloaded functions to prepare coffee
 prepareCoffee("Espresso"); // Without sugar
 prepareCoffee("Cappuccino", 2); // With 2 sugar spoons
 return 0;
 }

Conclusion

C and C++ are two cornerstones of Software Development that can help you bring almost any product to life, guaranteeing world-class performance. Even though they came out decades ago, they’re two of the fastest languages and use the least energy. In other words, they’re also extremely eco-friendly compared to most languages.