Docsity

C Programming: Data Types, Operators, Statements, Loops, Functions, Recursion, I/O, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download C Programming: Data Types, Operators, Statements, Loops, Functions, Recursion, I/O and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Lecture 01

C programming

  • C programming 구조
  • 연산자 ( 산술 , 관계 , 논리 , 대입 )

C language

 Data type: numbers, characters, Boolean

 Statements

Assignment

Conditional statements

Loop statements

Function statements

Input / output

2

Conditional statements

Selective branch

 if, switch

if statement  if (condition) then S 1 else S 2

switch statement  switch (variable) { cond1 : S1; break; cond2 : S2; break;

... condn : Sn; break; default : Sn+ }

cond S 1

false

true

S 2

S 1

cond 1 true

false

S 2

cond 2

Sn

false (^)... condn false Sn+ true true

4

Loop statement 1

 Repeat a set of statements in a loop

(often with the exit condition)

 while, for, do-while

 while

format

 while (condition) do
S

Infinite loop

 while (1) do
S

cond S

false

true

5

Loop statement 3

 do-while

format

 do
S
while (condition);

S is executed at least once

 Other loop statements

goto statement : unconditional branch

exit : move the control to the statement just after

loop

S cond

true

false

7

Function

 format

 function-name(parameter_list) {
S

Return to the calling function

 return expression;
 Expression: result of the function

Call function

 function-name(argument-list)
 Match the number of arguments and type in function

Call by value: return the actual value

8

Input / output

Input function : scanf (argument_list); getchar(); read();

gets()

Output function : printf (argument_list); putchar();

write()

10

sizeof example

Prof. DaeEun Kim 11

 assignment.c

13

14

16

 &&(and), ||(or), !(not)

 C 언어는 참과 거짓이라는 상수는 없으며, 단순히 0 을 거짓으로,

17

 변환명세에서 필드 폭(width)을 지정하려면 %f 사이에 폭을 기술

19

#include <stdio.h>

int main() {

printf("12345678901234567890\n"); printf("%10c\n", 'A'); printf("%10d\n", 128); printf("%10lf\n", 3.1415926); printf("%10le\n", 3.1415926); printf("%10.3lf\n", 3.1415926); return 0; }

A

3.141593e+ 3.

printf

20