Unit 1 of 1 · B.Sc IT Sem 1

Unit 1: C programming implementation

Software Lab-I (Programming in C) notes · PTU syllabus (BSIT105/BSBC105)

4 min read9 topics11 exam questions
On this page
  1. Unit summary
  2. How to approach every lab program
  3. Data types, scope and formatted and unformatted I/O
  4. Operators including bitwise operators
  5. Decision-making programs
  6. Loop and switch programs
  7. Function and recursion programs
  8. Preprocessor directives and header files
  9. Arrays and string functions
  10. Pointers and file creation and editing
  11. Key terms
  12. Quick revision
  13. Important questions

Unit summary

This lab puts Programming in C into practice: keywords and identifiers, data types and scope, formatted and unformatted I/O, all operator families including bitwise, decision-making constructs, loops, functions and recursion, preprocessor directives and header files, arrays and string functions, pointers, and creating and editing files. Each section gives the logic and a model program, with viva questions examiners ask most often.

After this unit you can

  • Write programs using data types, operators and formatted and unformatted I/O
  • Use decision-making constructs and loops
  • Write functions, recursive functions and preprocessor macros
  • Use arrays, strings, pointers and files

PTU syllabus topics

  • Keywords and identifiers
  • variable/constant data types and scope
  • formatted and non-formatted I/O
  • arithmetic/logical/conditional/assignment/bitwise operators
  • decision-making constructs (switch, if-else, nested if)
  • loops (while, do-while, for)
  • function definition/declaration/parameterization/recursion
  • preprocessor directives
  • standard header files
  • array declarations and string functions
  • pointer declarations
  • file creation and editing
ProcessLab workflow for every C program
  1. 1

    Understand the problem

    Inputs and outputs

  2. 2

    Write the algorithm or flowchart

  3. 3

    Code it

    gcc file.c -o file

  4. 4

    Compile and fix errors

  5. 5

    Test with sample and edge inputs

  6. 6

    Record output in the lab file

1

Topic 1

How to approach every lab program

ProcessLab workflow
  1. 1

    Understand the problem

    Inputs and expected output

  2. 2

    Write the algorithm or flowchart

  3. 3

    Type the program

    Indent and comment it

  4. 4

    Compile

    gcc prog.c -o prog

  5. 5

    Test

    Normal, boundary and invalid inputs

  6. 6

    Record in the practical file

    Aim, code, output

Exam tip

In the practical exam you are marked on the program, the output and the viva. Keep a neat practical file with the aim, program and output for every experiment.

2

Topic 2

Data types, scope and formatted and unformatted I/O

c#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);
    printf("ASCII value of %c = %d\n", ch, ch);
    printf("int: %zu, float: %zu, double: %zu, char: %zu bytes\n",
           sizeof(int), sizeof(float), sizeof(double), sizeof(char));
    return 0;
}

Printing a char with %d shows its ASCII code. sizeof gives the size of a type in bytes.

3

Topic 3

Operators including bitwise operators

c#include <stdio.h>
int main(void) {
    int a = 12, b = 10;              /* 1100 and 1010 in binary */
    printf("a & b  = %d\n", a & b);  /* 8  (1000) */
    printf("a | b  = %d\n", a | b);  /* 14 (1110) */
    printf("a ^ b  = %d\n", a ^ b);  /* 6  (0110) */
    printf("~a     = %d\n", ~a);     /* -13 */
    printf("a << 1 = %d\n", a << 1); /* 24 */
    printf("a >> 2 = %d\n", a >> 2); /* 3  */
    int max = (a > b) ? a : b;       /* conditional operator */
    printf("max = %d\n", max);
    return 0;
}
  • Scope: a variable declared inside a block is local to it; a variable declared outside all functions is global.
4

Topic 4

Decision-making programs

  • Even or odd: if (n % 2 == 0) → even, else odd.
  • Largest of three: compare with nested if or max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
  • Prime check: a number n > 1 is prime if no number from 2 to √n divides it.
