6626070
2997924

PL01, Syntax

Back to the previous pagepage management
List of posts to read before reading this article


Contents


C language basic syntax

Compile

$ apt update
$ apt install gcc

Hello, World!, Basic

#include <stdio.h>

int main(void){
    printf("Hello, World!\n");
    system("pause");
    return 0;
}
Hello, World!





Variable, constant

static variables

the declared static variable have automatively initial values, 0.
#include <stdio.h>

int a;   // auto-initialization!

int main(void){
        printf("%d\n", a);
        system("pause");
        return 0;
}
0




non-static variables

#include <stdio.h>

int main(void){
        int a;   // no initialization!
        printf("%d\n", a);
        system("pause");
        return 0;
}
error!


#include <stdio.h>

int main(void){
        int a = 0;   // initialization!
        printf("%d\n", a);
        system("pause");
        return 0;
}
0




expression method for negative integer

  • sign absolute value(sign bit)
    • ex> +2(10) = 0 010(2), -2(10) = 1 010(2)
  • 2’s complement = 1’s complement + 1
    • ex> +2(10) = 0010(2), -2(10) = 1101(2) + 0001(2) = 1110(2)
      • 0010(2) + 1110(2) = 0000(2) for 4-bit computation system




expression method for real number
\((-1)^{s}*M*2^{E}\)





Input, output

Type specifier

type specifier INPUT OUTPUT
int(4 Bytes) %d %d
long long(8 Bytes) %lld %lld
double(8 Bytes) %lf %f
float(4 Bytes) %f %f
string(non-limit) %s %s
char(1 Bytes) %c %c
in the case of double type, the reaseon for different I/O is it require memory allocation of address during input, wherever it is not require memory allocation during output




I/O example 1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	double a;
	scanf("%lf", &a);
	printf("%.2f\n", a);
	system("pause");
        return 0;
}


I/O example 2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int a, b;
        scanf("%d %d", &a, &b);
        printf("%d %d\n", b, a);
        system("pause");
        return 0;
}


I/O example 3

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int a, b, c;
        scanf("%1d%1d%1d", &a, &b, &c);
        printf("%d %d %d\n", a, b, c);
        system("pause");
        return 0;
}





Operator

Operator reference URL


Arithmetic operator

the four fundamental arithmetic operations

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int a, b;
        scanf("%d %d", &a, &b);
        printf("%d + %d = %d\n", a, b, a+b);
        printf("%d - %d = %d\n", a, b, a-b);
        printf("%d * %d = %d\n", a, b, a*b);
        printf("%d / %d = %d\n", a, b, a/b);
        printf("%d %% %d = %d\n", a, b, a%b);
        system("pause");
        return 0;
}




Increment/Decrement operations

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int a = 7;
        printf("a = 7\n");
        printf("++a : %d\n", ++a);
        printf("a++ : %d\n", a++);
        printf("++a : %d\n", ++a);
        printf("--a : %d\n", --a);
        printf("a-- : %d\n", a--);
        printf("--a : %d\n", --a);
        system("pause");
        return 0;
}
a = 7
++a : 8
a++ : 8
++a : 10
--a : 9
a-- : 9
--a : 7




Escape sequence

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        printf("A  B  C  D\n");
        printf("\"A  B  C  D\"\n");
        printf("\"A\tB\tC\tD\"\n");
        system("pause");
        return 0;
}




Comparison operators

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int a, b;
        scanf("%d %d", &a, &b);
        printf("%d\n", a > b);
        system("pause");
        return 0;
}
0 = False(default), 1 = all of other things




Logical operator

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        printf("%d\n", !a);
        printf("%d\n", a && b);
        printf("%d\n", (a > b) && (b > c));
        printf("%d\n", a > b);
        system("pause");
        return 0;
}




Ternary conditional

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int a = 7, b = 7;
        printf("%d\n", (a==b) ? 100 : -100);
        system("pause");
        return 0;
}
100





Control structures

Selection statements

