UNIT 1 – C Programming Fundamentals
Short Answer Questions
1. What are the main features of the C programming language?
Answer:
C is a powerful, general-purpose programming language developed by Dennis Ritchie in
1972. Its main features are:
1. Simplicity – Easy syntax and flexible structure.
2. Portability – Programs can run on any machine with little or no modification.
3. Efficiency – Produces fast and optimized machine code.
4. Rich Library – Contains many built-in functions.
5. Structured Language – Programs can be divided into modules or functions.
6. Low-level Access – Can directly access memory using pointers.
7. Extensibility – New features can be easily added by defining functions.
2. Define a compiler and explain its role in C programming.
Answer:
A compiler is a special program that converts the entire C source code into machine
language (object code) before execution.
It checks for syntax errors and produces an executable file.
Stages of compilation:
1. Preprocessing
2. Compilation
3. Assembly
4. Linking
5. Loading & Execution
3. What are the basic data types in C?
Answer:
Data types define the kind of data a variable can hold.
Data Type Description Size (in bytes) Example
int Integer numbers 2 or 4 int age = 20;
float Real (decimal) numbers 4 float rate = 12.5;
double Double-precision float 8 double pi = 3.14159;
char Single character 1 char grade = 'A';
void No value – Used in functions without return
4. Explain the difference between variable declaration and definition.
Term Meaning Example
Tells the compiler the variable name and type (reserves no extern int
Declaration
memory). a;
Definition Actually allocates memory and may assign value. int a = 5;
5. What is the purpose of the printf() and scanf() functions?
Answer:
Both are standard I/O functions defined in <stdio.h>.
• printf() – Displays (outputs) data on the screen.
→ Example: printf("Sum = %d", sum);
• scanf() – Reads (inputs) data from the user.
→ Example: scanf("%d", &num);
6. What are constants and literals in C? Give examples.
Answer:
Constants/Literals are fixed values that do not change during program execution.
Type Example
Integer Constant 10, -25
Floating Constant 3.14, -0.5
Type Example
Character Constant 'A', '9'
String Constant "Hello"
Declared using const keyword:
const float PI = 3.14;
7. Define identifiers. What are the rules for naming identifiers in C?
Answer:
An identifier is the name given to variables, functions, arrays, etc.
Rules:
1. Must begin with a letter or underscore.
2. Can contain letters, digits, and underscores only.
3. No spaces or special characters allowed.
4. Case-sensitive (sum ≠ Sum).
5. Cannot be a keyword.
Example: total_marks, _count, x1
8. Explain the concept of type casting in C.
Answer:
Type casting means converting one data type into another.
• Implicit Casting: Done automatically by compiler.
Example: int a=10; float b=a;
• Explicit Casting: Done manually by programmer.
Example:
• float avg = (float)sum / count;
9. What is the difference between = and == operators?
Operator Meaning Example Explanation
= Assignment a = 5; Assigns 5 to a.
== Comparison if(a == 5) Checks if a equals 5.
10. What are escape sequences? Give two examples.
Answer:
Escape sequences are special characters used inside strings to format output.
Sequence Meaning
\n New line
\t Tab space
\\ Backslash
\" Double quote
Example:
printf("Hello\nWorld\tC");
Long Answer Questions
1. Describe the structure of a C program with an example.
Structure:
1. Documentation Section – Comments explaining program purpose.
2. Link Section – Header files (#include <stdio.h>).
3. Global Declaration Section – Global variables and functions.
4. Main Function – Entry point of program.
5. User-Defined Functions – Defined after main() if needed.
Example:
#include <stdio.h> // Link Section
int main() // Main Function
int a,b,sum; // Declarations
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
2. Explain different types of operators in C with examples.
Type Operators Example Use
Arithmetic +-*/% a+b Mathematical ops
Relational < > <= >= == != a>b Comparison
Logical `&& !`
Assignment = += -= *= /= a+=2 Assign values
Increment/Decrement ++ -- a++ Increase/decrease
Bitwise `& ^ ~ << >>` a&b
Conditional ?: (a>b)?a:b Short if-else
Special sizeof, &, * sizeof(int) Miscellaneous
3. Discuss various data types in C and their sizes.
Category Data Type Size Range (approx.)
Integer int, short, long, unsigned int 2–4 bytes −32,768 to 32,767
Float float, double, long double 4–12 bytes ±3.4 × 10⁻³⁸ to 3.4 × 10³⁸
Character char, unsigned char 1 byte −128 to 127
Void void – No data type
4. Write and explain the steps of the program development cycle in C.
Steps:
1. Problem Definition – Understand the problem clearly.
2. Algorithm Design – Step-by-step logical solution.
3. Flowchart – Graphical representation of logic.
4. Coding – Writing C code.
5. Compilation – Converting code to object file.
6. Execution & Testing – Running and debugging.
7. Documentation & Maintenance – Finalizing the program.
5. Explain the process of compiling, linking, and executing a C program.
1. Compilation:
The compiler translates the .c source code into object code (.obj or .o) and reports
syntax errors.
2. Linking:
The linker combines object code with library functions to create an executable file
(.exe).
3. Execution:
The loader loads the executable into memory and the CPU executes the instructions
sequentially.
6. Compare and contrast global and local variables.
Basis Local Variable Global Variable
Declaration Inside a function Outside all functions
Scope Visible only within the function Accessible by all functions
Lifetime Created & destroyed when function runs Exists till program ends
Example int main(){ int a=5; } int a=5; int main(){...}
7. Explain formatted and unformatted input/output functions in C.
Category Functions Description
Formatted I/O printf(), scanf() Use format specifiers for structured I/O.
Unformatted getchar(), putchar(), Deal directly with characters or strings
I/O gets(), puts() without format control.
Example:
char ch;
ch = getchar();
putchar(ch);
8. Write a program to swap two numbers using a third variable and without using a third
variable.
(a) Using a third variable:
#include <stdio.h>
int main() {
int a=5, b=10, temp;
temp = a;
a = b;
b = temp;
printf("a=%d b=%d", a, b);
return 0;
}
(b) Without using a third variable:
#include <stdio.h>
int main() {
int a=5, b=10;
a = a + b;
b = a - b;
a = a - b;
printf("a=%d b=%d", a, b);
return 0;