0% found this document useful (0 votes)
17 views18 pages

Answers

The document covers various fundamental concepts in C programming, including operator precedence and associativity, console I/O functions, and the structure of C programs. It explains the differences between compilers and interpreters, outlines basic data types, and discusses error types in programming. Additionally, it provides insights into algorithms, flowcharts, and storage class specifiers, emphasizing their roles in variable scope and lifetime.
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)
17 views18 pages

Answers

The document covers various fundamental concepts in C programming, including operator precedence and associativity, console I/O functions, and the structure of C programs. It explains the differences between compilers and interpreters, outlines basic data types, and discusses error types in programming. Additionally, it provides insights into algorithms, flowcharts, and storage class specifiers, emphasizing their roles in variable scope and lifetime.
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

1.

Demonstrate how operators and expressions are evaluated in C with examples for
precedence and associativity

Precedence = Which operator is evaluated first

Associativity = If multiple operators have same precedence, which direction is used


(Left-to-Right or Right-to-Left)
2. Name any four C function used to perform console I/O.

Console I/O Functions in C:

➢ Input Functions:

❖ scanf() → Reads formatted input from keyboard.


❖ getchar() → Reads a single character.

➢ Output Functions:

❖ printf()→ Prints formatted output to screen.


❖ putchar()→ Prints a single character.
3. Design a pseudo code and its equivalent C program to calculate the area and perimeter of
a rectangle.

• Pseudocode is a compact and informal high-level description of an algorithm that uses


the structural conventions of a programming language.
• Pseudo code to calculate the area and perimeter of a rectangle.

• C program to calculate the area and perimeter of a rectangle.


4. Write a note on Compiler.

Compiler:
A compiler is a system software that translates a high-level programming language (like
C, C++, Java) into machine code (binary) that the computer's hardware can understand
and execute.

➢ Translates the entire program into machine code at once.


➢ Produces an executable file.
➢ Faster execution after compilation.

5. Write a note on Flowchart?


• A flowchart is a graphical representation of an algorithm, process or a system.
• It is basically used to design and document virtually complex processes to help the
viewers to visualize the logic of the process.
• When designing a flowchart, each step in the process is depicted by a different symbol
and is associated with a short description.
• The symbols in the flowchart are linked together with the arrows to show the flow of
logic in the process.
• Uses symbols to represent steps.
1. Oval → Start/End
2. Parallelogram → Input/Output
3. Rectangle → Statements/Expressions
4. Diamond → Decision (Yes/No)
5. Arrow → Flow of control

Example: Flowchart to check whether a number is even or odd:

6. Explain Formatted Console I/O with c snippet.

Formatted Console I/O

▪ The functions printf() and scanf() perform formatted output and input.
▪ They can read and write data in various formats that are under your control.
▪ The printf() function writes data to the console.
▪ The scanf() function reads data from the keyboard.

printf( )

Prototype: int printf(const char *control_string, ...);

▪ It returns:
a) The number of characters written on success.
b) A negative value if an error occurs.
▪ Control String: It control string (also called the format string) tells the function how to
display the output. It consists of two types of items:

a) Ordinary characters: printed exactly as they appear.


b) Format specifiers: begin with a % sign and define how subsequent arguments are
displayed.

There must be exactly the same number of arguments as there are format specifiers. They are
matched in order from left to right.

scanf()

Prototype: int scanf(const char *control_string, …);

▪ It returns:
a) The number of data items successfully assigned a value.
b) A negative value if an error occurs.
▪ Control String: It determines how values are read into the variable pointed to in the
argument list. It consists of three classifications of characters:

a) Format specifiers (%d, %f, etc.)


b) White space characters (space, tabs; used to separate inputs)
c) Non-white space characters : literal characters that must match exactly in the input.
7. List the basic data types in C language.
8. Making use of suitable examples explain all the basic data types available in C.

9. Differentiate between compiler and interpreter.


10. Compare compiler and interpreter with suitable diagrams and examples.
Compiler:
➢ Translates the entire program into machine code at once.
➢ Produces an executable file.
➢ Faster execution after compilation.
➢ Example: C, C++ use compilers.

Interpreter:
➢ Translates and executes line by line.
➢ No separate executable file.
➢ Slower execution but easier debugging.
➢ Example: Python, JavaScript use interpreters.
Feature Compiler Interpreter

Translation Entire program at once Line by line

Output Executable file No executable

Speed Fast execution Slower

Example C, C++ Python, BASIC

11. Utilize suitable diagram to explain structure of c programming in detail. Write a sample
program to Demonstrate the components in the structure of C program.

▪ A C program contains one or more functions.


