Detailed Notes For BCA Course
Subject : Programming in C
Course : BCA
Module I: Overview of C Language
1. Overview of C
Importance of C: C is a foundational programming language known for its efficiency and flexibility. It
is widely used in developing operating systems (e.g., Linux), embedded systems, and applications
requiring high performance.
Sample C Programs: Simple programs help illustrate the basic syntax and functionality of C:
Example 1: Printing "Hello World":
#include<stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
Example 2: Calculating the sum of two numbers:
#include<stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
Basic Structure of C Programs: A C program consists of the following components:
1. Preprocessor Directives: Statements starting with # (e.g., #include<stdio.h>).
2. Main Function: Entry point of the program (int main()).
3. Statements and Expressions: Code inside the main function or other functions.
Programming Style: Follow these best practices for readability:
Use consistent indentation.
Use descriptive variable names (e.g., studentAge instead of a).
Include comments for clarity.
Executing a C Program:
1. Write the program in a text editor or IDE.
2. Compile it using a compiler like GCC.
3. Execute the generated binary file.
2. Constants, Variables, and Data Types
Character Set: C supports characters, numbers, and special symbols. For example:
Letters: A-Z, a-z
Digits: 0-9
Special Characters: +, -, *, /, etc.
C Tokens: Tokens are the smallest building blocks of a program:
Keywords (e.g., int, return)
Identifiers (e.g., variable names)
Constants (e.g., 3.14)
Operators (e.g., +, -)
Constants and Variables:
Constants: Fixed values. Example: #define PI 3.14
Variables: Named locations in memory. Example:
int age = 20;
Data Types: C has basic, derived, and user-defined data types:
Basic: int, float, char
Derived: Arrays, pointers
User-defined: Structures, unions
Declaring Variables: Syntax: data_type variable_name; Example:
int age;
float salary;
3. Operators and Expressions
Arithmetic Operators: Perform mathematical operations:
Example:
int a = 10, b = 5;
printf("Sum: %d\n", a + b); // Output: Sum: 15
Relational Operators: Compare values:
Example:
if (a > b) {
printf("a is greater\n");
Logical Operators: Combine conditions:
Example:
if (a > 0 && b > 0) {
printf("Both are positive\n");
Type Conversions:
Implicit: Automatically done by the compiler.
Explicit (Casting):
float avg = (float)(a + b) / 2;
4. Managing Input and Output Operations
Formatted Input/Output: Using scanf() and printf():
Example:
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
printf("Marks: %d\n", marks);
Module II: Decision Making with Conditional Statements
1. Decision Making and Branching
If-Else Statement:
Example:
if (marks >= 50) {
printf("Pass\n");
} else {
printf("Fail\n");
Switch Case: Useful for menu-driven programs:
Example:
int choice;
printf("Enter choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Option 1\n"); break;
case 2: printf("Option 2\n"); break;
default: printf("Invalid choice\n");
2. Decision Making and Looping
For Loop: Iterates a fixed number of times:
Example:
for (int i = 0; i < 5; i++) {
printf("Iteration: %d\n", i);
While Loop: Repeats while the condition is true:
Example:
int i = 0;
while (i < 5) {
printf("Iteration: %d\n", i);
i++;
3. Arrays and Strings
One-Dimensional Arrays:
Example:
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d\n", arr[i]);
String Handling: Using library functions:
Example:
char str1[10] = "Hello";
char str2[10] = "World";
strcat(str1, str2);
printf("%s\n", str1);
Module III: Structures, Pointers, and Functions in C
1. User-Defined Functions
Syntax:
return_type function_name(parameters) {
// function body
Recursion:
Example:
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
2. Structures and Unions
Structure Definition:
Example:
struct Student {
char name[50];
int age;
};
Unions:
Example:
union Data {
int i;
float f;
};
3. Pointers and File Handling
Pointers:
Example:
int a = 10, *p;
p = &a;
printf("%d\n", *p);
File Handling:
Example:
FILE *f;
f = fopen("[Link]", "w");
fprintf(f, "Hello, File!\n");
fclose(f);