Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

C/C++ Development Tutorial

Introduction to C/C++

C and C++ are powerful programming languages widely used for system and application software development. C is a procedural programming language, while C++ extends C with object-oriented features. This tutorial will guide you through the basics of C/C++ development, using Eclipse IDE for coding.

Setting Up Your Development Environment

Before you start coding in C/C++, you need to set up your development environment. Eclipse is a popular IDE that supports C/C++ development. Here’s how to install it:

  1. Download Eclipse IDE for C/C++ Developers from the official Eclipse website.
  2. Install Eclipse by following the installation instructions.
  3. Install a C/C++ compiler like GCC. Make sure it is added to your system’s PATH.
  4. Launch Eclipse and create a new C/C++ project.

Your First C Program

Let’s write a simple "Hello, World!" program in C. Follow these steps:

  1. Open Eclipse and create a new C project.
  2. Create a new source file named hello.c.
  3. Write the following code:
  4. #include <stdio.h> int main() { printf("Hello, World!\\n"); return 0; }
  5. Run the program by clicking the run button or pressing Ctrl + F11.

The output will be:

Hello, World!

Understanding Variables and Data Types

In C/C++, variables are used to store data. Each variable has a type that determines the size and layout of the variable's memory. Common data types include:

  • int - Integer type.
  • float - Floating point type.
  • double - Double precision floating point type.
  • char - Character type.

Here’s an example:

#include <stdio.h> int main() { int age = 25; float height = 5.9; char initial = 'A'; printf("Age: %d\\n", age); printf("Height: %.1f\\n", height); printf("Initial: %c\\n", initial); return 0; }

Output:

Age: 25
Height: 5.9
Initial: A

Control Structures

C/C++ provides several control structures to manage the flow of execution. The most common are:

  • If statements - Used for conditional execution.
  • Loops - for, while, and do-while loops for repeated execution.

Example of an if statement and a for loop:

#include <stdio.h> int main() { int number = 10; if (number > 0) { printf("Positive number\\n"); } for (int i = 0; i < 5; i++) { printf("Iteration %d\\n", i); } return 0; }

Output:

Positive number
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

Functions in C/C++

Functions are blocks of code that perform a specific task. They help in organizing code and promoting code reusability. Here’s how to define and call a function:

#include <stdio.h> void greet() { printf("Hello from the function!\\n"); } int main() { greet(); // Function call return 0; }

Output:

Hello from the function!

Introduction to Object-Oriented Programming in C++

C++ supports object-oriented programming (OOP), which allows for the creation of classes and objects. Here’s a simple class example:

#include <iostream> class Car { public: void honk() { std::cout << "Beep beep!" << std::endl; } }; int main() { Car myCar; myCar.honk(); // Call the method return 0; }

Output:

Beep beep!

Conclusion

This tutorial covered the essentials of C/C++ development, including environment setup, syntax, control structures, functions, and basic OOP concepts in C++. With this foundation, you can delve deeper into more advanced topics such as pointers, templates, and libraries. Happy coding!