0% found this document useful (0 votes)
13 views48 pages

C for Begineer

The document provides a comprehensive introduction to the C programming language, covering basic syntax, data types, operators, control structures, and functions. It includes examples of code snippets for various concepts such as printing output, using data types, and implementing control flow with if statements, loops, and switch cases. Additionally, it discusses the use of libraries like <stdio.h> and <math.h> for input/output and mathematical functions.

Uploaded by

b93359671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views48 pages

C for Begineer

The document provides a comprehensive introduction to the C programming language, covering basic syntax, data types, operators, control structures, and functions. It includes examples of code snippets for various concepts such as printing output, using data types, and implementing control flow with if statements, loops, and switch cases. Additionally, it discusses the use of libraries like <stdio.h> and <math.h> for input/output and mathematical functions.

Uploaded by

b93359671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

C for

begineer
1. Hello World
c , main , . c
. , <stdio.h> ,
<math.h>, <window.h> .
#

Hello, World <stdio.h> printf .


.

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;

printf("Size of int: %zu bytes\n", sizeof(integer));


printf("Size of short int: %zu bytes\n",
sizeof(shortInteger));
printf("Size of unsigned int: %zu bytes\n",
sizeof(unsignedInteger));
printf("Size of long: %zu bytes\n", sizeof(longInteger));
printf("Size of unsigned long int: %zu bytes\n",
sizeof(unsignedLongInteger));
printf("Size of unsigned short int: %zu bytes\n",
sizeof(unsignedShortInteger));
printf("Size of float: %zu bytes\n",
sizeof(floatingPoint));
printf("Size of double: %zu bytes\n",
sizeof(doublePrecision));
printf("Size of long double: %zu bytes\n",
sizeof(longDoublePrecision));
printf("Size of char: %zu byte\n", sizeof(character));
printf("Size of signed char: %zu byte\n",
sizeof(signedCharacter));
printf("Size of unsigned char: %zu byte\n",
sizeof(unsignedCharacter));

return 0;
}
.

Size of int: 4 bytes


Size of short int: 2 bytes
Size of unsigned int: 4 bytes
Size of long: 8 bytes
Size of unsigned long int: 8 bytes
Size of unsigned short int: 2 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of long double: 16 bytes
Size of char: 1 byte
Size of signed char: 1 byte
Size of unsigned char: 1 byte

#sizeof #
3-1. , ?

1. 'scanf'
C scanf() . scanf printf
. scanf .

scanf(" ", & ( ));

scanf , , .

: scanf("%d", &a); , a int .


: scanf("%f", &b); , b float .
: scanf("%c", &c); , c char .

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 .

 %Ld - long double .


 %lf - double .
 %0.2f , %0.5f -
.

 %c - .
 %su - .
 %s8 UTF 8 .
 %ma ASCII 64 .

.
.
3-3.
C %c .
?
.

1 1 . scanf
.

“Pasted image 20230720105732.png” could not be found.

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;

if(a<b && b>0)


{
printf("Done!")
}
return 0;
}
2. OR

#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;

printf("\n a++\'s result is: %d", a++);


printf("\n ++a\'s result is: %d", ++a);
printf("\n b--\'s result is: %d", b--);
printf("\n --b\'s result is: %d", --b);

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 )
.



. (ex> i>3 && i<5)

 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

switch(a) case ' ' : ; break;

. #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 .

stdio.h printf, scanf...


ctype.h isalpha, tolower...
math.h (log ) sin, sqrt, log...
stdlib.h rand, srand...
string.h strlen, strcpy...
time.h time, acstime

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;

printf(”pow(10, 3) = %d\n”, pow(10, 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 , ,
. .

int abs(int a) int


double fabs(double b) double
int pow(int x, int y) x y
int exp(int a) e( 2.7182) 'a'
int log(int x) log(x)
int log10(int y) 10 y
int sqrt(int x) x
sin int sin(int a) sin(a)
cos int cos(int b) cos(b)
tan int tan(int c) tan(c)
double ceil(double x)
double floor(double x)

1-3.
(prototype) , , .
( , int a=10;)
. pow .

int pow(int x, int y)


:

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)
.
.

“Pasted image 20230806163726.png” could not be found.

0 ^ 0 -> : 0
0 ^ 1 -> : 1
1 ^ 0 -> : 1
1 ^ 1 -> : 0

 ( >> , << ) 2
(unsigned int) .
<<( ) 0 .
>>( ) 0 .

< >

“Pasted image 20230806164759.png” could not be found.

< >
“Pasted image 20230806164917.png” could not be found.

, 2 ,
2 .

< >

#include <stdio.h>

int main() {
int num = 16; // 00010000 (2 )

printf(" : %d (2 : %d)\n", num, num);

num = num >> 1;

printf(" : %d (2 : %d)\n", num, num);

return 0;
}

Result>
: 16 (2 : 10000)
: 8 (2 : 1000)

< >

#include <stdio.h>

int main() {
int num = 4; // 00000100 (2 )

printf(" : %d (2 : %d)\n", num, num);

num = num << 2;

printf(" : %d (2 : %d)\n", num, num);

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 : condition3

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;

printf("Enter a number: ");


scanf("%d", &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(i=2; i<10; i++)


{
for(i=j; j<10; j++)
{
printf("%d * %d = %d", i, j, i*j);
}
printf("\n");
}
printf("\n");
return 0;
}

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(i=0; i<3; i++)


{
for(j=0; j<3; j++)
{
for(k=0; k<3; k++)
{
printf(“%d ”, matrix[i][j][k]);
}
printf(“\n”);
}
printf(“\n”);
}
return 0;
}

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

You might also like