0% found this document useful (0 votes)
25 views6 pages

PF Thro

j

Uploaded by

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

PF Thro

j

Uploaded by

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

 To: KIANAT KHALID

 Student Name: SYED NADEEM UL HASSAN


 Student ID:Sp24-BCs-192
 Date:20 / 04 / 2025

BEST OF LUCK…..!
Assignment No 1:
Assignment: C Programming Basics to Conditional Statements

Write all answers in your own words.


Part 1: Theory & Concepts
Write short notes (2–4 lines) on the following concepts:

· Structure of a C program

A C program consists of three main parts:

Preprocessor Directives – These start with # and are used to include libraries or define constants before
compilation (e.g., #include <stdio.h>)

Main Function – The main() function is the starting point of program execution (e.g., int main())

Body of the Main Function – Enclosed in curly braces {}, this section contains the actual code like variable
declarations, input/output functions, and logic (e.g., { printf("Hello, World!"); return 0; }).

2. #include and main() Function


#include is used to include header files like stdio.h. The main() function is the entry point of the program where
execution begins.

3. Keywords and Identifiers


Keywords are reserved words in C with predefined meanings (e.g., int, return). Identifiers are user-defined
names for variables, functions, etc.

4. Data Types in C (int, float, char, etc.)


Data types define the type of data stored. Common types are int for integers, float for decimals, char for
characters, and double for large decimals.

5. Variables and Constants


Variables store changeable values during program execution. Constants hold fixed values, declared using the
const keyword.

6. Input/Output Functions (scanf, printf)


scanf() reads input from the user. printf() displays output to the console. Both are defined in the stdio.h library.

7. Arithmetic Operators
Used for mathematical operations: + (add), - (subtract), * (multiply), / (divide), % (modulus).
8. Assignment and Compound Assignment Operators
The assignment operator = assigns values. Compound operators (+=, -=, *=, /=, %=) perform an operation and
assign the result.

9. Relational Operators
Used to compare values: <, >, <=, >=, ==, !=. They return true (1) or false (0) in conditions.

10. Logical Operators


Used for combining conditions: && (AND), || (OR), ! (NOT). They return true or false based on logical
evaluation.

11. Conditional Statements (if, if-else, else-if, nested if, switch)


Used to control program flow based on conditions. if, if-else, and else-if handle decision-making. nested if
allows multiple layers, while switch checks one variable against multiple constants.

12. Difference Between else-if and switch


else-if can evaluate complex and range-based conditions. switch is used for checking a variable against
specific constant values, improving readability for multiple options.

Precedence and Associativity of Operators

Category Operators Precedence Associativity


Parentheses () Highest Left to Right
Unary +, -, ++, --, ! High Right to Left
Multiplicative *, /, % Medium Left to Right
Additive +, - Medium Left to Right
Relational <, >, <=, >= Low Left to Right
Equality ==, != Lower Left to Right
Logical AND && Very Low Left to Right
Logical OR ` `

Part 2: Technical Questions


1. What is the output of printf("%d", 7 / 2); and why?
The output is 3. In C, both 7 and 2 are integers, so integer division is performed. This discards the decimal
part and returns only the whole number.

2. Can we use % with floating-point numbers in C?


No, the modulus operator (%) is used exclusively with integer data types in C. Applying % to floating-point
numbers like 5.5 % 2.2 will result in a compilation error. For floating-point numbers, alternatives such as
fmod (from <math.h>) can be used to find the remainder.

3. How is a character stored in memory?


A character is stored as its ASCII value, which is a unique integer code. For example, the character 'A' is
stored as 65 in memory, while 'a' is stored as 97. A single character occupies 1 byte (8 bits) in memory.

4. Explain the use of && and || with examples.


Logical AND (&&) returns true if both conditions are true; otherwise, it returns false. For example, if a = 5
and b = 10, the expression a > 0 && b < 20 evaluates to true because both conditions are satisfied.

Logical OR (||) returns true if at least one condition is true; otherwise, it returns false. For example, if a = 5
and b = 10, the expression a > 0 || b > 20 evaluates to true because the first condition is true.
Short-Circuit Evaluation means:
&& stops if the first condition is false.
|| stops if the first condition is true.

5. What is the result of 5 < 3 == 0?


The result is 1 (true).
5 < 3 evaluates to 0 (false).
0 == 0 evaluates to 1 (true).
In C, relational operators are evaluated from left to right, ensuring correctness.

6. Write the truth table for logical AND, OR, and NOT.
A B A && B A || B !A

0 0 0 0 1

0 1 0 1 1

1 0 0 1 0

1 1 1 1 0

7. What is the difference between if-else and switch statements?


Key Differences Between If-Else and Switch Statements

1. Evaluation of Conditions:
If-Else: Handles complex logical expressions and ranges (e.g., a > 10 && b < 20).
Switch: Works with discrete, constant values (e.g., case 1, case 'A').

2. Execution Flow:
If-Else: Evaluates each condition sequentially until one is true.
Switch: Jumps directly to the matching case, allowing for faster execution.

3. Default Handling:
If-Else: Requires an explicit fallback condition (e.g., else).
Switch: Has a built-in default case for unmatched conditions.

4. Readability:
If-Else: Can become less readable, especially with multiple nested conditions.
Switch: Provides a cleaner structure for handling many fixed values.

5. Efficiency:
If-Else: Can be slower when evaluating multiple conditions sequentially.
Switch: More efficient for large sets of constant values as it directly jumps to the matching case.
8. What is the role of break and default in a switch-case structure?
Role of break: The break statement terminates the execution of a case and exits the switch block. Without
break, the program continues to execute subsequent cases, known as "fall-through."
Example:

int a = 2;
switch (a) {
case 1: printf("One."); break;
case 2: printf("Two."); break;
default: printf("Other.");
}

Explanation: The program executes case 2 and prints "Two." because a is 2. After the break, it exits the
switch block.

Role of default: The default statement provides a fallback option when none of the cases match the value.
It is optional but useful for handling unmatched inputs.
Example:

int a = 3;
switch (a) {
case 1: printf("One."); break;
case 2: printf("Two."); break;
default: printf("Other.");
}

Explanation: Since a is 3, the default block executes and prints "Other.".

9. Explain implicit and explicit type conversion with examples.


1. Implicit Type Conversion (Type Promotion)

When the compiler automatically converts a smaller or lower-ranked data type


to a larger or higher-ranked data type, without any intervention from the programmer,
it is known as implicit type conversion. For example:

int x = 10;
float y = x; // Integer 'x' is automatically converted to float.

This ensures compatibility and prevents data loss during operations.

2. Explicit Type Conversion (Type Casting)

In explicit type conversion, the programmer deliberately converts one data type
to another using casting. This method is manual and allows for conversions even when
there is a possibility of data loss. For example:

float x = 5.75;
int y = (int)x; // Float 'x' is explicitly cast to integer, truncating decimals.
Explicit type conversion provides control, but programmers must ensure its correctness.

10. What will be the output of the following code?


int a = 5;
if (a = 0)
printf("Hello");
else
printf("World");

Output: World

a=0 is an assignment, not a comparison. It sets a to 0 (false), so the else block runs.

THE END…

You might also like