Programming Language & Its Applications — Introduction to C Programming, NEC licence examination syllabus (Nepal Engineering Council).
a + b * c is not (a + b) * c, and ++i is not i++. These distinctions cost marks and cause real bugs.
C has an unusually rich operator set — including some, like the comma operator and compound assignment, that other languages lack. The syntax is easy; the difficulty is precedence and associativity, which determine how an expression without parentheses is actually grouped. Most C exam numericals are precedence puzzles in disguise.
755 in chmod 755 is three groups of three bits, tested with &. Every image format packs RGBA into one 32-bit integer and extracts channels with >> and & 0xFF. Network code masks IP addresses with & to find the subnet. Search for "bit manipulation tricks" — checking whether a number is a power of two is one line, n & (n-1), and understanding why is genuinely delightful.a & 1 == 0 parses as a & (1 == 0), which is almost never what you meant. That single misplacement is a classic real-world bug.
Evaluate step by step: int r = 10 + 20 * 3 % 7 - 4 / 2;
#include <stdio.h>
int main(void) {
int i = 5, j = 5;
printf("%d\n", i++); /* prints 5, THEN i becomes 6 */
printf("i is now %d\n", i);
printf("%d\n", ++j); /* j becomes 6 FIRST, prints 6 */
printf("j is now %d\n", j);
int a = 5;
int b = a++ + ++a; /* UNDEFINED BEHAVIOUR - avoid! */
printf("b = %d (compiler dependent)\n", b);
return 0;
}
Output:
5
i is now 6
6
j is now 6
b = 12 (compiler dependent)
#include <stdio.h>
int main(void) {
unsigned char a = 12; /* 0000 1100 */
unsigned char b = 10; /* 0000 1010 */
printf("a & b = %d\n", a & b); /* AND */
printf("a | b = %d\n", a | b); /* OR */
printf("a ^ b = %d\n", a ^ b); /* XOR */
printf("~a = %d\n", (unsigned char)~a);
printf("a << 2 = %d\n", a << 2); /* multiply by 4 */
printf("a >> 2 = %d\n", a >> 2); /* divide by 4 */
return 0;
}
Output:
a & b = 8 0000 1000
a | b = 14 0000 1110
a ^ b = 6 0000 0110
~a = 243 1111 0011
a << 2 = 48 0011 0000 (12 x 4)
a >> 2 = 3 0000 0011 (12 / 4)
Shifting is the cheapest multiply and divide a processor has — which is why compilers turn x * 8 into x << 3 automatically.
#include <stdio.h>
int side_effect(void) {
printf(" side_effect called!\n");
return 1;
}
int main(void) {
int x = 0;
/* && stops at the first FALSE */
if (x != 0 && side_effect()) { } /* never calls it */
printf("after && test\n");
/* || stops at the first TRUE */
if (x == 0 || side_effect()) { } /* never calls it */
printf("after || test\n");
/* The idiom this enables — safe division: */
int d = 0;
if (d != 0 && 100/d > 5)
printf("big\n");
else
printf("guarded against divide-by-zero\n");
return 0;
}
Output:
after && test
after || test
guarded against divide-by-zero
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…