057. 문자열 복사하기(strcpy)057. 문자열 복사하기(strcpy)

Posted at 2011. 1. 30. 17:47 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>
#include <string.h>

#define KOREA "대한민국"

void main( void )
{
	char *string1;
	char string2[100];

	strcpy( string1, KOREA );		// 실행 시 에러 발생
	strcpy( string2, KOREA );
	strcpy( string2, "봄" );
}
//

056. 문자열 출력하기(puts)056. 문자열 출력하기(puts)

Posted at 2011. 1. 30. 17:45 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

#define KOREA "대한민국"
#define SUMMER "여름"

void main( void )
{
	const char* winter = "겨울";

	puts( KOREA );
	puts( SUMMER );
	puts( winter );
}
//

055. 문자열 입력받기(gets)055. 문자열 입력받기(gets)

Posted at 2011. 1. 30. 17:43 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

int count( char *str );

void main( void )
{
	char string[100];
	char *ret;

	ret = gets( string );

	if( ret != NULL )
	{
		printf( "문자 'a'의 갯수는 %d개입니다.", count(string) );
	}
}

int count( char *str )
{
	int cnt = 0;

	while( *str != (int)NULL )
	{
		if( *str++ == 'a' ) cnt++;
	}

	return cnt;
}
//

054. 정수값 출력하기(printf)054. 정수값 출력하기(printf)

Posted at 2011. 1. 30. 17:37 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

void main( void )
{
	int i = 100;
	int j = 1000;
	int k = 12345;

	// 정렬을 사용하지 않는 경우
	printf( "[%d]\n", i );		// [100]
	printf( "[%d]\n", j );		// [1000]
	printf( "[%d]\n", k );		// [12345]

	// 출력을 5자리 설정
	printf( "[%5d]\n", i );		// [  100]
	printf( "[%5d]\n", j );		// [ 1000]
	printf( "[%5d]\n", k );		// [12345]

	// 출력을 10자리 설정
	printf( "[%10d]\n", i );		// [       100]
	printf( "[%10d]\n", j );		// [      1000]
	printf( "[%10d]\n", k );		// [     12345]

	// 출력을 10자리로 설정하고 왼쪽 설정
	printf( "[%-10d]\n", i );		// [100       ]
	printf( "[%-10d]\n", j );		// [1000      ]
	printf( "[%-10d]\n", k );		// [12345     ]
}
//

053. 정수값 입력받기(scanf)053. 정수값 입력받기(scanf)

Posted at 2010. 11. 22. 04:45 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

void main( void )
{
	int count;		// 3회를 카운트하기 위한 변수
	int tmp;		// 정수값을 읽을 임시 변수
	int total = 0;		// 읽은 정수값을 합산하기 위한 변수

	for( count = 1; count <= 3; count++ )
	{
		printf( "%d 번째 정수값을 입력한 후 Enter키를 누르세요.\n", count );

		scanf( "%d", &tmp );

		total += tmp;

		printf( "입력 값 = %d, 총 합 = %d\n", tmp, total );
	}

	printf( "읽은 정수의 총 합은 %d입니다.\n", total );
}
//

052. 문자 출력하기(putch)052. 문자 출력하기(putch)

Posted at 2010. 11. 22. 04:42 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>
#include <conio.h>

int print( char *string );

void main( void )
{
	print( "This is a putch function!" );
}

int print( char *string )
{
	int len = 0;

	while( *string != (char)NULL )
	{
		putch( *string );
		string++;
		len++;
	}

	// 현재 출력되고 있는 줄을 다음 줄의 첫 번째로 이동
	putch( '\r' );		// 캐리지 리턴
	putch( '\n' );		// 라인 피드

	return len;
}
//

051. 문자 입력받기(getch)051. 문자 입력받기(getch)

Posted at 2010. 11. 22. 04:39 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>
#include <conio.h>

#define ENTER 13

void main( void )
{
	int ch;

	printf( "아스키 코드로 변환할 키를 누르세요...\n" );
	printf( "Enter 키를 누르면 프로그램은 종료됩니다.\n" );

	do
	{

		ch = getch();

		printf( "문자 : (%c) , 아스키 코드 = (%d)\n", ch, ch );

	} while( ch != ENTER );
}
//

050. 매크로 이해하기050. 매크로 이해하기

Posted at 2010. 11. 22. 04:36 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

#define MAX(a,b) a > b? a : b
#define MIN(a,b) a < b? a : b

main()
{
	int i, j;

	i = 5;
	j = 7;

	printf( "최대값은 %d입니다.\n", MAX(i, j) );
	printf( "최소값은 %d입니다.\n", MIN(i, j) );
}
//

049. #include 문 이해하기049. #include 문 이해하기

Posted at 2010. 11. 22. 04:34 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>
#include <conio.h>

main()
{
	int ch;

	printf( "아무키나 누르세요...\n" );

	ch = getch();

	printf( "%c 키가 눌려졌습니다.", ch );
}
//

048. 변수의 범위 이해하기048. 변수의 범위 이해하기

Posted at 2010. 11. 22. 04:32 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

void print_x( int x );
void print_gx( void );

int x = 20;

main()
{
	int x = 5;
	printf( "x = %d\n", x );		// 5가 출력(지역 변수 x가 사용됨)

	print_x( 10 );
	print_gx();
}

void print_x( int x )
{
	printf( "x = %d\n", x );		// 10이 출력(지역 변수 x가 사용됨)
}

void print_gx( void )
{
	printf( "x = %d\n", x);			// 20이 출력(전역 변수 x가 사용됨)
}

'Computer > 초보자를 위한 C 언어 300제' 카테고리의 다른 글

050. 매크로 이해하기  (0) 2010.11.22
049. #include 문 이해하기  (0) 2010.11.22
047. 함수와 인수 이해하기  (0) 2010.11.22
046. 데이터형 정의하기  (0) 2010.11.22
045. 열거형 이해하기  (0) 2010.11.22
//