여기에서 우리는 C 프로그래밍에 대한 몇 가지 흥미로운 사실을 볼 것입니다. 아래와 같습니다.
- 때때로 일부 switch 문의 case 레이블은 if-else 문 안에 배치될 수 있습니다.
예시
#include <stdio.h>
main() {
int x = 2, y = 2;
switch(x) {
case 1:
;
if (y==5) {
case 2:
printf("Hello World");
}
else case 3: {
//case 3 block
}
}
} 출력
Hello World
-
배열[인덱스]는 인덱스[배열]로 쓸 수 있습니다. 그 이유는 포인터 산술을 사용하여 배열 요소에 액세스하기 때문입니다. array[5]의 값은 *(array + 5)입니다. 5[array]와 같이 역순이면 *(5 + array)와 동일합니다.
예시
#include <stdio.h>
main() {
int array[10] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 110};
printf("array[5]: %d\n", array[5]);
printf("5[array]: %d\n", 5[array]);
} 출력
array[5]: 66 5[array]: 66
- 대괄호 [,] 대신 <:, :>을 사용하고 중괄호 {,} 대신 <%, %>를 사용할 수 있습니다.
예시
#include <stdio.h>
main() <%
int array<:10:> = <%11, 22, 33, 44, 55, 66, 77, 88, 99, 110%>;
printf("array[5]: %d\n", array<:5:>);
%> 출력
array[5]: 66
-
우리는 이상한 곳에서 #include를 사용할 수 있습니다. 여기에서 abc.txt 파일이 "The Quick Brown Fox Jumps Over Lazy Dog" 줄을 잡고 있다고 가정해 보겠습니다. printf 문 뒤에 파일을 포함하면 해당 파일 내용을 인쇄할 수 있습니다.
예시
#include <stdio.h>
main() {
printf
#include "abc.txt" ;
} 출력
The Quick Brown Fox Jumps Over The Lazy Dog
- scanf()에서 %*d를 사용하여 입력을 무시할 수 있습니다.
예시
#include <stdio.h>
main() {
int x;
printf("Enter two numbers: ");
scanf("%*d%d", &x);
printf("The first one is not taken, the x is: %d", x);
} 출력
Enter two numbers: 56 69 The first one is not taken, the x is: 69