Installation
Installation
pacman -S mingw-w64-x86_64-gcc
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
#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:
On Linux/macOS:
./hello
On Windows:
hello.exe
-Wall: Enables all common warnings, which helps catch errors during compilation.
-g: Adds debugging information to the executable, useful when debugging with tools like
gdb.
Example:
gcc hello.c -o hello # Compile hello.c and output executable hello
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:
● 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:
hello.exe
● The OS allocates memory and sets up the environment for the program to run,
including initializing command-line arguments and handling system resources.
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
● Here, argc is the argument count, and argv is the array of arguments passed to the
program.
● 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:
In short, the operating system loads your program, the C runtime initialises it, and the C
runtime calls your program's main() function.
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:
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...);
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:
Output:
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).
Example:
Example:
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).
Example Program
#include <stdio.h>
int main() {
int age = 25; // Integer variable
float price = 19.99; // Floating-point variable
char grade = 'A'; // Character variable
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.