C
C

C

Updated
Mar 5, 2024 02:46 PM
Category
Programing
Status
Open

C Windows Setup

Download the GCC Compiler

C Linux Setup

Ensure GCC is installed

Make sure your Linux distribution have GCC installed
gcc -v
If GCC isn't installed, run the following command from the terminal window to update the Ubuntu
sudo apt-get update
Next install the GNU compiler tools and the GDB debugger with this command:
sudo apt-get install build-essential gdb

VSCode Setup

  • Download Code Runner and C++ extension pack from VSCode extensions.
  • Create and open a C HelloWorld.c File in any folder.
Go to Terminal and select Configure Default Build Task.
notion image
Select the compiler.
In Windows select C/C++: gcc.exe build active file:
notion image
 
In Linux select g++ build and debug active file:
notion image
 
Basic Hello World starting code
#include <stdio.h>

int main() {
    printf("Hello World");
}
 

C Variable Declaration

Variable Declaration

In C, a variable declaration consists of the following parts:
  • Data Type: Specifies the type of data the variable can hold. Examples include int, float, char, and user-defined types.
  • Variable Name: The name of the variable, which is used to identify and access it in the program. Variable names must follow certain rules:
    • Must start with a letter (uppercase or lowercase) or underscore (_).
    • Can contain letters, digits, and underscores.
    • Case-sensitive.
  • Semicolon (;): A statement in C ends with a semicolon, including variable declarations.

Example Variable Declarations

Here are some examples of variable declarations:
Data Type
Variable Name
Declaration
int
age
int age;
float
pi
float pi;
char
initial
char initial;
double
price
double price;

Variable Initialization

Initialization is the process of assigning an initial value to a variable at the time of declaration. In C, you can initialize a variable when you declare it by using the assignment operator (=).

Example Variable Initializations

Here are some examples of variable declarations with initialization:
Data Type
Variable Name
Initialization
int
count
int count = 0;
float
temperature
float temperature = 98.6;
char
grade
char grade = 'A';
double
balance
double balance = 1000.0;

Rules and Tips

  • Variables should be declared and initialized before they are used in your program.
  • Uninitialized variables contain garbage values.
  • Local variables (inside functions) are not automatically initialized, so it's essential to initialize them explicitly.
  • Global variables (outside functions) are initialized to zero by default.

C Data Types and Format Specifiers

Basic Data Types

C provides several basic data types, which can be categorized into the following groups:
  1. Integer Types: Used for storing whole numbers.
  1. Floating-Point Types: Used for storing numbers with a fractional part.
  1. Character Types: Used for storing individual characters.
  1. Void Type: Represents the absence of a value.

Integer Types

Integer data types are used for representing whole numbers (positive and negative) without a fractional part.
Data Type
Description
Range
Format Specifier
int
Standard integer type
-32,768 to 32,767 (at least)
%d
short
Short integer
-32,768 to 32,767 (at least)
%hd
long
Long integer
-2,147,483,648 to 2,147,483,647 (at least)
%ld
long long
Long long integer
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (at least)
%lld
unsigned int
Unsigned integer
0 to 65,535 (at least)
%u

Floating-Point Types

Floating-point data types are used for representing real numbers with fractional parts.
Data Type
Description
Format Specifier
float
Single-precision float
%f
double
Double-precision float
%lf
long double
Extended-precision float
%Lf

Character Types

Character data types are used for representing individual characters.
Data Type
Description
Format Specifier
char
Character (1 byte)
%c
signed char
Signed character
%c
unsigned char
Unsigned character
%c

Void Type

The void type is used to indicate the absence of a specific type. It's often used in functions that don't return a value or for generic pointers.
Data Type
Description
Example Declaration
Format Specifier
void
Void (no value)
void displayMessage();
N/A
void*
Generic pointer
void* dataPtr;
N/A

C Arithmetic Operators

Arithmetic operators in C are used to perform mathematical operations on variables and constants.

Basic Arithmetic Operators

C provides several basic arithmetic operators that can be used with numeric data types. Here are the common arithmetic operators along with their symbols:
Operator
Description
Symbol
Example
Addition
Adds two operands
+
result = x + y;
Subtraction
Subtracts right operand from the left
-
result = x - y;
Multiplication
Multiplies two operands
*
result = x * y;
Division
Divides left operand by the right
/
result = x / y;
Modulus
Computes the remainder after division
%
result = x % y;

Division Operator - Caveats and Considerations

While division is a common mathematical operation, there are some important considerations to keep in mind when using this operator in C programs.

Division by Zero

One of the most critical caveats in C division is dividing by zero. When you attempt to divide by zero, it results in undefined behavior, and your program may crash or produce unexpected results.
Example
Description
Result
x / 0
Division by zero
Undefined
0 / 0
Zero divided by zero
Undefined
5 / 0
Non-zero divided by zero
Undefined
To prevent division by zero errors, it's essential to check the divisor before performing the division operation.
if (divisor != 0) {
    result = dividend / divisor;
} else {
    // Handle the error or report it
}

Integer Division

Another important consideration is integer division. When you divide two integers using the division operator, the result will also be an integer, and any fractional part is truncated (discarded). This behavior can lead to unexpected results if not handled correctly.
Example
Description
Result
5 / 2
Integer division
Result: 2
7 / 3
Integer division
Result: 2
To obtain a floating-point result from integer division, you can cast one or both operands to a floating-point type before the division.
float result = dividend / (float)divisor;

Assignment Operators

Assignment operators are used to assign the result of an operation to a variable. They combine an arithmetic operation with assignment.
Operator
Description
Symbol
Example
Assignment
Assigns the value of the right operand to the left
=
x = 5;
Addition Assignment
Adds the right operand to the left and assigns the result
+=
x += 3; (Equivalent to x = x + 3;)
Subtraction Assignment
Subtracts the right operand from the left and assigns the result
-=
x -= 2; (Equivalent to x = x - 2;)
Multiplication Assignment
Multiplies the left by the right and assigns the result
*=
x *= 4; (Equivalent to x = x * 4;)
Division Assignment
Divides the left by the right and assigns the result
/=
x /= 2; (Equivalent to x = x / 2;)
Modulus Assignment
Computes the remainder after division and assigns the result
%=
x %= 3; (Equivalent to x = x % 3;)

Increment and Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable by 1.
Operator
Description
Symbol
Example
Increment
Increases the operand by 1
++
x++;
Decrement
Decreases the operand by 1
--
y--;