home

C language quick start

Table of Contents

Hello world program in C.

#include <stdio.h>

int main() {
    printf("hello world");
}

Data types and Variables

#include <stdio.h>

int main() {
    int var_name = 69;
    char a = 'a';
    // string reprsentation in C
    // char *name = "siddhant";
    // char name[] = "siddhant";
    char name[] = {'s', 'i', 'd', 'd', 'h', 'a', 'n', 't', '\0'};

    printf("%s\n", name);

    printf("%d\n", sizeof(var_name));
    printf("%d\n", sizeof(name));
    printf("%d\n", sizeof(a));
}

I/O (Input/Output) in C

#include <stdio.h>

int main() {
    int num;
    scanf("%d", &num);
    printf("%d", num);
}

TODO Operators

TODO Arithmetic

TODO Logical

TODO Relational

TODO Address of

TODO Increment/Decrement

Control Statements/Structures

if/else/else if

Odd/Even program

#include <stdio.h>

int main() {
    int n = 68;
    // modulus operator returns remainder of the division operation
    if (n % 2 == 0) {
        printf("even\n");
    } else {
        printf("odd\n");
    }
}

for loop

while

do while

switch

TODO Pointers

TODO Array

TODO Memory Management

Structs

#include<stdlib.h>
#include<stdio.h>

struct point {
    int x,y;
    char c;
    double f;
};

int main() {
    struct point p = {1,2, 'a', 69.99};
    printf("%d %d %d %.2f\n", p.x, p.y, p.c, p.f);

    struct point *x = malloc(sizeof(struct point));
    x->x = 90;
    x->y = 10;
    x->c = 'c';
    x->f = 289.2;

    printf("size: %d\n", sizeof(struct point));
    printf("pointer x: %d\n", &x->x);
    printf("pointer y: %d\n", &x->y);
    printf("pointer c: %d\n", &x->c);
    printf("pointer f: %d\n", &x->f);

    printf("value after x: %d\n", *(&x->x + 1));
    printf("value at    y: %d", x->y);
}

TODO Union

Author: Siddhant Kumar