C for Begineer
C for Begineer
begineer
1. Hello World
c , main , . c
. , <stdio.h> ,
<math.h>, <window.h> .
#
printf(“ ”);
“” .
#include <stdio.h>
int main(void)
{
printf(“Hello, World!”);
return0;
}
?
2-1
C , , 3 .
6 .
1. int 4 , .
2. short int 2 , int ,
2 .
3. unsigned int 4 int ,
2 .
4. unsigned short int - unsigned int 2 unsigned
int 2 .
5. long 8 int 2 64
4 .
6. unsigned long int 4 unsigned int
unsigned int 2 .
c .
#include <stdio.h>
int main(void)
{
int a = 4;
short int b = 2;
unsigned int c = 4;
long d = 8;
unsigned short int e = 4;
}
#int
float .
1. float - ,4 .
2. double 4 float 2 ,8 .
3. long double - double 10 .
c .
#include <stdio.h>
int main(void)
{
float a = 4;
double b = 8;
long double c = 10;
}
#float
char .
1. char - 1 .
2. signed char 1 char .
3. unsigned char 1 unsigned int
, char 2 .
c .
#include <stdio.h>
int main(void)
{
char a = 1;
signed char b = 1;
unsigned char c = 1;
}
#char
END
#
2-2
2-1 .
2-1 sizeof(A)
.
.
#include <stdio.h>
int main() {
int integer;
short int shortInteger;
unsigned int unsignedInteger;
long longInteger;
unsigned long unsignedLongInteger;
unsigned short int unsignedShortInteger;
float floatingPoint;
double doublePrecision;
long double longDoublePrecision;
char character;
signed char signedCharacter;
unsigned char unsignedCharacter;
return 0;
}
.
#sizeof #
3-1. , ?
1. 'scanf'
C scanf() . scanf printf
. scanf .
scanf , , .
2-1.
%d, %f #
.
%d 10 .
%o 8 .
%x 16 .
%f - .
%c - .
.
2-2. '&'
scanf '&' .
'&' , scanf ,
printf .
scanf printf .
#include <stdio.h>
int main(void)]
{
int a;
scanf("%d", &a);
printf("%d", a);
return 0;
}
printf '&' .
//a 3
<cd~/coding> 3
3-2.
printf scanf # .
, , .
, , 3-1. , ?
.
%u - unsigned int .
%lu - unsigned long .
%lld - long long .
%ld - long .
%c - .
%su - .
%s8 UTF 8 .
%ma ASCII 64 .
.
.
3-3.
C %c .
?
.
1 1 . scanf
.
1. 10 >> 48~57
2. 26 >> 65~90
3. 26 >> 97~122
4. 33 >> 32 ~
5. 33 >> 0, 9, 10, 13
\0 , \n , \t .
4-1. if
C .
#if #while 2 .
if () , {} 2 .
() .
() , , , , {} C
. if if .
if .
#include <stdio.h>
int main(void)
{
int a, b;
a = 1, b = 1;
if(a == b)
{
printf("a equals to b");
}
return 0;
}
'=' '==' .
a = b a b .
a b '=' '==' .
4-2. if AND, OR
C ,
.
=> AND, OR .
AND && , OR || .
AND
3 5 Correct!
a ,b a b
a 'True' b 5 5
OR
a>3 b<3
y>0 ax<0
1. AND
#include <stdio.h>
int main(void)
{
int a=1, b=5;
#include <stdio.h>
int main(void)
{
int a=5, b;
scanf("%d", &b)
if(a<b || b>0)
{
printf("Hello!");
}
return 0;
}
if
scanf
5-1. while
#if {} . #if
. #if {}
#while {} .
#while .
while( )
{
while .
#include <stdio.h>
int main(void)
{
int i;
while(i<100)
{
printf("The Number is %d", i);
i++;
}
return 0;
}
if
6-1 ,
1. ,
1-1.
++ -- , . a+b , a*b
2 (binary) , 1
(unary) .
1-2.
, 1 1 .
Ex) >> ++(a+b) , 3++ , (x-y)-- .
2.
2-1
(++) (--) ,
.
2-2
. .
#include <stdio.h>
int main(void)
{
int a, b;
a = 10;
b = 20;
return 0;
}
< >
a++ 10
++a 12
b-- 20
--b 18
2 . ,
.
( , ...etc)
,
.
a++ a 1 , a (print)
a 1 a 11 , a++ 10 .
++a a 1 , a (11+1)
a 12 , ++a 12 .
b .
for
for #for , for
.
, .
for , .
#include <stdio.h>
int main(void)
{
for(int i=0; i<5; i++)
{
printf("%d\n", i);
}
return 0;
}
1 .
for
6-2. for
#while () ( ) {}
. () 3 (2 ) , .
for ( i )
.
or ?( )
,
.(ex> i++)
; .
#for .
#inlcude <stdio.h>
int main(void)
{
for(int i=0; i<100; i++)
{
printf("%d\n", i);
}
return 0;
}
while
if
&
7. switch, case
#if ?
.
int a=5;
if(a==5)
{
printf("5");
}
else if(a<5)
{
printf("a<5");
}
else if(a>5)
{
printf("a>5");
}
. .C
#switch . switch
. #break , case
. break .
switch .
#include <stdio.h>
int main(void)
{
char s;
scanf("%c", &s);
switch(s)
case 'W' : printf("WINTER"); break;
case 'S' : printf("SPRING"); break;
case 'SU' : printf("SUMMER"); break;
case 'F' : printf("FALL"); break;
return 0;
}
if
8-1.
?
1-1.
.
.
C .
[ ] ([ ]) {
//
//
//
}
1-2. ?
. 'Hello world!' printf() ,
scanf("%d", &a) . if , while
. .
1-3. ?
C ? 5%
.
C <stdio.h>
C <stdio.h> ,
. , C
.
Hello World
if
while
for
8-2.
?
C ' ' .
printf() , scanf() . <stdio.h>
, #include <stdio.h>
.h , .
include ,
#include import .
C .
Hello World
8-4.
<math.h>
.
1.
int abs(int a) .
abs .
#include <stdio.h>
#include <math.h>
int main(void)
{
int a;
printf(“Enter a number: ”);
scanf(“%d”, &a);
printf(“\n %d\’s absolute value is: %d”, a, abs(a));
return 0;
}
2.
pow .
pow x^y .
#include <stdio.h>
#include <math.h>
int main(void)
{
int x, y;
int x = 10;
int y = 3;
return 0;
}
< >
pow(10, 3) = 1000
3.
x sqrt
.
sqrt x .
#include <stdio.h>
#include <math.h>
int main(void)
{
printf(“sqrt(4) = %d\n”, sqrt(4));
printf(“sqrt(2) = %.3f\n”, sqrt(2));
return 0;
}
< >
sqrt(4) = 2
sqrt(2) = 1.414
<math.h>
8.5 rand()
C ?
1-1. math.h
C , .
C (log, sin, √, |x| ...etc) math.h .
1-2. math.h
math.h , ,
. .
1-3.
(prototype) , , .
( , int a=10;)
. pow .
x: (base)
y: (exponent)
x y .
pow x y .
, x^y .
pow .
#include <stdio.h>
#include <math.h>
int main(void)
{
int result = pow(2, 3);
printf("2^3 = %d\n", result);
return 0;
}
2^3 = 8
1-1.
(bit) .
(bit operator) .
, .
< >
~ ~a a 1
& a&b a ,b
| a|b a ,b
3 .
.
(AND) , .
.
(OR) if AND, OR ,
.
0 & 0 -> : 0
0 & 1 -> : 0
1 & 0 -> : 0
1 & 1 -> : 1
(OR) , .
.
if ,
.
.
1 | 0 -> : 1
0 | 1 -> : 1
1 | 1 -> : 1
0 | 0 -> : 0
< >
^ a^b a b
<< a<<b a b
>> a>>b a b
3 .
.
(XOR) , ( ) (1)
.
.
0 ^ 0 -> : 0
0 ^ 1 -> : 1
1 ^ 0 -> : 1
1 ^ 1 -> : 0
( >> , << ) 2
(unsigned int) .
<<( ) 0 .
>>( ) 0 .
< >
< >
“Pasted image 20230806164917.png” could not be found.
, 2 ,
2 .
< >
#include <stdio.h>
int main() {
int num = 16; // 00010000 (2 )
return 0;
}
Result>
: 16 (2 : 10000)
: 8 (2 : 1000)
< >
#include <stdio.h>
int main() {
int num = 4; // 00000100 (2 )
return 0;
}
Result>
: 4 (2 : 100)
: 16 (2 : 10000)
1.
C . 5 ,
. .
+ a+b a b
- a-b a b
- -a a
* a*b a b
/ a/b a b
% a%b a b
#include <stdio.h>
int main(void)
{
double x, y;
x = 2.5;
y = 5.0;
printf("\n x+y = %.3f", x+y);
printf("\n x-y = %.3f", x-y);
printf("\n x*y = %.3f", x*y);
printf("\n x/y = %.3f", x/y);
printf("\n x%%y = %.3f", x%y);
return 0;
}
x+y 7.5
x-y 2.5
x*y 12.5
x/y 0.5
x%y -
% /
% / .
</ >
12/4 3
16/3 5
5/2 2.5
<% >
12%4 3
16%3 1
5%2 -
% 3 - .
if
1-1.
(conditional operator) 3 3 . ? :
1 : 2
. if .
1-2.
condition1 condition2 ,
conditon3 .
X = (a>b) ? a : b;
a b a X ,b a b X
.
.
#include <stdio.h>
int main() {
int num1, num2, max;
//
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// max
max = (num1 > num2) ? num1 : num2;
//
printf(" : %d\n", max);
return 0;
}
&
if
1.
1-1.
(relational operator) , .
< a<b a b 1,
0
<= a<=b a b 1,
0
> a>b a 1,
0
>= a>=b a b 1,
0
== a==b a b 1,
0
! a!=b a b 1,
0
1-2.
#if ,
.
.
#include <stdio.h>
int main(void)
{
int a, b;
a = 5;
b = 10;
printf("%d\n", a>b);
printf("%d\n", a>=b);
printf("%d\n", a<b);
printf("%d\n", a<=b);
printf("%d\n", a==b);
printf("%d\n", a!=b);
return 0;
}
If
if {} ,
.
if .
#include <stdio.h>
int main(void)
{
int a;
if(a>0)
{
printf("a is a postive number!");
}
return 0;
}
if
6-1 ,
for
#for .
for 2 .
for 1 .
for 1
.
for printf("\n"); .
`for` .
#include <stdio.h>
int main(void)
{
int i, j;
for
3
I. 3 ?
C . int
int ( )[]...[]; . [] .
ex1 3
#include <stdio.h>
int main(void)
{
int matrix = {
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
{
{10, 11, 12},
{13, 14, 15},
{16, 17, 18},
},
{
{19, 20, 21},
{22, 23, 24},
{25, 26, 27},
},
};
}
3 .
#include <stdio.h>
int main(void)
{
int i, j, k;
int matrix = {
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
{
{10, 11, 12},
{13, 14, 15},
{16, 17, 18},
},
{
{19, 20, 21},
{22, 23, 24},
{25, 26, 27},
},
};
for 3 3 . for
, for
, for 3 .
if
#if .
,2 #int 2 ,
#char .
#if
.
.
#if 2 .
#include <stdio.h>
int main(void)
{
int a, b;
char operand;
printf(" : ");
scanf("%d, %d\n", &a, &b);
printf(" : ");
scanf("%c", &operand);
if(operand == '+')
{
printf("%d + %d = %d", a, b, a+b);
}
else if(operand == '-')
{
printf("%d - %d = %d",a, b, a-b);
}
else if(operand == '*')
{
printf("%d * %d = %d", a, b, a*b);
}
else if(operand == '/')
{
printf("%d / %d = %d", a, b, a/b);
}
else
{
printf("Invaild operand! choose the right operand");
}
return 0;
}
a 4, b 2, operand = '+' .
4 + 2 = 6
done
?
if