Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Interesting Facts about C Programming
C programming language has several interesting and lesser-known features that can surprise even experienced programmers. Here are some fascinating facts about C that demonstrate its flexibility and unique behaviors.
Fact 1: Switch Case Labels Inside If-Else
Case labels in a switch statement can be placed inside if-else blocks due to the way switch works with jump labels −
#include <stdio.h>
int main() {
int x = 2, y = 2;
switch(x) {
case 1:
;
if (y == 5) {
case 2:
printf("Hello World");
}
else case 3: {
printf("Case 3 executed");
}
}
return 0;
}
Hello World
Fact 2: Reverse Array Indexing
Array indexing can be written in reverse order because array[index] is equivalent to *(array + index), which is the same as *(index + array) −
#include <stdio.h>
int main() {
int array[10] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 110};
printf("array[5]: %d<br>", array[5]);
printf("5[array]: %d<br>", 5[array]);
return 0;
}
array[5]: 66 5[array]: 66
Fact 3: Alternative Bracket Notation
C provides digraph sequences as alternatives for brackets: <: and :> for square brackets, <% and %> for curly braces −
#include <stdio.h>
int main() <%
int array<:10:> = <%11, 22, 33, 44, 55, 66, 77, 88, 99, 110%>;
printf("array[5]: %d<br>", array<:5:>);
return 0;
%>
array[5]: 66
Fact 4: Ignoring Input with scanf
The %*d format specifier in scanf() reads and discards an integer input without storing it −
#include <stdio.h>
int main() {
int x;
printf("Enter two numbers: ");
scanf("%*d%d", &x);
printf("The first one is ignored, x is: %d<br>", x);
return 0;
}
Enter two numbers: 56 69 The first one is ignored, x is: 69
Key Points
- Switch statements use jump labels, allowing cases inside conditional blocks
- Array indexing works through pointer arithmetic, making reverse indexing possible
- Digraphs provide alternative syntax for systems lacking certain characters
- The asterisk (*) in scanf format strings creates assignment suppression
Conclusion
These C programming facts showcase the language's low-level flexibility and historical design decisions. While some features are rarely used in modern code, understanding them helps appreciate C's underlying mechanisms and can be useful for code golf or legacy system maintenance.
