프로그래밍언어/C

[C] printf()로 문자열 출력, scanf()로 문자열 입력하기

dan2el 2021. 8. 25. 17:18
< 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;
}