[go: up one dir, main page]

0% found this document useful (0 votes)
11 views10 pages

pcl pyq unit-1

C is a general-purpose programming language developed in the early 1970s, known for its efficiency, modularity, and low-level access to hardware. It has limitations such as complexity, manual memory management, and lack of built-in security features. The document also covers various types of errors, data types, operators, type conversion, and key programming concepts like identifiers and constants.

Uploaded by

Mansi Panwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

pcl pyq unit-1

C is a general-purpose programming language developed in the early 1970s, known for its efficiency, modularity, and low-level access to hardware. It has limitations such as complexity, manual memory management, and lack of built-in security features. The document also covers various types of errors, data types, operators, type conversion, and key programming concepts like identifiers and constants.

Uploaded by

Mansi Panwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT-1(INTRODUCTION)

1Q. What is C? Briefly explain its history along with its features. What are its limitations?

Ans. C is a general-purpose programming language renowned for its efficiency and


flexibility. It was developed by Dennis Ritchie at Bell Labs in the early 1970s as an evolution
of the B programming language. C was designed to be a system programming language,
allowing developers to interact directly with hardware and create efficient, low-level code.

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.

2Q. What are various types of errors?

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.

3Q. Explain various data types in detail.

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:

1. Basic Data Types:

• 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.

4Q. Explain various operators in detail. What is the hierarchy/precedence of operators?

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:

1. Implicit Conversion (Promotion):


o In implicit conversion, the compiler automatically converts a value from a smaller data
type to a larger data type during an expression or assignment. This ensures
1

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)

2. Explicit Conversion (Casting):


o In explicit conversion, you, the programmer, instruct the compiler to convert a value
from one type to another using the cast operator (type). This allows for more control
over data conversions but can potentially lead to data loss if the target type is smaller
than the source type.
Example:
C
double d = 3.14159;
int i = (int)d; // Explicit conversion from double (8 bytes) to int
(4 bytes)
printf("%d\n", i); // Output: 3 (decimal part truncated, potential
data loss)
6Q. Differentiation ➢ printf() vs scanf()
➢= and ==
➢ Identifier Vs Variable
➢ Pre Vs post increment
➢ Pre Vs post decrement
➢ Getch() Vs getche()
Ans.(a)

Feature printf() scanf()

Purpose Prints formatted output to the Reads formatted input from the
console console

Syntax printf("format string", variable scanf("format string", &variable


list); list);

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

Argument Variables are passed directly Addresses of variables are


Passing passed using the & operator

Return Value Returns the number of Returns the number of items


characters printed successfully read

Header File stdio.h stdio.h

Example printf("Hello, world!\n"); scanf("%d", &num);

(b)

Operator Meaning Usage

= Assignment Assigns a value to a variable.


Operator

== Equality Operator Compares two values for equality. Returns 1 if


equal, 0 otherwise.
Example:

int x = 10; // Assigns the value 10 to the variable x


if (x == 10) { // Compares if the value of x is equal to 10
printf("x is equal to 10\n");
}

(c)

Feature Identifier Variable

Definition A name given to an entity in a A name given to a memory


program location to store a value

Scope Can be used to name various entities Specifically used to name


like variables, functions, structures, memory locations
etc.

Value Doesn't hold any value Holds a value of a specific


data type

Relationship All variables are identifiers, but not all Variables are a type of
identifiers are variables identifier

(d)

Feature Pre-increment (++) Post-increment (++)

Order of Increment the value first, then use Use the current value in the
Operation it in the expression. expression, then increment
it.

Example int x = 5; y = ++x; (y = 6, x = 6) int x = 5; y = x++; (y = 5, x =


6)

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)

Feature Pre-Decrement (--x) Post-Decrement (x--)

Timing of Decrements the value of x Decrements the value of x after


Decrement before using it in the using it in the expression.
expression.

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)

Feature getch() getche()

Header file conio.h conio.h

Character No Yes
Echoing

Buffering No No

Return Value Character read Character read

Portability Non-standard, limited to Non-standard, limited to older


older compilers compilers

Use Cases Password masking, Simple character input without


character-based games waiting for Enter key
7Q. Explain ➢ Break statement ➢ Goto statement ➢ Continue statement➢ Enumeration
➢ Ternary/Conditional operator
Ans. 1. Ternary/Conditional Operator

The ternary operator is a shorthand way to write an if-else statement in a single line. Its
syntax is:

condition ? expression1 : expression2

If the condition is true, expression1 is evaluated. Otherwise, expression2 is evaluated.

Example: 1

int x = 10, y = 20;


int max = (x > y) ? x : y; // max will be 20

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:

for (int i = 1; i <= 10; i++) {


if (i == 5) {
break; // Exit the loop when i becomes 5
}
printf("%d ", i);
}

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:

for (int i = 1; i <= 10; i++) {


if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}

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:

enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,


Sunday };

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:

• These are reserved words with predefined meanings in the C language.


• They cannot be used as variable names or identifiers.
• Examples include: int, float, char, if, else, for, while, do, break, continue,
etc.

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:

• These are punctuation marks used to structure the C program.


• Examples: ( ), [ ], { }, ;, ,, #.

6. Strings:

• These are sequences of characters enclosed in double quotes.


• They are used to represent text.

You might also like