1) What is C programming and why is it called a middle-level language?
C is a general-purpose, procedural programming language developed by Dennis Ritchie in the 1970s. It is
called a “middle-level” language because it combines the features of both low-level and high-level
programming languages.
C allows manipulation of hardware and memory directly (low-level), but it also provides high-level
abstractions such as functions and structures. This makes it suitable for system programming (like
operating systems) and application programming, offering both flexibility and control over the
hardware.
2) What are the basic data types in C? Provide examples of each.
The basic data types in C are:
int: Used to store integers. Example: int a = 5;
char: Used to store single characters. Example: char b = ‘A’;
float: Used to store floating-point numbers. Example: float c = 3.14;
double: Used for double-precision floating-point numbers. Example: double d = 3.141592;
void: Represents no value or no type. Example: void func() {}
Each type has a specific size and range depending on the system architecture.
3) Write a program to print “Hello, World!” without using ; .
Answer: Use an if or switch statement:
#include <stdio.h>
int main() {
if (printf("Hello, World!")) {}
return 0;
}
4) What is the difference between #include <stdio.h> and #include “file.h”?
#include <stdio.h>: This directive is used to include standard library header files. The compiler looks for
stdio.h in the system’s predefined library directories.
#include “file.h”: This directive is used to include user-defined header files. The compiler first looks for
file.h in the current directory and, if not found, checks the system directories.
In short, < > is used for standard headers, and ” ” is used for custom headers.
5) What is the purpose of a main() function in C?
The main() function is the entry point of any C program. It is where execution begins. Every C program
must have a main() function, which returns an integer value (typically 0 to indicate successful
execution). The arguments int argc, char *argv[] can be passed to main() to handle command-line
inputs.
6) Explain the difference between printf() and scanf().
printf(): A function used to output formatted text to the standard output (usually the console). It allows
formatting through format specifiers like %d for integers, %f for floating-point numbers, etc.
Example: printf(“The value is: %d”, x);
scanf(): A function used to read input from the standard input (keyboard). It takes input from the user
and stores it in specified variables.
Example: scanf(“%d”, &x);
In short, printf() is used for output, and scanf() is used for input.
7) What are keywords in C? List five examples and their uses.
Keywords in C are reserved words that have special meanings and cannot be used as identifiers (variable
names, function names, etc.). Examples include:
int: Defines an integer data type.
return: Terminates a function and optionally returns a value.
if: Used for conditional statements.
while: Defines a loop that executes as long as a condition is true.
struct: Defines a structure to group different data types.
These keywords are part of the syntax of the language and enable structured programming.
8) What is the purpose of the return statement in C functions?
The return statement is used to exit a function and optionally pass a value back to the calling function. In
non-void functions, it specifies the function’s return value. In void functions, return is used without a
value to exit the function prematurely.
Example: return 0; in int main() indicates successful execution.
9) Explain the use of comments in C. How many types of comments are there?
Comments are used to add explanatory notes to the code. They are ignored by the compiler and help
make the code more readable for developers. There are two types of comments in C:
Single-line comments: Denoted by //. They comment out a single line.
Example: // This is a comment
Multi-line comments: Denoted by /* */. They can span multiple lines.
Example: /* This is a multi-line comment */
10) What is a variable in C? How do you declare and initialize it?
A variable in C is a named storage location in memory that holds a value of a specified data type. To
declare a variable, you specify its type followed by its name, and optionally, its initial value.
Declaration: int x; declares an integer variable x.
Initialization: int x = 5; initializes the variable x with a value of 5.
11) What is the difference between a compiler and an interpreter? Why is C a compiled language?
Compiler: A program that translates the entire source code of a program into machine code (or
intermediate code) before execution. The output is an executable file.
Interpreter: A program that reads and executes the source code line by line.
C is a compiled language because it uses a compiler to translate the C source code into machine code
before execution, resulting in faster execution. The compiled code is platform-dependent.