Installation
1. Download MSYS2 Installer:
○ Go to the MSYS2 website and download the installer for Windows.
2. Install MSYS2:
○ Run the downloaded installer.
○ During installation, choose a directory (e.g., C:\msys64) and complete the
installation process.
3. Install MinGW-w64 Compiler:
○ In the terminal, run one of the following commands to install the desired
MinGW-w64:
For 64-bit Windows development:
pacman -S mingw-w64-x86_64-gcc
4. Set Up Environment Variables (Optional but Recommended):
○ You may want to add the MinGW-w64 bin folder to your system's PATH
environment variable for easy access to the gcc and g++ commands from
any terminal.
○ Open System Properties → Advanced → Environment Variables.
○ Under "System Variables", find the Path variable, and click Edit.
○ Add the path to the MinGW-w64 bin directory (e.g.,
C:\msys64\mingw64\bin for 64-bit).
5. Test the Installation:
Open a new terminal (either MSYS2 MinGW 64-bit or MSYS2 MinGW 32-bit) and run the
following command to check if gcc is available:
gcc --version
○ You should see the version of GCC installed.
Get Set Go
First C Program: "Hello, World!"
● Explain the Structure of a C Program:
Write a simple “Hello, World!” program and explain each part:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
○ Break it down:
■ #include <stdio.h>: Header files and their purpose.
■ int main(): Entry point of the program.
■ printf(): Introduce printf() for output and how strings work in C.
■ return 0: Explain that 0 indicates successful execution.
● Save the program with .c extension in current directory
● Open cmd prompt (windows-> type CMD ->enter)
● Navigate to the Directory Containing Your Program
Use the cd command to move to the directory where your .c file is saved:
cd path/to/your/c/file
○
● Compile the Program:
Use the gcc command to compile your program. For example, to compile hello.c, run:
gcc hello.c -o hello
■ hello.c: The source code file.
■ -o hello: This tells GCC to output an executable named hello (the
default is a.out on Linux/macOS or a.exe on Windows if you omit
the -o option).
● Run the Compiled Program:
○ After successful compilation, run the program:
On Linux/macOS:
./hello
On Windows:
hello.exe
Common GCC Options:
● -o: Specifies the name of the output file (executable).
-Wall: Enables all common warnings, which helps catch errors during compilation.
gcc -Wall hello.c -o hello
-g: Adds debugging information to the executable, useful when debugging with tools like
gdb.
gcc -g hello.c -o hello
Example:
gcc hello.c -o hello # Compile hello.c and output executable hello
./hello # Run the executable on Linux/macOS
hello.exe # Run the executable on Windows
This will compile and run your C program using gcc.
In C, the main() function is the entry point of any program. When you run a C program, the
operating system (OS) is responsible for invoking the main() function. Here's a more
detailed breakdown of the process:
1. Compilation and Linking:
● When you write and compile a C program, the source code (including the main()
function) is converted into machine code.
● The compiler (like gcc) translates the code into an executable file, which the
operating system can execute.
● During the linking phase, external libraries and the C runtime (which handles
program startup and shutdown) are linked with your program.
2. Program Execution:
● When you run the compiled program
hello.exe
the operating system loads the executable into memory.
● The OS allocates memory and sets up the environment for the program to run,
including initializing command-line arguments and handling system resources.
3. C Runtime Library (CRT):
● The C Runtime (CRT) is responsible for setting up the necessary environment
before calling the main() function. It’s part of the standard library that gets linked
with your program during compilation.
● The runtime performs several initializations like setting up stack space, handling
global variables, and preparing the arguments (argc, argv) that main() may use.
● After this setup, the CRT makes a call to the main() function.
4. Calling main():
● Once the runtime is set up, it calls your program’s main() function, which begins the
actual execution of your code.
The prototype of the main() function might look like this:
c
int main(int argc, char *argv[])
● Here, argc is the argument count, and argv is the array of arguments passed to the
program.
5. After main() Completes:
● After the main() function finishes execution and returns a value (typically 0 to
indicate successful execution), the CRT handles the cleanup process.
● This involves releasing any resources the program used and returning the exit status
back to the operating system.
Process Overview:
● Operating System → Loads the program → C Runtime Library → Initializes
environment → Calls main() → Your Program Executes → Returns control to CRT
→ CRT exits and sends status to the Operating System.
In short, the operating system loads your program, the C runtime initialises it, and the C
runtime calls your program's main() function.
printf() and scanf()
printf:
The printf function is used to print (or display) information to the screen. You can think
of it as a way to show output from your program.
Syntax:
printf("format_string", values...);
● "format_string": This is a string that contains text and format specifiers. Format
specifiers start with a % symbol and are placeholders for variables you want to print.
● values...: These are the variables or values that replace the format specifiers in
the output.
Example:
int age = 20;
printf("I am %d years old.\n", age);
scanf:
The scanf function is used to read input from the user. It allows the program to take user
input and store it in variables.
Syntax:
scanf("format_string", &variables...);
● "format_string": Similar to printf, but in scanf, it tells the function how to
interpret the user input.
● &variables...: These are the memory addresses (pointers) where the input
values will be stored.
Example:
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
In this example:
● scanf("%d", &age); reads an integer from the user and stores it in the variable
age.
● The & before the variable is used to pass the address of the variable, as scanf
needs to know where to store the input.
Input:
Enter your age: 25
Output:
You are 25 years old.
Variables in C
In C programming, variables are used to store data that can be manipulated throughout the
program. Each variable in C must be declared with a data type before it is used, which
specifies the kind of data it can hold (e.g., integer, floating-point number, character).
Key Rules for Variables in C
1. Declaring a Variable:
○ You must declare a variable with a specific data type before using it.
Example:
int age; // Declares an integer variable named 'age'
2. Assigning a Value to a Variable:
○ You can assign a value to a variable using the assignment operator =.
Example:
age = 25; // Assigns the value 25 to the variable 'age'
3. Initialization:
○ You can declare and assign a value to a variable in one statement.
Example:
int age = 25; // Declares and initializes 'age' with the value 25
○
4. Variable Naming Rules:
○ Must start with a letter or an underscore (_). Cannot start with a number.
○ Can only contain letters (uppercase or lowercase), digits, and
underscores.
○ Case-sensitive: age, Age, and AGE are different variables.
○ Cannot use reserved keywords (e.g., int, return, if, etc.).
5. Example of valid and invalid variable names:
○ Valid: age, height_in_cm, _total
○ Invalid: 2total, total%score, return
6. Scope of Variables:
○ Local variables: Declared inside a function and only accessible within that
function.
○ Global variables: Declared outside all functions and accessible throughout
the program.
7. Types of Variables in C:
int: Stores integers (whole numbers).
int age = 30;
float: Stores floating-point numbers (numbers with decimal points).
float price = 9.99;
char: Stores a single character.
char grade = 'A';
double: Stores double-precision floating-point numbers (larger decimal values).
double distance = 12345.678;
Example Program
#include <stdio.h>
int main() {
int age = 25; // Integer variable
float price = 19.99; // Floating-point variable
char grade = 'A'; // Character variable
printf("Age: %d\n", age);
printf("Price: %.2f\n", price);
printf("Grade: %c\n", grade);
return 0;
}
Important Notes:
● Data Type Matching: Always use the correct format specifiers in printf and scanf
for variables. For example, use %d for int, %f for float, and %c for char.
● Memory Efficiency: Choose the correct data type to save memory. For instance, use
float or double based on the precision you need.
Variables in C are fundamental to storing and manipulating data, and following these rules
ensures efficient and correct usage.