Basic Syntax
More Basic
#include<stdio.h>
int main(void) {
printf("Hello World");
return 0;
}
#include<stdio.h>
mean is reading library(In C lang it is called header file).int main(void)
is entry point function of programs.printf()
is display to standerd output.
Variable declaration
short sh;
int in = 1;
long lo = in;
float fl = 0.5;
double dou = 0.55;
char = 'a';
bool bo = true;
char array[5] = {'h', 'o', 'g', 'e', '\0'};
char str[] = "piyo";
unsigned int uin = 100;
unsigned float ufl = 0.2;
'\0' mean is line end.
This is part of the variable declaration.
Enum
#include<stdio.h>
// default start is 0
enum Week { Sun, Mon, Tue};
int main(void) {
enum Week week;
week = Sun;
// Can not used other than defined enum value
// week = Fri; // Error
printf("%d", week);
return 0;
}
"for" and "while" loop
#include<stdio.h>
for(int i = 0; i < 5; i++) {
printf("for loop : %d\n", i);
}
int j = 0;
while(j == 3) {
j++;
if(j == 1) continue;
printf("while loop : %d\n", j);
}
int k = 0;
do {
k++;
printf("%d\n", k);
}while(k == 5);
"switch case" and "goto"
#include<stdio.h>
int j = 2
bar:
swtich(j) {
case 1: break;
case 2: break;
case 3: goto foo;
case 4: goto bar;
default: break;
}
foo:
Be careful not to become spaghetti code when using 'goto'.
"if" and "if else"
#include<stdio.h>
int i = 0;
if(i == 0) {
printf("i == 1");
} else if(i == 2) {
printf("i == 2");
} else {
printf("Other");
}
Function
#include<stdio.h>
void disphello(void) {
printf("Hello);
}
int main(void){
disphello();
return 0;
}
Frequently used function declaration method
#include<stdio.h>
void disphello(void);
int main(void) {
disphello();
return 0;
}
void disphello(void) {
printf("Hello");
}