C Programming Basics – C Language Tutorial For Beginners (2024)

C is a general-purpose, procedural programming language developed in 1972 by Dennis Ritchie at Bell Laboratories. It is one of the most widely used programming languages and is the foundation for many other programming languages like C++, Java, and Python.

C programming is an excellent choice for beginners because it is a structured, low-level language that provides a solid understanding of how computers work and how to manage system resources like memory and CPU cycles efficiently.

Getting Started with C

Before coding in C, you must set up a development environment. You can use an Integrated Development Environment (IDE) like Code::Blocks, Eclipse, or Visual Studio Code. Alternatively, you can use a text editor like Sublime Text or Notepad++ and compile your code using a command-line compiler like GCC (GNU Compiler Collection).

Writing Your First C Program

Every C program starts with a main() function, which is the program's entry point. Here's a simple "Hello, World!" program in C:

#include <stdio.h>

int main() {

printf("Hello, World!");

return 0;

}

  • #include <stdio.h> is a preprocessor directive that includes the standard input/output library, which provides functions like printf().
  • int main() is the main function where the program execution begins.
  • printf("Hello, World!"); is a function that prints the string "Hello, World!" to the console.
  • return 0; indicates that the program executed successfully. A non-zero value typically means an error occurred.

Data Types and Variables

In C, you need to declare variables and specify their data types before using them. Here are some of the basic data types in C:

  • int: Stores integer values (whole numbers)
  • float: Stores floating-point numbers (numbers with decimals)
  • double: Stores double-precision floating-point numbers
  • char: Stores single characters
  • _Bool (C99 and later): Stores boolean values (true or false)

To declare a variable, you specify the data type followed by the variable name:

int age = 25;

float price = 9.99;

char grade = 'A';

_Bool isStudent = true;

Operators and Expressions

C supports various operators for performing arithmetic, assignment, comparison, logical, and bitwise operations. Here are some examples:

int x = 5, y = 3;

// Arithmetic operations

int sum = x + y; // sum = 8

int diff = x - y; // diff = 2

int prod = x * y; // prod = 15

int quot = x / y; // quot = 1 (integer division)

int rem = x % y; // rem = 2 (remainder)

// Assignment operators

x += 2; // Equivalent to x = x + 2; x is now 7

y *= 3; // Equivalent to y = y * 3; y is now 9

// Comparison operators

_Bool isEqual = (x == y); // isEqual is false

_Bool isGreater = (x > y); // isGreater is false

// Logical operators

_Bool andResult = (x > 0) && (y < 10); // andResult is true

_Bool orResult = (x < 0) || (y > 10); // orResult is false

Control Flow

Control flow statements in C allow you to control the execution flow of your program based on certain conditions or loops.

Conditional Statements

The if statement is used to execute a block of code if a certain condition is met:

int age = 18;

if (age >= 18) {

printf("You are an adult.\n");

} else {

printf("You are a minor.\n");

}

The switch statement is an alternative to multiple if...else statements:

char grade = 'B';

switch (grade) {

case 'A':

printf("Excellent!\n");

break;

case 'B':

printf("Good job!\n");

break;

case 'C':

printf("You can do better.\n");

break;

default:

printf("Invalid grade.\n");

}

Loops

Loops allow you to repeat a block of code multiple times.

The for loop is used when you know the number of iterations:

int sum = 0;

for (int i = 1; i <= 10; i++) {

sum += i;

}

printf("The sum of the first 10 numbers is %d.\n", sum);

The while loop is used when the number of iterations is not known in advance:

int number = 1;

while (number <= 5) {

printf("%d\n", number);

number++;

}

The do...while loop is similar to the while loop, but it executes the loop body at least once:

int number = 10;

do {

printf("%d\n", number);

number++;

} while (number <= 5);

Functions

Functions in C allow you to break down your code into smaller, reusable parts. A function is a block of code that performs a specific task and can optionally take input parameters and return a value.

Here's an example of a function that calculates the square of a number:

int square(int num) {

return num * num;

}

int main() {

int x = 5;

int result = square(x);

printf("The square of %d is %d.\n", x, result);

return 0;

}

Functions can also have multiple parameters and return different data types.

FAQs

What is the difference between C and C++?

C is a procedural programming language, while C++ is an object-oriented programming language that is based on C. C++ extends the functionality of C by adding features like classes, objects, inheritance, and polymorphism. However, C++ is backward-compatible with C, meaning that valid C code can be compiled and run in a C++ compiler.

What is the use of the #include directive in C?

The #include directive is a preprocessor command that instructs the compiler to include the contents of a specified header file into the source code file. Header files typically contain function declarations, macro definitions, and other useful code that can be shared across multiple source files.

What is the difference between printf() and scanf() functions in C?

The printf() function is used to print formatted output to the console or a file, while the scanf() function is used to read formatted input from the console or a file. Both functions use format specifiers (like %d for integers, %f for floats, and %s for strings) to specify how the data should be formatted or interpreted.

What is the purpose of the break statement in C?

The break statement is used to exit from a loop or a switch statement. In a loop, it terminates the current iteration and transfers control to the statement following the loop. In a switch statement, it causes the program to exit the switch block after executing the matching case.

What is the difference between call by value and call by reference in C?

In C, when you pass a value to a function, it is passed by value by default. This means that a copy of the value is created and passed to the function, and any changes made to the parameter inside the function do not affect the original value in the calling code.

On the other hand, call by reference involves passing the memory address of a variable to a function. This allows the function to modify the original value directly, rather than working with a copy.

C Programming Basics – C Language Tutorial For Beginners (2024)
Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5435

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.