Computer Studies - C Programming Notes
Section B - Short Answer Questions
1. Pointer in C - Definition & Initialization
Definition: A pointer is a variable that stores the memory address of another variable.
Example:
int x = 10;
int *ptr = &x;
printf("%d", *ptr); // Output: 10
2. Format Specifiers - %d, %f, %c, %s
Definition: Format specifiers are used in printf() and scanf() to indicate data types.
Examples:
%d - Integer
%f - Float
%c - Character
%s - String
int a = 5;
printf("Value is %d", a);
3. Valid/Invalid Identifiers
Definition: An identifier is the name of a variable, function, or array.
Valid: num1, _value, totalSum
Invalid: 1value, float, total-amount
4. Math Functions - sqrt(), pow(), abs()
Definition: Used to perform mathematical operations. Header: #include <math.h>
Examples:
sqrt(25); // 5
pow(2,3); // 8
abs(-7); // 7
5. Expression Conversion - Infix to Postfix
Definition: Converting expressions from infix (A + B) to postfix (A B +).
Example:
Infix: A + B * C
Postfix: A B C * +
6. Simple C Programs - Largest Number, Loop
Largest:
int a=10, b=20;
if(a > b) printf("A is largest"); else printf("B is largest");
Loop:
for(int i=1; i<=5; i++) printf("%d ", i);
7. String and Reserved Words
String: Character array for text.
Reserved Words: Predefined C keywords like int, return.
char name[] = "Ali";
8. Structure of C Program
#include <stdio.h>
int main() {
// Body
return 0;
9. If-Else and Switch - Syntax & Example
If-Else:
int x=5;
if(x>0) printf("Positive"); else printf("Negative");
Switch:
int day=2;
switch(day){ case 1: printf("Mon"); break; case 2: printf("Tue"); break; default: printf("Invalid"); }
10. Break & Continue - Usage in Loops
Break exits loop, Continue skips current iteration.
for(int i=1;i<=5;i++) { if(i==3) continue; printf("%d ", i); }
11. Identifiers - Local & Global
Local: Declared in function.
Global: Declared outside, accessible everywhere.
int a=10; // global
void test() { int b=5; // local }
Section C - Long Answer Questions
1. Data Types in C - With Examples
Data types define what kind of data a variable can store.
Types:
int (e.g., 10)
float (e.g., 3.14)
char (e.g., 'A')
double (e.g., 3.14159)
Example:
int age = 20;
float salary = 12000.50;
char grade = 'A';
double pi = 3.141592;
2. Switch Statement with Syntax
Used to select one of many code blocks based on a value.
Syntax:
switch(expression){ case val1: break; default: }
Example:
int ch = 1;
switch(ch){ case 1: printf("One"); break; default: printf("Invalid"); }
3. Functions in C - Declaration, Prototype, Example
Function is a block of code for a specific task.
Declaration: int sum(int, int);
Definition: int sum(int a, int b){ return a + b; }
Calling: int result = sum(3, 5);
Example with main and add function included.
4. Loops - for, while, do-while with Examples
Used to repeat code multiple times.
for: for(int i=1; i<=5; i++)
while: while(i<=5)
do-while: do{...} while(i<=5);
5. Basic Operators in C - Arithmetic, Relational, Logical
Operators are symbols for operations.
Arithmetic: +, -, *, /, %
Relational: >, <, ==, !=
Logical: &&, ||, !
Example: if(a > b && b != 0) printf("Valid");
6. Define Variable and Constant
Variable: A memory location that can change.
int age = 20;
Constant: A fixed value.
const float PI = 3.14;
7. Difference Between Source Code and Object Code
Source Code: Human-readable (e.g., main.c)
Object Code: Machine-readable, compiled (e.g., main.o)
8. Define Comments and Give Example
Comments explain code.
Single-line: // comment
Multi-line: /* comment */
9. What Are Escape Sequences - With Examples
Special characters starting with '\'.
\n = New Line, \t = Tab, \\ = Backslash, \" = Double Quote
Example: printf("Hello\nWorld");