1. Difference between actual and formal parameters
Actual parameters: Values/variables passed to a function when it is called
Formal parameters: Variables declared in the function definition to receive the passed values
Example:
1 2 |
void sum(int a, int b); // a and b are formal parameters sum(x, y); // x and y are actual parameters |
2. Use of ftell() and rewind() functions
-
ftell(): Returns the current file position indicator (in bytes) of the file stream
Example:long pos = ftell(filePtr);
-
rewind(): Sets the file position indicator to the beginning of the file
Example:rewind(filePtr);
3. Four keywords of C
-
int
-
if
-
return
-
while
(Other examples:char
,float
,for
,switch
)
4. NULL pointer
A pointer that doesn’t point to any memory location (value 0). Used to indicate that the pointer is not currently valid or initialized.
Example:
1 |
int *ptr = NULL; |
5. When to use -> (arrow) operator
Used to access members of a structure or union through a pointer to that structure/union.
Example:
1 2 3 |
struct Student { char name[20]; }; struct Student *sptr; sptr->name; // Equivalent to (*sptr).name |
6. What’s wrong with: name = "NCCS"
-
name
must be a character array or pointer -
For arrays, use
strcpy(name, "NCCS")
-
For pointers, first allocate memory
Correct versions:
1 2 |
char name[20]; strcpy(name, "NCCS"); // Array version char *name = malloc(20); strcpy(name, "NCCS"); // Pointer version |
7. Can { } be used for single line of code?
Yes, curly brackets can enclose a single line, though not required. It’s good practice for consistency and future code expansion.
Example:
1 2 3 |
if (condition) { single_statement; } |
8. When “switch” is preferable over “if”
-
When testing a single variable against multiple constant values
-
For better readability with many conditions
-
When compiler can optimize with jump tables
Example:
1 2 3 4 5 |
switch(grade) { case 'A': printf("Excellent"); break; case 'B': printf("Good"); break; // More cases... } |
9. Preprocessor with example
The preprocessor processes source code before compilation. Directives begin with #
.
Example:
1 2 3 4 5 |
#define PI 3.14159 // Defines a macro #include <stdio.h> // Includes header file #ifdef DEBUG // Conditional compilation printf("Debug mode"); #endif |
10. Bitwise operator
Operators that work on bits of integer types:
Operator | Meaning | Example | ||
---|---|---|---|---|
& |
AND | a & b |
||
|
OR | a |
b | |
^ |
XOR | a ^ b |
||
~ |
NOT (flips bits) | ~a |
||
<< |
Left shift | a << 2 |
||
>> |
Right shift | a >> 1 |
Example usage:
1 2 |
unsigned char flags = 0x0A; // 00001010 in binary flags = flags | 0x01; // Set least significant bit (00001011) |
12. Program to check if first name contains only lowercase alphabets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> #include <ctype.h> #include <stdbool.h> bool isAllLowercase(const char *name) { while(*name) { if(!islower(*name) || !isalpha(*name)) return false; name++; } return true; } int main() { char firstName[50]; printf("Enter your first name: "); scanf("%s", firstName); if(isAllLowercase(firstName)) printf("Valid: Only lowercase alphabets\n"); else printf("Invalid: Contains non-lowercase characters\n"); return 0; } |
17. Program to sort array in descending order using pointers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> void sortDescending(int *arr, int size) { for(int i = 0; i < size-1; i++) { for(int j = i+1; j < size; j++) { if(*(arr + i) < *(arr + j)) { // Swap using pointers int temp = *(arr + i); *(arr + i) = *(arr + j); *(arr + j) = temp; } } } } int main() { int arr[8]; printf("Enter 8 integers:\n"); for(int i = 0; i < 8; i++) { scanf("%d", arr + i); // Equivalent to &arr[i] } sortDescending(arr, 8); printf("Sorted array in descending order:\n"); for(int i = 0; i < 8; i++) { printf("%d ", *(arr + i)); // Equivalent to arr[i] } return 0; } |
18. Palindrome Check
19. Program to print the binary pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int rows; printf("Enter number of rows: "); scanf("%d", &rows); for(int i = 1; i <= rows; i++) { for(int j = 1; j <= i; j++) { printf("%d", j % 2); // Alternates between 1 and 0 } printf("\n"); } return 0; } |
20. Program to find largest and smallest in array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> struct Course { char course_code[10]; char name[50]; int credit_hour; }; int main() { struct Course courses[5]; // Input course data printf("Enter details for 5 courses:\n"); for(int i = 0; i < 5; i++) { printf("\nCourse %d:\n", i+1); printf("Course Code: "); scanf("%s", courses[i].course_code); printf("Course Name: "); scanf(" %[^\n]", courses[i].name); // Reads entire line printf("Credit Hour: "); scanf("%d", &courses[i].credit_hour); } // Display courses with credit hour > 2 printf("\nCourses with credit hour > 2:\n"); for(int i = 0; i < 5; i++) { if(courses[i].credit_hour > 2) { printf("\nCourse Code: %s\n", courses[i].course_code); printf("Course Name: %s\n", courses[i].name); printf("Credit Hour: %d\n", courses[i].credit_hour); } } return 0; } |
a