If, example 1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        printf("how many customers are there?");
        int a;
        scanf("%d", &a);
	
        if ( a == 1 || a == 2 ){
                printf("i will show you around for two\n");
        }
        else if ( a == 3 || a == 4 ){
                printf("i will show you around for four\n");
        }
        else{
                printf("i will show you the extra-large seat\n");
        }
        system("pause");
        return 0;
}


If, example 2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	printf("please, enter the main memory size(GB) of your computer\n");
	int size;
	scanf("%d", &size);
	if (size >= 16) {
		printf("your memory size is enough\n");
	}
	else {
		printf("expand your memory size\n");
	}
	system("pause");
	return 0;
}




Switch, example 1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	printf("please, enter your grade\n");
	char a;
	scanf("%c", &a);
	switch (a) {
	case 'A':
		printf("it's A\n"); break;
	case 'B':
		printf("it's B\n"); break;
	case 'C':
		printf("it's C\n"); break;
	default:
		printf("check your grade being right\n");
	}
	system("pause");
	return 0;
}


Switch, example 2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	printf("please, enter month if you want to know which season\n");
	int a;
	scanf("%d", &a);
	switch (a) {
	case 1: case 2: case 3:
		printf("it's spring\n"); break;
	case 4: case 5: case 6:
		printf("it's summer\n"); break;
	case 7: case 8: case 9:
		printf("it's fall\n"); break;
	case 10: case 11: case 12:
		printf("it's winter\n"); break;
	}
	system("pause");
	return 0;
}




Iteration statements

for, example 1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        int i;
        for (i=0; i<=100; i++){
                printf("%d\n", i);
        }
        system("pause");
        return 0;
}
Initialization on for statement
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
        for (int i=0; i<=100; i++){
                printf("%d\n", i);
        }
        system("pause");
        return 0;
}




for, example 2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	int n, sum = 0;
	printf("n을 입력하세요. ");
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		sum = sum + i;
	}
	printf("%d\n", sum);
	system("pause");
}




for, Infinite loop 1

#include <stdio.h>
int main(void) {
	for (;;) {
		// 조건문의 내용이 없으면 항상 참(True)
		printf("Hello World!\n");
	}
	system("pause");
}




for, Infinite loop 2

#include <stdio.h>

int main(void) {
	for (int i = 0; i <= 100; i--) {
		printf("Hello World!\n");
	}
	system("pause");
}




for, example 3

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	int sum = 0;
	for (; 1;) {
		int x;
		scanf("%d", &x);
		if (x == -1) break;
		sum += x;
	}
	printf("%d\n", sum);
	system("pause");
}




for, example 4

#include <stdio.h>

int main(void) {
	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= 9; j++) {
			printf("%d * %d = %d\n", i, j, i * j);
		}
		printf("\n");
	}
	system("pause");
}




while, example 1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	int n;
	char a;
	scanf("%d %c", &n, &a);
	while (n--) {
		printf("%c ", a);
	}
	system("pause");
	return 0;
}




while, example 2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) {
	int n;
	scanf("%d", &n);
	int i = 1;
	while (i <= 9) {
		printf("%d * %d = %d\n", n, i, n * i);
		i++;
	}
	system("pause");
}




while, example 3

#include <stdio.h>

int main(void) {
	int i = 1;
	while (i <= 9) {
		int j = 1;
		while (j <= 9) {
			printf("%d * %d = %d\n", i, j, i * j);
			j++;
		}
		printf("\n");
		i++;
	}
	system("pause");
}





Functions

Attach subject

#include <stdio.h>

void dice(int input) {
	printf("Dice thrown by user: %d\n", input);
}

int main(void) {
	dice(3);
	dice(5);
	dice(1);
	system("pause");
}




Add operation

#include <stdio.h>

int add(int a, int b) {
	return a + b;
}
int main(void) {
	printf("%d\n", add(10, 20));
	system("pause");
}




Arithmetic operation

#include <stdio.h>

void calculator(int a, int b) {
	printf("%d + %d = %d\n", a, b, a + b);
	printf("%d - %d = %d \n", a, b, a - b);
	printf("%d * %d = %d \n", a, b, a * b);
	printf("%d / %d = %d \n", a, b, a / b);
	printf("\n");
}

int main(void) {
	calculator(10, 3);
	calculator(15, 2);
	calculator(18, 4);
	system("pause");
}




