C Lang simple stuff

From PedrosBrainDump

All C keywords

auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

C Lang basics

Primitive types

  • char: Typically 1 byte in size. It's used to store characters or small integers.
  • int: The most common integer type, typically at least 2 bytes in size.
  • short: Short integer, usually smaller than int.
  • long: Long integer, typically larger than int.
  • long long: Very long integer, introduced in C99, typically larger than long.
  • float: Single-precision floating-point type.
  • double: Double-precision floating-point type.
  • long double: Extended-precision floating-point type, larger than double.

Arithmetic operators

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the right operand from the left operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the left operand by the right operand. If both operands are integers, the result is an integer (truncated towards zero).
  • Modulus (%): Computes the remainder of dividing the left operand by the right operand.
  • Increment (++): Increases the value of the operand by 1.
  • Decrement (--): Decreases the value of the operand by 1.

Operator Precedence: Operators like *, /, % have higher precedence over + and -. Use parentheses to enforce desired evaluation order if needed.

Increment and Decrement: ++ and -- can be used as prefix (++a, --b) or postfix (a++, b--). Their behavior can differ in expressions due to their prefix/postfix position.

Division and Modulus: Division / truncates towards zero, meaning it discards any fractional part in integer division. Modulus % computes the remainder after division.

Logical operators

  • Logical AND &&: Returns true if both operands are true, otherwise returns false.
  • Logical OR ||: Returns true if at least one of the operands is true, otherwise returns false.
  • Logical NOT !: Reverses the logical state of its operand. If the operand is true, ! makes it false, and vice versa.

Short-circuit Evaluation: For && and || operators, the right-hand side operand is evaluated only if necessary. This means that if the outcome of the expression can be determined by evaluating only the left-hand side operand, the right-hand side operand is not evaluated. This behavior is useful for optimizing code and preventing unnecessary computations.

Operator Precedence: ! has higher precedence than && and ||. Use parentheses to control the order of operations if needed.

Boolean Context: In C, any non-zero value is considered true, and zero is considered false. However, it's good practice to use explicit boolean values (true or false) where clarity is important.

Relational operators

  • Equal to ==: Checks if the values of two operands are equal.
  • Not equal to !=: Checks if the values of two operands are not equal.
  • Greater than >: Checks if the left operand is greater than the right operand.
  • Less than <: Checks if the left operand is less than the right operand.
  • Greater than or equal to >=: Checks if the left operand is greater than or equal to the right operand.
  • Less than or equal to <=: Checks if the left operand is less than or equal to the right operand.

Relational operators are primarily used in conditional statements (if, else if, while, for, etc.) to make decisions based on comparisons between variables or constants.

Each relational operator evaluates to either true (1) or false (0) based on the outcome of the comparison.

The operands can be variables, constants, or expressions that evaluate to compatible data types (e.g., integers, characters).

if else if else

if (condition) {
    //code block
} else if (condition2) {
    //code block 
} else {
    //code block 
}

switch case

switch (expression) {
    case constant1:
        //code block 
        break;
    case constant2:
        //code block 
        break;
    default:
        //code block 
}

for

for (int i = 0; i < 10; i++) { 
    //code block 
}

while

int i = 0;
while (i < 10) {
    //code block 
    i++;
}

do while

int i = 0;
do {
    //code block
    i++;
} while (i < 10);

function

return_type function_name(parameter_list) {
    // Function body (statements)
    // Local variable declarations

    // Statements
    return expression; // Optional return statement
}

Get Input And Printing Out On Terminal

At the beginning of the file include stdio.h

#include <stdio.h>

read

scanf("type", destinationVariable);

print

printf("Text here.");

types

  • %d: Reads an integer value into an int.
  • %ld: Reads a long integer value into a long.
  • %lld: Reads a long long integer value into a long long.
  • %f: Reads a floating-point value into a float.
  • %lf: Reads a double-precision floating-point value into a double.
  • %c: Reads a single character into a char.
  • %s: Reads a string of characters into a character array (char[]).
  • %u: Reads an unsigned integer into an unsigned int.
  • %lu: Reads an unsigned long integer into an unsigned long.
  • %llu: Reads an unsigned long long integer into an unsigned long long.

(e.g.)

#include <stdio.h>
int main(int argc, char **argv) {
    int n = 0;
    printf("Please a number:\n");
    scanf("%d", &n);
    printf("You entered number: %d\n", n);
}