Nepal's open education portal

EduNepal

C Programming — Complete Course Notes (BCA 1st Sem)

Comprehensive master notes covering fundamentals, syntax, control statements, arrays, pointers, structures, and file I/O for TU BCA 1st semester.

C Programming · CACS101 · Semester 1

BCA Curriculum 2079

Welcome to the comprehensive lecture and study notes for C Programming (Course Code: CACS101), strictly adhering to the Tribhuvan University (TU) BCA 1st Semester syllabus.


1. Fundamentals of C Programming

C is a general-purpose, procedural, middle-level programming language developed in 1972 by Dennis M. Ritchie at Bell Telephone Laboratories to develop the UNIX operating system.

Key Characteristics:

  • Middle-Level Language: Combines the high-level readability of user-defined functions and structures with low-level direct memory manipulation using pointers.
  • Structured Programming: Modular design using functions, loops, and block-scoped variables.
  • Portability & Efficiency: Generates compact, highly optimized native machine assembly code.

```c #include <stdio.h>

int main(void) { printf("Hello, EduNepal Student!\n"); return 0; } ```


2. Compilation and Execution Process

The transition from human-readable C source code (.c) to an executable binary (.exe or ELF) consists of four sequential phases:

  1. Preprocessing (cpp): Expands macros (#define), strips comments, and includes header files (#include).
  2. Compilation (cc1): Converts preprocessed code into assembly instructions.
  3. Assembly (as): Translates assembly code into relocatable object code (.o or .obj).
  4. Linking (ld): Combines object files with C standard libraries (libc) to produce the final executable.

3. Data Types and Memory Footprint (32/64-bit Architecture)

Data TypeKeywordSize (Bytes)Format SpecifierTypical Range
Characterchar1%c-128 to 127
Integerint4%d-2,147,483,648 to 2,147,483,647
Floating Pointfloat4%f1.2E-38 to 3.4E+38 (6 decimals)
Double Precisiondouble8%lf2.3E-308 to 1.7E+308 (15 decimals)
Voidvoid0-Valueless

4. Control Statements & Decision Making

If-Else Decision Making:

```c #include <stdio.h>

int main() { int score; printf("Enter exam marks (0-100): "); scanf("%d", &score);

if (score >= 80) { printf("Grade: Distinction\n"); } else if (score >= 60) { printf("Grade: First Division\n"); } else if (score >= 40) { printf("Grade: Pass\n"); } else { printf("Grade: Fail\n"); } return 0; } ```


5. Pointers and Dynamic Memory Allocation

A pointer is a variable that stores the memory address of another variable.

  • The address-of operator & retrieves the memory address.
  • The dereference operator * accesses the value stored at that address.

```c #include <stdio.h> #include <stdlib.h>

int main() { int ptr = (int )malloc(5 * sizeof(int)); if (ptr == NULL) { printf("Memory allocation failed!\n"); return 1; }

for (int i = 0; i < 5; i++) { ptr[i] = (i + 1) 10; printf("Element %d at %p = %d\n", i, (void)&ptr[i], ptr[i]); }

free(ptr); // Prevent memory leak return 0; } ```

Related notes

View all →

Related video lessons

View all →

Practice questions

View all →
  • Which of the following is an invalid variable name in C? (a) _salary (b) 1st_rank (c) total_sum (d) age2objective · 1 marks
  • What is the return type of the `malloc()` function in C? (a) `int*` (b) `char*` (c) `void*` (d) `float*`objective · 1 marks
  • Explain the difference between call by value and call by reference in C with suitable code snippets.short · 5 marks
  • What is recursion? Write a recursive function in C to calculate the factorial of a positive integer.short · 5 marks
  • Explain dynamic memory allocation in C. Differentiate between `malloc()` and `calloc()`. Write a C program to dynamically allocate memory for N integers, sort them in ascending order, and free the memory.long · 10 marks

View all questions for this subject →

← Back to all notes