Basic structure of C syntex of C programming with ...
Basic structure of C syntex of C programming with ...
int main() {
printf("Hello, world!\n");
return 0;
}
2. Operators in C
Operators are symbols that perform operations on data. Here are the main types:
● Arithmetic Operators: +, -, *, /, % (modulo)
● Relational Operators: == (equals), != (not equals), >, <, >=, <=
● Logical Operators: && (and), || (or), ! (not)
● Assignment Operators: =, +=, -=, *=, /=, %=
● Bitwise Operators: & (and), | (or), ^ (xor), ~ (complement), << (left shift), >> (right shift)
● Increment/Decrement Operators: ++, --
● Conditional Operator (Ternary Operator): condition ? expression1 : expression2
3. Comments in C
Comments are used to explain code and are ignored by the compiler.
● Single-line comments: // This is a single-line comment
● Multi-line comments: /* This is a multi-line comment */
4. Conditional Statements
● if statement: Executes a block of code if a condition is true.
if (condition) {
// Code to execute if the condition is true
}
● if-else statement: Executes one block of code if a condition is true and another block if
it's false.
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
● nested if and if-else statements: Allows you to embed if or if-else statements within
other if or if-else statements for more complex logic.
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
} else {
// Code to execute if condition1 is true but condition2 is
false
}
} else {
// Code to execute if condition1 is false
}
5. Switch Statement
The switch statement provides a way to execute different code blocks based on the value of an
expression.
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if expression doesn't match any case
}
6. Loop Control
● for loop: Repeats a block of code a specific number of times.
for (initialization; condition; update) {
// Code to execute repeatedly
}
● do-while loop: Similar to a while loop, but the code block is executed at least once
before the condition is checked.
do {
// Code to execute repeatedly
} while (condition);
● Nested loops: You can place loops inside other loops to create more complex iterations.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
// Code to execute in the inner loop
}
}
7. Functions
Functions are self-contained blocks of code that perform specific tasks.
● Definition:
return_type function_name(parameter_list) {
// Function body (code to execute)
return value; // (if the return type is not void)
}
● Example:
int add(int a, int b) {
return a + b;
}
● Function Properties:
○ Modularity: Break down complex problems into smaller, manageable parts.
○ Reusability: Use the same function multiple times in a program.
○ Maintainability: Easier to debug and update code.
● Function Types:
○ Library functions: Predefined functions (e.g., printf, scanf)
○ User-defined functions: Functions created by the programmer.
● Argument vs. Parameter:
○ Parameter: A variable in the function definition.
○ Argument: The actual value passed to the function when it's called.
8. Recursion
Recursion is a technique where a function calls itself.
● Definition:
void recursive_function(parameters) {
if (base_case) {
// Stop the recursion
} else {
recursive_function(modified_parameters);
}
}
● Example (factorial):
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
● Properties of Recursion:
○ Must have a base case to stop the recursion.
○ Each recursive call should move towards the base case.
○ Can be less efficient than iterative solutions (loops) due to function call overhead.
9. Pointers
A pointer is a variable that stores the memory address of another variable.
● Definition:
data_type *pointer_name;
● Example:
int num = 10;
int *ptr = # // ptr stores the address of num
● Pointers in Functions:
○ Call by value: A copy of the argument is passed to the function. Changes made
inside the function don't affect the original variable.
○ Call by reference: The address of the argument is passed to the function.
Changes made inside the function affect the original variable.
void swapByValue(int x, int y) {
int temp = x;
x = y;
y = temp;
}
10. Arrays
An array is a collection of elements of the same data type stored in contiguous memory
locations.
● Definition:
data_type array_name[size];
● Example:
int numbers[5] = {1, 2, 3, 4, 5};
11. Strings
A string in C is an array of characters terminated by a null character (\0).
● Definition:
char string_name[size];
● Example:
char message[] = "Hello";
● Example:
struct Student {
char name[50];
int roll_no;
float marks;
};
● Initialization of Structures:
struct Student s1 = {"John Doe", 123, 85.5};
● Pointer to Structures: You can use pointers to access and manipulate structures.
● Arrow Operator (->): Used to access members of a structure using a pointer to the
structure.
struct Student s1;
struct Student *ptr = &s1;
ptr->roll_no = 123; // Accessing roll_no using the arrow operator
Important Notes:
● This is a comprehensive overview of the basic concepts of C programming.
● There are many more advanced topics and libraries in C that you can explore as you
learn more.
● Practice writing code regularly to solidify your understanding.
● Use online resources, tutorials, and books to deepen your knowledge.
● Don't hesitate to ask for help if you get stuck!
● https://github.com/SwadhinPanda/code.iblogger
● https://github.com/astajyoti1/Python-Training
● https://history-computer.com/how-to-use-switch-case-in-python/
● https://kuchtoseekho.com/javascript-2/