pcl pyq unit-1
pcl pyq unit-1
1Q. What is C? Briefly explain its history along with its features. What are its limitations?
Key Features of C:
• Efficiency: C code compiles into highly efficient machine code, making it ideal for
performance-critical applications.
• Modularity: C supports modular programming, allowing code to be broken down
into reusable functions and modules.
• Low-Level Access: C provides direct access to memory and hardware, enabling fine-
grained control over system resources.
• Rich Standard Library: C offers a comprehensive standard library with functions
for input/output, string manipulation, mathematical operations, and more.
• Portability: C code can be compiled and run on various platforms with minimal
modifications, making it highly portable.
Limitations of C:
• Complexity: C's low-level capabilities can make it more complex to learn and use
effectively.
• Memory Management: C requires manual memory management, which can lead to
memory leaks and other errors if not handled carefully.
• Lack of Built-in Security Features: C doesn't have built-in security features, making
it susceptible to vulnerabilities like buffer overflows and memory corruption.
• Verbosity: C code can be verbose, requiring more lines of code to achieve certain
tasks compared to higher-level languages.
Ans. C programming, like any other language, is prone to errors. These errors can lead to
unexpected program behavior or even crashes. Here are the primary types of errors you might
encounter:
1. Syntax Errors:
o These occur when you violate the grammar rules of the C language.
o Examples: missing semicolons, mismatched parentheses, incorrect keywords.
o The compiler usually identifies these errors and provides error messages.
2. Runtime Errors:
o These errors manifest during the execution of the program.
o Examples: division by zero, accessing invalid memory locations, out-of-
bounds array indices.
o The program may crash or produce incorrect results.
3. Logical Errors:
o These occur when the program's logic is flawed, leading to incorrect output.
o The code may compile and run without errors, but the results are not as
expected.
oExamples: incorrect calculations, incorrect conditional statements, infinite
loops.
4. Semantic Errors:
o These errors arise from a misunderstanding of the programming language or
the problem domain.
o The code may be syntactically correct, but it doesn't do what you intended.
o Examples: using incorrect data types, incorrect function calls, incorrect
variable assignments.
5. Linker Errors:
o These occur when the linker fails to resolve references to external functions or
variables.
o This might happen due to missing libraries, incorrect library paths, or
undefined symbols.
Ans. n C programming, data types define the type of values a variable can hold and the
operations that can be performed on those values. Understanding data types is crucial for
writing efficient and accurate C programs. Here's a breakdown of various data types in C:
• int: This is the most common integer data type. The size of an int can vary depending
on the system (typically 2 or 4 bytes) but is guaranteed to be at least 2 bytes. It can
store whole numbers, both positive and negative.
o Range: [INT_MIN, INT_MAX] (implementation-dependent)
o Format Specifier: %d or %i
• char: This data type stores a single character. In C, characters are treated as small
integers (usually 1 byte) according to the ASCII or Unicode character set.
o Range: [CHAR_MIN, CHAR_MAX] (usually -128 to 127)
o Format Specifier: %c
• float: This data type represents single-precision floating-point numbers (numbers
with decimal points). It typically uses 4 bytes and offers a balance between precision
and storage efficiency.
o Range: (implementation-dependent) but generally not suitable for very
precise calculations.
o Format Specifier: %f
• double: This data type represents double-precision floating-point numbers, offering
greater precision than float (typically 8 bytes). Use double for calculations requiring
higher accuracy.
o Range: (implementation-dependent) but generally more precise than float.
o Format Specifier: %lf
2. Modifier Types:
• signed: This is the default for integer data types (int, char, short, long). It allows
storing both positive and negative numbers.
• unsigned: This modifier specifies that an integer data type can only store non-
negative values (0 and positive numbers). It allows for a slightly larger range
compared to signed integers of the same size.
• short: This modifier reduces the size of an integer data type (usually to half its size).
It's useful when memory usage is a concern, but be aware of the reduced range.
• long: This modifier increases the size of an integer data type (usually to double its
size). It allows for a larger range of values compared to the base type.
3. Floating-Point Types:
• long double: This data type provides extended precision for floating-point numbers
(typically 10 or more bytes). Use it when very high precision is required, but be aware
of the increased storage space needed.
4. void:
• void: This data type indicates the absence of a specific value. It's often used for
functions that don't return any value or function arguments that don't hold any data.
Ans. Operators are symbols that perform specific operations on variables and values. C
provides a rich set of operators to manipulate data.
Types of Operators in C:
1. Arithmetic Operators:
o +: Addition
o -: Subtraction
o *: Multiplication
o /: Division
o %: Modulus (remainder after division)
2. Relational Operators:
o ==: Equal to
o !=: Not equal to
o >: Greater than
o <: Less than
o >=: Greater than or equal to
o <=: Less than or equal to
3. Logical Operators:
o &&: Logical AND
o ||: Logical OR
o !: Logical NOT
4. Bitwise Operators:
o &: Bitwise AND
o |: Bitwise OR
o ^: Bitwise XOR
o ~: Bitwise NOT
o <<: Left shift
o >>: Right shift
5. Assignment Operators:
o =: Simple assignment
o +=: Add and assign
o -=: Subtract and assign
o *=: Multiply and assign
o /=: Divide and assign
o %=: Modulus and assign
o <<=: Left shift and assign
o >>=: Right shift and assign
o &=: Bitwise AND and assign
o |=: Bitwise OR and assign
o ^=: Bitwise XOR and assign
6. Increment and Decrement Operators:
o ++: Increment
o --: Decrement
7. Conditional Operator:
o ?:: Ternary operator (condition ? expression1 : expression2)
8. Comma Operator:
o ,: Used to separate expressions
5Q. What is Type conversion? What are its types? Explain with the help of example.
Ans. Type conversion, also known as casting, refers to the process of transforming a value
from one data type to another in C. This allows you to manipulate data of different types
within expressions and assignments. There are two main types of type conversion:
compatibility for calculations and avoids data loss (within certain limits). 2
o Example:
C
int x = 10;
float y = x; // Implicit conversion from int (4 bytes) to float
(4 bytes)
printf("%f\n", y); // Output: 10.000000 (no data loss)
Purpose Prints formatted output to the Reads formatted input from the
console console
Format Used to specify the data type of Used to specify the data type of
Specifiers the values to be printed the values to be read
(b)
(c)
Relationship All variables are identifiers, but not all Variables are a type of
identifiers are variables identifier
(d)
Order of Increment the value first, then use Use the current value in the
Operation it in the expression. expression, then increment
it.
Use Case When you want to use the When you want to use the
incremented value immediately in original value first and then
the same expression. increment it.
(e)
Example int x = 5; <br> int y = --x; int x = 5; <br> int y = x--; <br> // y
<br> // x becomes 4, y becomes 5, x becomes 4
becomes 4
Use Cases Often used when you want Useful when you want to use the
to immediately use the original value of x in the current
decremented value. expression and then decrement it
for future use.
(f)
Character No Yes
Echoing
Buffering No No
The ternary operator is a shorthand way to write an if-else statement in a single line. Its
syntax is:
Example: 1
2. Break Statement
The break statement is used to immediately exit a loop (for, while, or do-while) or a switch
statement. It's often used to terminate a loop prematurely based on a certain condition.
Example:
3. Goto Statement
The goto statement is used to transfer control to a specific labeled statement within a
function. However, it's generally discouraged as it can make code less readable and harder
to maintain. Excessive use of goto can lead to spaghetti code.
Example:
#include <stdio.h>
int main() {
int i = 1;
loop:
printf("%d ", i);
i++;
if (i <= 10) {
goto loop;
}
return 0;
}
4. Continue Statement
The continue statement is used to skip the current iteration of a loop and move to the next
iteration. It's often used to skip certain iterations based on a condition.
2
Example:
5. Enumeration
An enumeration (enum) is a user-defined data type that consists of a set of named integer
constants. It's useful for defining a set of related values and making code more readable.
Example:
int main() {
enum Days today = Wednesday;
printf("Today is %d", today); // Output: 2 (since Wednesday is
the 3rd constant)
return 0;
}
8Q.Explain Identifier
➢ Keyword
➢ Tokens
➢ Constant (and its
types)
Ans. In C programming, tokens are the smallest individual units that make up a program.
Think of them as the building blocks of your code. There are six main types of tokens in C:
1. Keywords:
2. Identifiers:
• These are user-defined names given to variables, functions, arrays, and other program
elements.
• They must start with a letter (uppercase or lowercase) or an underscore (_).
• Subsequent characters can be letters, digits, or underscores.
• They cannot be keywords.
• Examples: age, name, sum, myFunction.
3. Constants:
• These are fixed values that cannot be changed during program execution.
• They are classified into several types:
o Integer Constants: Represent whole numbers.
§ Decimal: 10, -25
§ Octal (base 8): 012, 077
§ Hexadecimal (base 16): 0xAF, 0x10
o Floating-Point Constants: Represent real numbers with decimal points.
§ Single precision: 3.14f
§ Double precision: 3.14159
o Character Constants: Represent single characters enclosed in single quotes.
§ 'a', 'Z', '9', '$'
o String Literals: Represent sequences of characters enclosed in double quotes.
§ "Hello, world!", "C programming"
4. Operators:
• These are symbols that perform specific operations on variables and constants.
• Examples: +, -, *, /, %, ++, --, ==, !=, <, >, <=, >=, &&, ||, !, =.
5. Special Symbols:
6. Strings: