< printf() 예제 >
(1)
#include <stdio.h>
int main(){
printf("%c\n",'A');
printf("%sn","ASDF");
printf("%c는 영어로 %s입니다.\n",'1',"one");
return 0;
}
실행 결과
A
ASDF
1은 영어로 one입니다.
(2)
#include <stdio.h>
int main(void){
printf("%d\n",10); //%d 위치에 10 출력
printf("%lf\n",3.4); //%lf 위치에 3.4를 소수점 이하 6자리까지 출력
printf("%.1lf\n",3.45); //소수점 이하 첫째 자리까지 출력
printf("%.10lf\n",3.4); //소수점 이하 10자리까지 출력
printf("%d + %d = %d\n",10,20,10+20);
printf("%.1lf - %.1lf = %.1lf\n",3.4,1.2,3.4-1.2);
return 0;
}
실행결과
(3) + 연산도 됩니다 ㄷㄷ;
#include <stdio.h>
int br(){
printf("Brazil,Russia");
}
int ic(){
printf("India,China");
}
int main(void){
br() + printf(",") + ic() + printf("\n");
ic() + printf("\n");
br();
ic();
}
실행 결과
< scanf() 예제 >
(1)
#include <stdio.h>
#include <unistd.h>
//리눅스에서는 sleep함수를 쓰려면 unistd.h 라이브러리를 써야함.윈도우는 windows.h임
int main(void){
char name[50];
char add[50];
printf("What's your name?\n");
scanf("%s",name);
printf("what's your address?\n");
scanf("%s",add);
printf("your name & address is...");
sleep(3); //3초를 멈춰주는 함수
printf("\n\n\nyour name is %s.\nAnd your address is %s!\n",name,add);
return 0;
}
실행 결과
(2)
#include <stdio.h>
int main(void){
int a=0;
int b=0;
char c;
printf("Please enter two numbers\n");
printf("+ : ");
scanf("%d%c%d",&a,&c,&b);
printf(" = %d\n",a+b);
printf("- : ");
scanf("%d%c%d",&a,&c,&b);
printf(" = %d\n",a-b);
printf("* : ");
scanf("%d%c%d",&a,&c,&b);
printf(" = %d\n",a*b);
printf("/ : ");
scanf("%d%c%d",&a,&c,&b);
printf(" = %f\n",(float)a/(float)b);
return 0;
}
'프로그래밍언어 > C' 카테고리의 다른 글
[c] 관계 연산자 (>, <, >=, <=, ==, !=) (0) | 2021.08.25 |
---|---|
[c] 논리 연산자 (&&, ||, !) (0) | 2021.08.25 |
[c] 변수 초기화의 중요성 (0) | 2021.08.25 |
[c] printf()로 ASCII Table 만들기 (0) | 2021.08.25 |
[c] 구구단 출력 예제 3가지! (0) | 2021.08.25 |