day | title | description |
---|---|---|
1 |
Refresher to C programming |
History of C, basic syntax, hello-world, operators and expressions |
Installing a compiler: GCC (MinGW)
Install an IDE/Editor : Visual Studio Code
Note:: If gcc
isn't recognized as a command, make sure that you've added its bin folder's path as an environment variable.
-
Preprocessor Directives: Preprocessor directives are lines included in the code that are not program statements but instructions for the preprocessor. They begin with the # symbol. The most common preprocessor directive is
#include
.Example:
#include<stdio.h>
,#define PI 3.142
-
main() Function: Every C program must have a main function. This is the entry point of the program where execution begins.
#include<stdio.h>
void main(){
//your code here
}
- Statement Termination with
;
: In C, each statement must end with a semicolon (;
). This tells the compiler that the statement is complete.
printf("Hello");
//semicolon marks the end of the statement
- Comments: Comments are used to explain the code and are ignored by the compiler. They make the code more readable.
//This is a single line comment
printf("Hello");
/*
This is a
multiline comment
*/
printf("There");
#include<stdio.h> //standard input-output header
void main(){
printf("Hello World"); //simple print statement
}
Variables are used to store data. Each variable must be declared with a specific data type.
int
: Integer type
float
: Floating-point type
char
: Character type
double
: Long-floating-point type
void
: void type
int num = 10; // Integer variable
float price = 9.99; // Floating-point variable
char grade = 'A'; // Character variable
- Input and Output
C provides standard functions to take input and display output.
printf
: Used to print output to the screen.
printf("Hello, World!\n");
scanf
: Used to take input from the user.
int n;
scanf("%d",&n);
Here %d
is the format specifier used for integer datatype.
Some other format specifiers are
%f
-float%c
-char%lf
- long float/double%s
- string/character_array
Arithmetic operators are used to perform basic mathematical operations.
int a = 5;
int b = 3;
int sum = a + b;
printf("Sum: %d\n", sum); // Output: Sum: 8
int a = 5;
int b = 3;
int difference = a - b;
printf("Difference: %d\n", difference); // Output: Difference: 2
int a = 5;
int b = 3;
int product = a * b;
printf("Product: %d\n", product); // Output: Product: 15
int a = 6;
int b = 3;
int quotient = a / b;
printf("Quotient: %d\n", quotient); // Output: Quotient: 2
int a = 5;
int b = 3;
int remainder = a % b;
printf("Remainder: %d\n", remainder); // Output: Remainder: 2
Relational operators are used to compare two values.
int a = 5;
int b = 3;
printf("%d\n", a == b); // Output: 0 (false)
printf("%d\n", a != b); // Output: 1 (true)
printf("%d\n", a > b); // Output: 1 (true)
printf("%d\n", a < b); // Output: 0 (false)
printf("%d\n", a >= b); // Output: 1 (true)
printf("%d\n", a <= b); // Output: 0 (false)
Logical operators are used to combine multiple conditions.
int a = 5;
int b = 3;
int c = 5;
printf("%d\n", (a > b) && (a == c)); // Output: 1 (true)
printf("%d\n", (a < b) || (a == c)); // Output: 1 (true)
printf("%d\n", !(a == c)); // Output: 0 (false)
Increment and decrement operators are used to increase or decrease the value of a variable by one.
int a = 5;
a++; // Postfix increment
printf("a: %d\n", a); // Output: a: 6
++a; // Prefix increment
printf("a: %d\n", a); // Output: a: 7
int a = 5;
a--; // Postfix decrement
printf("a: %d\n", a); // Output: a: 4
--a; // Prefix decrement
printf("a: %d\n", a); // Output: a: 3
Assignment operators are used to assign values to variables.
int a = 5;
int a = 5;
a += 3; // Equivalent to a = a + 3
printf("a: %d\n", a); // Output: a: 8
a -= 2; // Equivalent to a = a - 2
printf("a: %d\n", a); // Output: a: 6
a *= 2; // Equivalent to a = a * 2
printf("a: %d\n", a); // Output: a: 12
a /= 3; // Equivalent to a = a / 3
printf("a: %d\n", a); // Output: a: 4
a %= 3; // Equivalent to a = a % 3
printf("a: %d\n", a); // Output: a: 1
Type casting and conversion allow you to change a variable's data type.
Automatically performed by the compiler when necessary.
int a = 5;
float b = 2.5;
float result = a + b; // a is implicitly converted to float
printf("Result: %.2f\n", result); // Output: Result: 7.50
Manually performed by the programmer.
int a = 5;
int b = 2;
float result = (float)a / b; // a is explicitly cast to float
printf("Result: %.2f\n", result); // Output: Result: 2.50