cint n, i, isPrime = 1;
scanf("%d", &n);
if (n < 2) isPrime = 0;
for (i = 2; i * i <= n; i++)
    if (n % i == 0) { isPrime = 0; break; }
printf(isPrime ? "Prime" : "Not prime");
5

Topic 5

Loop and switch programs

  • Multiplication table: for (i = 1; i <= 10; i++) printf("%d x %d = %d\n", n, i, n * i);
  • Simple calculator with switch:
cchar op; float a, b;
scanf("%f %c %f", &a, &op, &b);
switch (op) {
    case '+': printf("%.2f", a + b); break;
    case '-': printf("%.2f", a - b); break;
    case '*': printf("%.2f", a * b); break;
    case '/': if (b != 0) printf("%.2f", a / b); else printf("Divide by zero"); break;
    default: printf("Invalid operator");
}
6

Topic 6

Function and recursion programs

  • Factorial using a function (iterative or recursive), largest of three using a function, and first 20 squares using a function that returns n × n.
clong factorial(int n) {
    long f = 1;
    for (int i = 2; i <= n; i++) f *= i;
    return f;
}
7

Topic 7

Preprocessor directives and header files

c#include <stdio.h>
#include "area.h"                 /* user header containing: #define PI 3.14159 */
#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main(void) {
    printf("Square of 5 = %d\n", SQUARE(5));
    printf("Max = %d\n", MAX(7, 3));
#ifdef PI
    printf("Area of circle r = 2: %.2f\n", PI * SQUARE(2));
#endif
    return 0;
}
8

Topic 8

Arrays and string functions

  • Matrix addition: read two m × n matrices and add them with nested loops: c[i][j] = a[i][j] + b[i][j];
  • String functions: use strlen, strcpy, strcmp and a reverse (with strrev or a manual swap loop).
c/* Reverse a string without strrev */
int len = strlen(s);
for (int i = 0; i < len / 2; i++) {
    char t = s[i]; s[i] = s[len - 1 - i]; s[len - 1 - i] = t;
}
9

Topic 9

Pointers and file creation and editing

  • Pointer program: print a variable's value and address using & and *; swap with pointers.
  • Structure vs union memory: print sizeof of a struct and a union with the same members to show the difference.
  • Student I-card: a structure with name, roll number, course and address, printed in an ID-card layout.
  • Employee file: write employee records with fprintf to a file and read them back with fscanf.
cFILE *fp = fopen("emp.txt", "w");
fprintf(fp, "%d %s %.2f\n", id, name, salary);
fclose(fp);

Key terms

Compiler
Software that translates C source code into machine code
Bitwise operator
Operator working on individual bits
Macro
Name replaced by text before compilation
Pointer
Variable holding an address
Practical file
Record of aim, program and output for each experiment

Quick revision

  • %d on a char prints its ASCII value; sizeof gives sizes.
  • Bitwise &, |, ^, ~, <<, >>; conditional operator.
  • Always add break in switch cases; test primes up to √n.
  • Recursion needs a base case; macros need brackets.
  • Strings end with '\0'; always fclose() files.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.What is the output of 12 & 10?
  2. Q2.Distinguish local and global variables.
  3. Q3.Why should macro arguments be parenthesised?
  4. Q4.Write the recursive formula for factorial.
  5. Q5.How do you append to an existing file?
  6. Q6.What does the * operator do in a declaration and in an expression?

Long-answer questions

  1. Q1.Write a program to demonstrate arithmetic, relational, logical and bitwise operators.
  2. Q2.Write a menu-driven calculator using switch-case.
  3. Q3.Write recursive programs for factorial and Fibonacci series.
  4. Q4.Write a program to reverse a string and check whether it is a palindrome.
  5. Q5.Write a program to create a file, write records into it and read them back.

Stuck on this unit?

Message SBS on WhatsApp for help with Software Lab-I (Programming in C), or to ask about studying B.Sc IT at Synetic.

WhatsApp us