Recursive function : factorial

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int factorial(int a) {
	if (a == 1) return 1;
	else return a * factorial(a - 1);
}

int main(void) {
	int n;
	printf("n 팩토리얼을 계산합니다. ");
	scanf("%d", &n);
	printf("%d\n", factorial(n));
	system("pause");
}





Basic Data structures

Arrays

Declare array and initialize({0,})

#include <stdio.h>

int main(void){
    int a[10] = {0,};
    int i;
    for (i=0; i<10; i++){
        printf("%d ", a[i]);
    }
    system("pause");
    return 0;
}




Maximum of elements in array

#include <stdio.h>
#include <limits.h>

int main(){
    int a[10] = {5,3,6,4,1,8,9,0,7,2};
    int i, maxValue = INT_MIN;
    for (i=0; i<10; i++){
        if (maxValue < a[i]) maxValue = a[i];
    }
    printf("%d\n", maxValue);
    system("pause");
    return 0;
}




String and array(1)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
    char a[20];
    scanf("%s", &a);
    printf("%s\n", a);
    return 0;
}




String and array(2)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
    char a[20] = "Hello World";
    a[4] = ',';
    printf("%s\n", a);
    system("pause");
    return 0;
}




Number of chracters ‘l’ in string

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void){
    char a[] = "Hello World";
    int count = 0;
    for (int i=0; i<=10; i++){
        if (a[i] == 'l') count++;
    }
    printf("%d\n", count);
    system("pause");
    return 0;
}




Pointers

  • Address operator(&) : It find the value of the variable’s memory start address by attaching it in front of the variable
  • Pointer(*) : It declare pointer variable
  • Dereference operator(*) : It find the value for pointer variable to points to

Single pointer
image

#include <stdio.h>

int main(void){
    int a = 5;
    int *b = &a;         // pointer
    printf("%d\n", *b);  // dereference operator
    system("pause");
    return 0;
}




Double pointer
image

#include <stdio.h>

int main(void){
    int a = 5;
    int *b = &a;
    int **c = &b;
    printf("%d\n", **c);
    system("pause");
    return 0; 
}




Powerful function of pointer
Intercompatible pointer and array

#include <stdio.h>

int main(){
        int a[] = {1,2,3,4,5,6,7,8,9,10};
        int *b = a;
        printf("%d\n", b[2]);
        system("pause");
        return 0;
}




Characters

ascii codes
image




Structures and unions





Miscellaneous

Memory management




Input/Output




Preprocessor





Advanced Data Structure





C Project

Compile

on vscode

URL

image Press ctrl + shift + x and install c/c++ extension.

image Press ctrl + shift + p and edit c/c++ configurations(UI).

image Terminal > Configure Default Build Task > Create tasks.json file from template > Others
CODE

{
    "version": "2.0.0",
    "runner": "terminal",
    "type": "shell",
    "echoCommand": true,
    "presentation" : { "reveal": "always" },
    "tasks": [
          //C++ compile
          {
            "label": "save and compile for C++",
            "command": "g++",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",

            "problemMatcher": {
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
	
        //C compile
        {
            "label": "save and compile for C",
            "command": "gcc",
            "args": [
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",

            "problemMatcher": {
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        // execute binary(Windows)
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
                "/C", "${fileDirname}\\${fileBasenameNoExtension}"
            ]
    
        }
    ]
}

image CODE

[   
    //c compile
    {
        "key": "ctrl+alt+c", 
        "command": "workbench.action.tasks.build" 
    },
    
    //c run
    {
        "key": "ctrl+alt+r", 
        "command": "workbench.action.tasks.test"
    }
]

image compile(ctrl+alt+c) and execute(ctrl+alt+r)




gcc

gcc installation for windows

image Download mingw-get-setup.exe file at mingw.

image Check button of mingw32-base, mingw32-gcc-g++ on MinGW Installation Manager and apply that.

image And then, set up your system environment variable path = C:\MinGW\bin.


gcc [c_file_name] -o [executable_file_name]




make(Makefile)




cmake





List of posts followed by this article


Reference


OUTPUT