▪ The statements in a C program are written in a logical sequence to perform a specific task.
▪ Execution of a C program begins at the main() function.
▪ You can choose any name for the functions.
▪ Every program must contain one function that has its name as main().
▪ Where #include refers to the preprocessor statement used for the standard input/output
functions and it starts with the hash(#) symbol.
▪ Main() refers to the function from where the execution of the program begins.
12. Define algorithm, flowchart, and pseudo code with examples.
The three most common program design tools are:
a. Algorithms
b. Flowcharts
c. Pseudocode

Algorithms

➢ An algorithm is a step-by-step procedure to solve a problem.


➢ Must be finite, unambiguous, and correct.
➢ Should take input, process it, and produce output.
➢ Not even a single instruction must be repeated infinitely.

Example: Algorithm to check whether a number is even or odd:

Step 1: Start
Step 2: Declare a variable num
Step 3: Read the value of num
Step 4: If (num % 2 == 0)
then Display "Number is Even"
else Display "Number is Odd"
Step 5: Stop

Flowcharts

➢ A flowchart is a graphical representation of an algorithm, process or a system.


➢ It is basically used to design and document virtually complex processes to help the
viewers to visualize the logic of the process.

Example: Flowchart to check whether a number is even or odd:

Pseudocode

➢ Pseudocode is a compact and informal high-level description of an algorithm that uses


the structural conventions of a programming language.
➢ Pseudocodes are an outline of a program that can be easily converted into
programming statements.

Example: Find the Largest of Two Numbers

BEGIN DECLARE a, b AS INTEGER

PRINT "Enter two numbers: "


READ a, b
IF (a > b) THEN
PRINT "a is larger"
ELSE

PRINT "b is larger"


ENDIF

END
13. Construct an algorithm and flowchart to find the largest of three numbers. Explain its
working

Flowchart (text description)


Start → Input A, B, C → Decision (A > B and A > C?) → Yes → Print A
Else Decision (B > A and B > C?) → Yes → Print B
Else → Print C → Stop

Explanation of Working

1. First, the program reads three numbers.


2. Then it checks which one is greater in pairs:
o It first checks if A is bigger than B.
o If A > B, then A could be the largest, but we must compare it with C.
o If B >= A, then B could be the largest, but we must compare it with C.
3. After the comparisons, the program determines the largest number.
4. Finally, it prints or displays that largest number.

14. Making use of suitable examples explain different types of error.

➢ While writing programs, very often we get errors in our program.


➢ These errors if not removed will either give erroneous output or will not let the compiler
compile the program.
Types of Errors

• Run-time errors
• Compile-time errors
o Syntax error
o Semantic error
• Linker errors
• Logical errors

Run-time errors:

• Run-time Errors occur when the program is being run (executed).


• Such errors occur when the program performs some illegal operation like:
1. Dividing a number by zero.
2. Opening a file that already exists.
3. Lack of free memory space.
4. Finding square or logarithm of negative numbers

• Run-time errors may terminate program execution, so the code must be written in such a
way that it handles all sorts of unexpected errors rather than terminating it unexpectedly.
• This ability to continue operation of a program despite run-time errors is called
robustness.

Compile time errors:

• Compile-time Errors occur at the time of compilation of the program.


• Such errors can be further classified as follows:

Syntax Errors:
Syntax error is generated when rules of C programming language are violated.
For example, if we write int a: then a syntax error will occur since the correct statement should
be int a;.

Semantic Errors:
Semantic errors are those errors which may comply with rules of the programming language but
are not meaningful to the compiler.
For example, if we write a * b = c; it does not seem correct. Rather, if written like c = a * b; it
would have been more meaningful.

Logical errors:

• Logical Errors are errors in the program code that result in unexpected and undesirable
output which is obviously not correct.
• Such errors are not detected by the compiler, and programmers must check their code line
by line or use a debugger to locate and rectify the errors.
• Logical errors occur due to incorrect statements.
• For example, if you meant to perform c = a + b; and by mistake you typed c = a * b; then
though this statement is syntactically correct, it is logically wrong.

Linker errors:

• Linker Errors occur when the linker is not able to find the function definition for a given
prototype.
• For example, if you write clrscr(); but do not include conio.h then a linker error will be
shown.

15. Explain the steps involved in creating, compiling, and executing a C program. Illustrate
with an example.

• The compiler translates the source code into an object code.


• The object code contains the machine instructions for the CPU, and calls to the operating
system API (Application Programming Interface).
• However, even the object file is not an executable file.
• Therefore, in the next step, the object file is processed with another special program
called a linker.
• While there is a different compiler for every individual language, the same linker is used
for object files regardless of the original language in which the new program was written.
• The output of the linker is an executable or runnable file.

Using Comments

• It is a good programming practice to place some comments in the code to help the reader
understand the code clearly.
• Comments are just a way of explaining what a program does. It is merely an internal
program documentation.
• The compiler ignores the comments when forming the object file. This means that the
comments are non-executable statements.
C supports two types of commenting:

1. // is used to comment a single statement. This is known as a line comment.


A line comment can be placed anywhere on the line and it does not require to be
specifically ended as the end of the line automatically ends the line.
2. /* */ is used to comment multiple statements.
A /* is ended with */ and all statements that lie within these characters are commented.

16. Make use of suitable example to explain different data types and storage class specifiers
in C. How do they affect variable lifetime and scope?

Storage Class Specifiers in C

• It specifies or defines scope, lifetime, and visibility of variables.


• General Syntax: storage-specifier type variable_name;
• Types of storage class specifiers:

a) extern
b) static
c) register
d) auto

Auto:

• Default storage class for local variables.


• Used inside a function.
• Automatically allocated when the function is called and destroyed when it ends.
• Stored in stack memory.
• Scope: Local to that block (function).
• Lifetime: Until the block (function) ends.
• Eg

void func()

auto int x=0; // same as int x=0;

Register:

• Requests the compiler to store the variable in a CPU register instead of RAM (for faster
access).
• Used for variables accessed frequently (like loop counters).
• Can’t use the address of operator (&) because it may not be in RAM.
Scope: local to that block
Lifetime: Until the block/function ends

Static:

• Preserves a variable’s value between function calls.

Two types:
a) Local static variable:

• Scope: local
• Lifetime: entire program execution

b) Global static variable:

• Scope: visible only within the same file (not accessible from other files)
• Lifetime: entire program execution
• Stored in data segment (not stack).

Extern
It is used to declare a variable or function that is defined somewhere else (maybe later in the
same file or in another file).

• It doesn’t create storage for the variable; it just tells the compiler:
“This variable exists, and you will find its definition elsewhere.”
Linkage in C:

a) External linkage:

• Functions and global variables


• Accessible across files

b) Internal linkage:

• static global variables/functions


• Known only within the file where they are declared

c) No linkage:

• Local variables
• Known only inside their block

You might also like