Skip to content

yusrinayusri/snippets-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

snippets-C

My code snippets in C. I learn and I code and I write.

 -. .-.   .-. .-.   .-. .-.   .
  \   \ /   \   \ /   \   \ /
 / \   \   / \   \   / \   \
~   `-~ `-`   `-~ `-`   `-~ `-

Syntax of C

Syntax Elements

  1. Keywords (reserved words in C)
int, return, if, else, for, while, do, break, continue, switch, case, default, 
void, char, float, double, struct, typedef, const, 
unsigned, signed, static, extern, sizeof
  1. Identifiers

Names for variables, functions, arrays, etc., must start with a letter or underscore and can be followed by letters, digits, or underscores

int myVariable;
float _height;
char name6;
  1. Basic data types
int a =  27;
float b = 19.97;
double c = 3.14159;
char d = 'Y';
  1. Variable declaration & initialization
int age =  27;
float height 1.68;
char grade = 'A';
  1. Constants
const int MAX = 100;
#define PI 3.14159
  1. Operators

Arithmetic

+, -, *, /, %

Relational

==, !=, <, >, >=, <=

Logical

&&, ||, 

Bitwise

&, |, ^, ~, <<, >>

Assignment

=, +=, -=, *=, /=, %=

Increment/Decrement

++, --
  1. Conditional statements
if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

switch (variable) {
    case value1:
        // code to execute if variable == value1
        break;
    case value2:
        // code to execute if variable == value2
        break;
    default:
        // code to execute if variable doesn't match any case
}
  1. Loops
// For loop
for (initialization; condition; increment) {
    // code to execute in the loop
}

// While loop
while (condition) {
    // code to execute in the loop
}

// Do-while loop
do {
    // code to execute in the loop
} while (condition);
  1. Functions
  • void function(): Use when no parameters are needed and no value needs to be returned.
  • void function(int): Use when parameters are needed, but no value needs to be returned.
  • int function(int): Use when parameters are needed and a value needs to be returned.

Function declaration and definiton

// Function declaration
int add(int a, int b);

// Function definition
int add(int a, int b) {
    return a + b;
}

Function Call

int result = add(5, 3);
  1. Arrays
int arr[5]; // Declaration of an array of 5 integers
arr[0] = 10; // Assigning value to the first element
  1. Pointers
int a = 10;
int *p = &a; // p is a pointer to int, holding the address of a
int value = *p; // Dereferencing pointer to get the value of a
  1. Structure declaration & definition
struct Point {
    int x;
    int y;
};

struct Point p1; // Declaration of a structure variable
p1.x = 10; // Accessing members of the structure
p1.y = 20;
  1. Standard Input & Output
#include <stdio.h>

int main() {
    int a;
    printf("Enter a number: ");
    scanf("%d", &a); // Reading input from the user
    printf("You entered: %d\n", a); // Printing output to the console
    return 0;
}

Releases

No releases published

Packages

No packages published

Languages