C programming tutorial

C programming tutorial

C programming tutorial

Basics of C Programming

Welcome to the C programming full course! In this course, we will cover all the fundamentals of the C programming language, from basic syntax and data types to advanced topics such as memory management and pointers.

Before we get started, it’s important to note that this course assumes some prior programming experience. If you’re completely new to programming, you may want to start with an introductory course or tutorial before diving into C.

Now, let’s get started with the basics!

Lesson 1: Getting Started with C

What is C?

C is a general-purpose, high-level programming language that was developed in the 1970s by Dennis Ritchie at Bell Labs. It is a powerful language that is used for developing operating systems, compilers, and many other software applications.

Setting Up Your Environment

Before you can start coding in C, you will need to set up your development environment. There are many different text editors and IDEs (Integrated Development Environments) available for C programming, but we’ll be using Visual Studio Code in this course.

  1. Download and install Visual Studio Code: https://code.visualstudio.com/
  2. Install the C/C++ extension for Visual Studio Code: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

Your First C Program

Now that you have your environment set up, let’s write your first C program! In C, the traditional “Hello, world!” program looks like this:

#include <stdio.h>

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

Let’s break this down:

  • #include <stdio.h>: This line includes the standard input/output library, which provides the printf function.
  • int main(): This is the entry point of the program. Every C program must have a main function.
  • printf("Hello, world!\n");: This line uses the printf function to print the string “Hello, world!” to the console. The \n at the end is a newline character, which moves the cursor to the next line.
  • return 0;: This line returns a value of 0 to the operating system, indicating that the program completed successfully.

Compiling and Running Your Program

Now that you’ve written your first C program, let’s compile and run it!

  1. Open Visual Studio Code and create a new file called hello.c.
  2. Copy and paste the “Hello, world!” program into the file.
  3. Save the file.
  4. Open a terminal window and navigate to the directory where you saved hello.c.
  5. Type gcc hello.c -o hello and press Enter. This will compile your program and create an executable file called hello.
  6. Type ./hello and press Enter. This will run your program, and you should see the output “Hello, world!” printed to the console.

Congratulations, you’ve written and run your first C program!

Lesson 2: Variables and Data Types

Variables

In C, variables are used to store data values. Before you can use a variable, you must declare it. To declare a variable, you need to specify its data type and give it a name. For example:

int age;

This declares a variable named age of type int.

Data Types

C has several built-in data types, including:

  • int: An integer value.
  • float: A floating-point value.
  • double: A double-precision floating-point value.
  • char: A single character value.
  • bool: A Boolean value (true or false).

Constants

In C, you can also define constants, which are values that cannot be changed during the execution of the program. Constants are declared using the const keyword, like this:

const int MAX_AGE = 100;

This declares a constant named MAX_AGE with a value of 100.

Operators

C provides several operators for performing arithmetic, logical, and comparison operations on variables and constants. Some of the most commonly used operators include:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder after division)
  • ++: Increment
  • --: Decrement
  • ==: Equal to
  • !=: Not equal to
  • <: Less than
  • >: Greater than
  • <=: Less than or equal to
  • >=: Greater than or equal to
  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

Example Program

Let’s look at an example program that uses variables, data types, constants, and operators:

#include <stdio.h>

int main() {
    int age = 30;
    float weight = 72.5;
    const int MAX_AGE = 100;
    bool isOverMaxAge = age > MAX_AGE;
    printf("Age: %d\n", age);
    printf("Weight: %.1f\n", weight);
    printf("Is over max age? %d\n", isOverMaxAge);
    return 0;
}

This program declares an integer variable named age, a floating-point variable named weight, and a constant named MAX_AGE. It also uses the comparison operator (>) to set a Boolean variable named isOverMaxAge based on whether age is greater than MAX_AGE. Finally, it uses the printf function to print the values of these variables to the console.

Lesson 3: Control Structures

If-Else Statements

One of the most important control structures in C is the if-else statement, which allows you to conditionally execute code based on a Boolean expression. Here’s an example:

cCopy code#include <stdio.h>

int main() {
    int age = 30;
    if (age >= 18) {
        printf("You are an adult\n");
    } else {
        printf("You are a child\n");
    }
    return 0;
}

This program uses an if-else statement to check whether age is greater than or equal to 18. If it is, it prints “You are an adult”. Otherwise, it prints “You are a child”.

Loops

C provides several types of loops for repeating code:

  • while: Executes a block of code as long as a Boolean expression is true.
  • do-while: Executes a block of code at least once, and then repeatedly as long as a Boolean expression is true.
  • for: Executes a block of code a specified number of times.

Here’s an example program that uses a for loop to print the numbers 1 to 10:

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        printf("%d\n", i);
    }
    return 0;
}

Switch Statements

Another important control structure in C is the switch statement, which allows you to execute different code blocks depending on the value of a variable or expression. Here’s an example:

#include <stdio.h>

int main() {
    int day = 1;
    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            printf("Invalid day\n");
            break;
    }
    return 0;
}

This program uses a switch statement to print the name of a day of the week based on the value of the variable day.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top