037. 무조건 분기문 이해하기037. 무조건 분기문 이해하기

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

main()
{
	int i;
	int j;

	for( i = 1; i <= 100; i++ )
	{
		for( j = 1; j <= 99; j++ )
		{
			printf("%d * %d = %2d\n",i,j, i*j );
			if( i == 9 && j == 9 ) goto ku_ku_end;
		}
	}

ku_ku_end:;
}
//

036. 조건 순환문 이해하기 2(do~while~continue~break)036. 조건 순환문 이해하기 2(do~while~continue~break)

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

main()
{
	int i = 1;
	int hap = 0;

	do 
	{
		hap = hap + i;
		i++;				// i는 1,2,3,4,5,6,7,8,9,10,11까지 증가
	} while( i <= 10 );		// i가 10보다 작거나 같은 동안 반복, 11이면 순환 탈출

	printf( "hap = %d",hap );		// hap = 55
}
//

035. 조건 순환문 이해하기 1(while~continue~break)035. 조건 순환문 이해하기 1(while~continue~break)

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

main()
{
	int i = 1;
	int hap = 0;

	while( i <= 10 )
	{
		hap = hap + i;
		i++;
	}

	printf( "hap = %d",hap );
}
//

034. 조건 선택문 이해하기(switch~case~default)034. 조건 선택문 이해하기(switch~case~default)

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

main()
{
	int i = 5;

	switch( i )
	{
	case 1:
		printf( "i는 1입니다." );
		break;
	case 2:
		printf( "i는 2입니다." );
		break;
	default:
		printf( "i는 %d입니다.",i );
		break;
	}
}
//

033. 중첩 순환문 이해하기(for~continue~break)033. 중첩 순환문 이해하기(for~continue~break)

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

main()
{
	int i;
	int j;

	for( i = 1; i <= 9; i++ )
	{
		for( j = 1; j <= 9; j++ )
		{
			printf( "%d * %d = %2d\n",i,j,i*j );
		}
	}
}
//

032. 중첩 조건문 이해하기(if~else)032. 중첩 조건문 이해하기(if~else)

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

main()
{
	int i = 1;
	int j = 2;
	int k = 7;

	if( i == 1 )
	{
		if( j == 2 )
		{
			if( k == 3 )
				printf( "i=1, j=2, j=3입니다." );
			else if( k == 4 )
				printf( "i=1, j=2, k=4입니다." );
			else if( k == 5 )
				printf( "i=1, j=2, k=5입니다." );
			else
				printf( "i=1, j=2, k=%d입니다.",k );
		}
	}
}
//

031. sizeof 연산자 이해하기031. sizeof 연산자 이해하기

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

main()
{
	char i;
	int j;
	double k;

	printf( "%d\n",sizeof(i) );		// 1
	printf( "%d\n",sizeof(j) );		// 4
	printf( "%d\n",sizeof(k) );		// 8
}
//

030. 캐스트 연산자 이해하기030. 캐스트 연산자 이해하기

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

main()
{
	int x = 5, y = 2;

	printf( "%d \n", x / y );				// 2
	printf( "%f \n", (double)x / y );		// 2.500000
}
//

029. 비트 연산자 이해하기(|, &, ~, ^, <<, >>)029. 비트 연산자 이해하기(|, &, ~, ^, <<, >>)

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

main()
{
	unsigned char ch = 255, mask = 0x7F;

	printf( "%d \n",ch );				// 255
	printf( "%d \n",ch & mask);			// 127
	printf( "%d \n",(char)~ch );		// 0
	printf( "%d \n",ch ^ ch );			// 0
	printf( "%d \n",ch >> 1 );			// 127
	printf( "%d \n",mask << 1 );		// 254
}

관심있게 볼 부분은 mask이다. 이것은 IP Address와 Subnet Mask를 비트 마스크할 때와 같은 원리이다.

이러한 비트 연산자를 통해 빠른 연산을 할 수 있다.

물론 우리의 Desktop은 해당 사항은 별로 없을 것 같다.

아마 특수 형태 H/W, 예를 들면 방화벽에서는 빠른 연산이 필수 일 것이다.

//

028. 쉼표 연산자 이해하기(,)028. 쉼표 연산자 이해하기(,)

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

main()
{
	int x = 1, y = 2, max;

	max = x > y? x : y;

	printf( "max = %d, x = %d, y = %d", max, x, y );
}
//