Skip to content

Latest commit

 

History

History
232 lines (189 loc) · 5.54 KB

Day 1.md

File metadata and controls

232 lines (189 loc) · 5.54 KB
day title description
1
Refresher to C programming
History of C, basic syntax, hello-world, operators and expressions

Day 1 - Refresher to C Programming

Setting up the Environment

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.

Basic Syntax and Structure:

  1. 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

  2. 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
}
  1. 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
  1. 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");

Writing and running the first program ("Hello, World!")

#include<stdio.h> //standard input-output header
void main(){
    printf("Hello World"); //simple print statement
}

Variables and Data Types: Integers, floats, characters, and basic I/O functions

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
  1. 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

Operators and Expressions

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Addition

int a = 5;
int b = 3;
int sum = a + b;
printf("Sum: %d\n", sum);  // Output: Sum: 8

Subtraction

int a = 5;
int b = 3;
int difference = a - b;
printf("Difference: %d\n", difference);  // Output: Difference: 2

Multiplication

int a = 5;
int b = 3;
int product = a * b;
printf("Product: %d\n", product);  // Output: Product: 15

Division

int a = 6;
int b = 3;
int quotient = a / b;
printf("Quotient: %d\n", quotient);  // Output: Quotient: 2

Modulus

int a = 5;
int b = 3;
int remainder = a % b;
printf("Remainder: %d\n", remainder);  // Output: Remainder: 2

Relational and Logical Operators

Relational Operators

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

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

Increment and decrement operators are used to increase or decrease the value of a variable by one.

Increment

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

Decrement

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

Assignment operators are used to assign values to variables.

Basic Assignment

int a = 5;

Compound Assignment

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 Type Conversion

Type casting and conversion allow you to change a variable's data type.

Implicit Type Conversion

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

Explicit Type Casting

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