Programming with C
Comprehensive exam revision notes for BCA-103N Unit 1 (C Fundamentals, Structure, Data Types & Operators). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Programming with C
C Fundamentals, Structure, Data Types & Operators
Core Definitions & Key Concepts
Who developed C and when
Dennis Ritchie at Bell Laboratories (AT&T) in 1972, derived from Ken Thompson's B language and Martin Richards' BCPL, originally created to develop the UNIX operating system.
Why is C referred to as a 'Middle-Level Language'
C bridges high-level abstraction (structured programming, readable syntax, data types) with low-level assembly capabilities (direct memory manipulation via pointers, bitwise operations, register access).
How many standard keywords exist in ANSI C89/90? Can they be used as variable names
There are exactly 32 standard ANSI keywords (e.g. int, return, static, sizeof). No, keywords are reserved by the compiler and cannot be used as user identifiers.
State the rules for naming valid identifiers in C
- May contain letters (A-Z, a-z), digits (0-9), and underscores (
_). - Must begin with a letter or an underscore, NOT a digit.
- Keywords cannot be used.
- C is case-sensitive (
total!=Total). - No spaces or special symbols (
@,$,#) permitted.
Implicit Type Conversion (Coercion) and Explicit Type Casting
Implicit Conversion: Automatically performed by compiler following numeric hierarchy (e.g., float + int promotes int to float).
Explicit Casting: Manually forced by programmer using cast operator (type)value (e.g., (float)total / count).
What is the difference between Pre-increment (++x) and Post-increment (x++)
`++x` (Pre-increment): Increments x by 1 first, then returns the updated value in the expression.
`x++` (Post-increment): Returns the current value of x in the expression first, and increments x afterwards.
What is the result of integer division 7 / 2 in C
The result is 3 (not 3.5). In C, dividing two integers truncates the fractional part, yielding an integer quotient.
The Ternary / Conditional Operator (? :)
A compact three-operand operator replacing simple if-else blocks: condition ? expression_if_true : expression_if_false; Example: max = (a > b) ? a : b;
What are Format Specifiers in printf()? List four common ones
Format specifiers tell I/O functions how to interpret binary data: %d or %i (signed decimal integer), %f (float), %lf (double), %c (single char), %s (string), %p (pointer address).
Why is gets() dangerous and what safe alternative is used in modern C
gets() performs no array bounds checking, causing fatal buffer overflow security vulnerabilities. Modern C standard uses fgets(buffer, sizeof(buffer), stdin) which limits input length.
Conceptual & Architectural Analysis
The complete 5-stage Compilation and Execution Pipeline of a C Program
[hello.c Source Code]
|
v PREPROCESSOR (gcc -E hello.c)
[hello.i Expanded Code: header files included, macros expanded, comments stripped]
|
v COMPILER (gcc -S hello.i)
[hello.s Assembly Language Mnemonics: translated to target CPU architecture]
|
v ASSEMBLER (gcc -c hello.s)
[hello.o / hello.obj Relocatable Machine Code: binary opcodes with unresolved symbols]
|
v LINKER (gcc hello.o -lc)
[hello.exe Executable Binary: merges object files with C standard runtime library libc]
|
v LOADER
RUNNING PROCESS IN RAM (Code, Data, Stack, Heap)The primary Fundamental Data Types in C with their typical memory sizes and ranges (32/64-bit GCC)
| Data Type | Keyword | Size (Bytes) | Format | Value Range |
|---|---|---|---|---|
| Character | char | 1 | %c | -128 to +127 (or 0 to 255 unsigned) |
| Integer | int | 4 | %d | -2,147,483,648 to +2,147,483,647 |
| Short Int | short | 2 | %hd | -32,768 to +32,767 |
| Long Int | long | 4 or 8 | %ld | -2^63 to +2^63 - 1 |
| Single Float | float | 4 | %f | ±3.4E-38 to ±3.4E+38 (6 decimal precision) |
| Double Float | double | 8 | %lf | ±1.7E-308 to ±1.7E+308 (15 decimal precision) |
| Void | void | 0 | - | Empty / valueless type for function returns/pointers |
Classify Operators in C and explain Operator Precedence and Associativity
C operators categorized by function:
- Arithmetic:
+,-,*,/,%(modulus integer remainder). - Relational:
==,!=,<,>,<=,>=(return 1 for true, 0 for false). - Logical:
&&(AND),||(OR),!(NOT) with short-circuit evaluation. - Bitwise:
&(AND),|(OR),^(XOR),~(NOT),<<(left shift),>>(right shift). - Assignment:
=,+=,-=,*=,/=,%=. - Unary / Special:
sizeof,&(address-of),*(dereference),++,--.
Precedence & Associativity: Precedence dictates which operator binds first in mixed expressions (e.g. * before +). When operators have equal precedence, Associativity dictates evaluation order: Left-to-Right (most operators) or Right-to-Left (Unary, Assignment, Ternary).
Formatted I/O (printf, scanf) versus Unformatted I/O (getchar, putchar, gets, puts)
- Formatted I/O: Uses format specifiers to convert internal binary representations to ASCII characters or vice versa.
printf("Sum = %d\n", sum);scanf("%d %f", &age, &gpa);(requires address-of&operator for variables).- Unformatted I/O: Transfers raw characters and strings directly without format string parsing, resulting in faster execution.
ch = getchar();reads a single character from stdin.putchar(ch);prints a single character to stdout.puts(str);prints string with an automatic trailing newline\n.
Short-Circuit Evaluation in C Logical Operators with code examples
In logical expressions involving && and ||, C evaluates from left to right and stops evaluating as soon as the final truth value is guaranteed:
- `&&` (Logical AND): If the left operand is FALSE (0), the entire expression MUST be false; the right operand is never executed.
- `||` (Logical OR): If the left operand is TRUE (non-zero), the entire expression MUST be true; the right operand is never executed.
1int a = 0, b = 10;2if (a && ++b) { /* ++b is NEVER evaluated because a is 0 */ }3printf("b = %d\n", b); // Output: b = 10 (unchanged!)In-Depth Analysis & Implementation
Write a complete, structured C program demonstrating formatted input, calculations, type casting, and formatted output with clear documentation
1/**2 * Program: Student Grade and Percentage Calculator3 * Demonstrates: Variables, Constants, Formatted I/O, Type Casting, Precedence4 * Compiler: GCC (gcc student.c -o student)5 */6#include <stdio.h>7
8#define MAX_MARKS_PER_SUBJECT 1009#define TOTAL_SUBJECTS 510
11int main(void) {12 int roll_no;13 char grade;14 int s1, s2, s3, s4, s5;15 int total_obtained;16 float percentage;17
18 printf("==============================================\n");19 printf(" MJPRU BCA SEM-1 GRADE REPORT GENERATOR \n");20 printf("==============================================\n");21
22 // Input with validation23 printf("Enter Student Roll Number: ");24 if (scanf("%d", &roll_no) != 1) {25 printf("Error: Invalid roll number input!\n");26 return 1;27 }28
29 printf("Enter marks for 5 subjects (out of 100 each):\n");30 printf("BCA-101 (Computer Fundamentals): "); scanf("%d", &s1);31 printf("BCA-103 (Programming in C): "); scanf("%d", &s2);32 printf("BCA-105 (Basic Mathematics): "); scanf("%d", &s3);33 printf("BCA-107 (Communicative English): "); scanf("%d", &s4);34 printf("BCA-109 (Principles of Mgmt): "); scanf("%d", &s5);35
36 // Compute total37 total_obtained = s1 + s2 + s3 + s4 + s5;38
39 // Explicit type casting to prevent integer truncation40 percentage = ((float)total_obtained / (TOTAL_SUBJECTS * MAX_MARKS_PER_SUBJECT)) * 100.0f;41
42 // Determine grade via ternary / conditions43 if (percentage >= 75.0f) {44 grade = 'A';45 } else if (percentage >= 60.0f) {46 grade = 'B';47 } else if (percentage >= 40.0f) {48 grade = 'C';49 } else {50 grade = 'F';51 }52
53 // Formatted display54 printf("\n-------------- ACADEMIC SUMMARY --------------\n");55 printf("Roll Number: %d\n", roll_no);56 printf("Total Marks: %d / %d\n", total_obtained, TOTAL_SUBJECTS * MAX_MARKS_PER_SUBJECT);57 printf("Percentage: %.2f%%\n", percentage);58 printf("Awarded Grade: %c\n", grade);59 printf("Result Status: %s\n", (grade != 'F') ? "PASSED WITH MERIT" : "FAILED");60 printf("----------------------------------------------\n");61
62 return 0;63}Bitwise Operators in C (&, |, ^, ~, <<, >>) with bit-level truth tables and write a C program to check and manipulate bits
1. Bitwise Operations Explained
Bitwise operators manipulate individual binary bits within integer types:
- Bitwise AND (`&`): Result bit is 1 only if BOTH operand bits are 1 (Used for Bit Masking/Clearing).
- Bitwise OR (`|`): Result bit is 1 if EITHER operand bit is 1 (Used for Setting Bits).
- Bitwise XOR (`^`): Result bit is 1 if bits are DIFFERENT (Used for Toggling bits and fast swapping without temporary variable).
- Bitwise NOT (`~`): Inverts all bits (One's complement:
~x = -(x + 1)). - Left Shift (`<< n`): Shifts bits left by n positions, filling right with zeros. Equivalent to multiplying by
2^n. - Right Shift (`>> n`): Shifts bits right by n positions. Equivalent to integer division by
2^n.
2. Practical Bit-Manipulation Code
#include <stdio.h>
int main(void) {
unsigned int num = 29; // Binary: 0001 1101
int bit_pos = 1; // 0-indexed position
// 1. Check if bit at pos 1 is SET (1) or CLEARED (0)
if (num & (1 << bit_pos)) {
printf("Bit at position %d is SET (1)\n", bit_pos);
} else {
printf("Bit at position %d is CLEARED (0)\n", bit_pos);
}
// 2. Set bit at pos 1
num |= (1 << bit_pos);
// 3. Clear bit at pos 2 (make it 0)
num &= ~(1 << 2);
// 4. Toggle bit at pos 0 (flip it)
num ^= (1 << 0);
// 5. Fast power-of-two multiplication using <<
printf("29 * 4 (29 << 2) = %u\n", num << 2);
return 0;
}Discuss Operator Precedence and Associativity in C with the master hierarchy table and analyze complex evaluation expressions
1. The ANSI C Operator Precedence Table
| Level | Operators | Description | Associativity | ||
|---|---|---|---|---|---|
| 1 (Highest) | (), [], ->, . | Function call, Array subscript, Structure member access | Left to Right | ||
| 2 | !, ~, ++, --, +, -, *, &, (type), sizeof | Unary logical/bitwise, Pre-inc/dec, Address-of, Indirection, Cast | Right to Left | ||
| 3 | *, /, % | Multiplicative arithmetic | Left to Right | ||
| 4 | +, - | Additive arithmetic | Left to Right | ||
| 5 | <<, >> | Bitwise shift | Left to Right | ||
| 6 | <, <=, >, >= | Relational inequality | Left to Right | ||
| 7 | ==, != | Relational equality | Left to Right | ||
| 8 | & | Bitwise AND | Left to Right | ||
| 9 | ^ | Bitwise XOR | Left to Right | ||
| 10 | ` | ` | Bitwise OR | Left to Right | |
| 11 | && | Logical AND | Left to Right | ||
| 12 | ` | ` | Logical OR | Left to Right | |
| 13 | ? : | Ternary conditional | Right to Left | ||
| 14 | =, +=, -=, *=, /=, %=, etc. | Assignment operators | Right to Left | ||
| 15 (Lowest) | , | Comma operator | Left to Right |
2. Trace Evaluation of Complex Expression
Evaluate: int x = 5 + 3 * 2 > 10 && 4 != 2 + 2;
*evaluated first:3 * 2 = 6. Expression becomes5 + 6 > 10 && 4 != 2 + 2- Additions evaluated:
5 + 6 = 11and2 + 2 = 4. Expression:11 > 10 && 4 != 4 - Relational evaluated:
11 > 10is TRUE (1);4 != 4is FALSE (0). Expression:1 && 0 - Logical AND:
1 && 0evaluates to0.
Final stored value of x is 0.
Important Exam Points & Quick Revision
In BCA-103N Unit 1, always be ready for: Operator precedence problems, difference between Pre-increment and Post-increment, and explaining the compilation pipeline. Trace operator expressions step-by-step in your answer paper.
100% left on this page — finishing it closes the gap for C Fundamentals, Structure, Data Types & Operators.
C Fundamentals, Structure, Data Types & Operators still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.