0% found this document useful (0 votes)
13 views13 pages

Short Question C

The document provides a comprehensive overview of the C programming language, covering topics such as its classification as a mid-level language, data types, variable scope, memory allocation, recursion, loops, functions, and file operations. It also includes examples of C programs for practical understanding, such as checking for prime numbers, swapping variables, and reversing numbers. Additionally, it explains key concepts like call by value vs. call by reference and the differences between source code and object code.

Uploaded by

jibonpyypl686
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)
13 views13 pages

Short Question C

The document provides a comprehensive overview of the C programming language, covering topics such as its classification as a mid-level language, data types, variable scope, memory allocation, recursion, loops, functions, and file operations. It also includes examples of C programs for practical understanding, such as checking for prime numbers, swapping variables, and reversing numbers. Additionally, it explains key concepts like call by value vs. call by reference and the differences between source code and object code.

Uploaded by

jibonpyypl686
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/ 13

Structural Programming Language with C

Final Exam Suggestion

1. Why is C called a mid-level programming language?


Due to its ability to support both low-level and high-level features, C is considered a middle-level
language. It is both an assembly-level language, i.e. a low-level language, and a higher-level
language. Programs that are written in C are converted into assembly code, and they support pointer
arithmetic (low-level) while being machine-independent (high-level). Therefore, C is often referred to
as a middle-level language. C can be used to write operating systems and menu-driven consumer
billing systems.

2. What are the features of the C programming language?

Features of C Programming language

3. What are basic data types supported in the C Programming Language?


Each variable in C has an associated data type. Each data type requires different amounts of memory
and has some specific operations which can be performed over it. It specifies the type of data that
the variable can store like integer, character, floating, double, etc. In C data types are broadly
classified into 4 categories:

• Primitive data types: Primitive data types can be further classified into integer, and floating
data types.

o Void Types: Void data types come under primitive data types. Void data types
provide no result to their caller and have no value associated with them.

• User Defined data types: These data types are defined by the user to make the program
more readable.

• Derived data types: Data types that are derived from primitive or built-in data types.
Structural Programming Language with C
Final Exam Suggestion

Data Types in C

4. What do you mean by the scope of the variable?


Scope in a programming language is the block or a region where a defined variable will have its
existence and beyond that region, the variable is automatically destroyed. Every variable has its
defined scope. In simple terms, the scope of a variable is equal to its life in the program. The variable
can be declared in three places These are:

• Local Variables: Inside a given function or a block

• Global Variables: Out of all functions globally inside the program.

• Formal Parameters: In-function parameters only.

5. What is the difference between malloc() and calloc() in the C programming


language?
calloc() and malloc() library functions are used to allocate dynamic memory. Dynamic memory is the
memory that is allocated during the runtime of the program from the heap segment. “stdlib.h” is the
header file that is used to facilitate dynamic memory allocation in the C Programming language.

Parameter Malloc() Calloc()

It is a function that assigns more than


It is a function that creates one
one block of memory to a single
block of memory of a fixed size.
Definition variable.

Number of
It only takes one argument. It takes two arguments.
arguments
Structural Programming Language with C
Final Exam Suggestion
Parameter Malloc() Calloc()

malloc() function is faster than


calloc() is slower than malloc().
Speed calloc().

Efficiency It has high time efficiency. It has low time efficiency.

It is used to indicate memory It is used to indicate contiguous


Usage allocation. memory allocation.

6. What is recursion in C?
Recursion is the process of making the function call itself directly or indirectly. A recursive function
solves a particular problem by calling a copy of itself and solving smaller subproblems that sum up
the original problems. Recursion helps to reduce the length of code and make it more
understandable. The recursive function uses a LIFO ( Last In First Out ) structure like a stack. Every
recursive call in the program requires extra space in the stack memory.

7. What is the difference between the local and global variables in C?


Local variables are declared inside a block or function but global variables are declared outside the
block or function to be accessed globally.

Local Variables Global Variables

Variables that are declared outside the block or


Declared inside a block or a function.
a function.

By default, variables store a garbage value. By default value of the global value is zero.

The life of the local variables is destroyed after The life of the global variable exists until the
the block or a function. program is executed.

Variables are stored inside the stack unless The storage location of the global variable is
they are specified by the programmer. decided by the compiler.
Structural Programming Language with C
Final Exam Suggestion
Local Variables Global Variables

To access the local variables in other functions No parameter passing is required. They are
parameter passing is required. globally visible throughout the program.

8. What are loops and how can we create an infinite loop in C?


Loops are used to execute a block of statements repeatedly. The statement which is to be repeated
will be executed n times inside the loop until the given condition is reached. There are two types of
loops Entry controlled and Exit-controlled loops in the C programming language. An Infinite loop is a
piece of code that lacks a functional exit. So, it repeats indefinitely. There can be only two things
when there is an infinite loop in the program. One it was designed to loop endlessly until the
condition is met within the loop. Another can be wrong or unsatisfied break conditions in the
program.

Types of Loops

Below is the program for infinite loop in C:

// C program for infinite loop


// using for, while, do-while
#include <stdio.h>

// Driver code
int main()
{
for (;;) {
printf("Infinite-loop\n");
}

while (1) {
printf("Infinite-loop\n");
}

do {
Structural Programming Language with C
Final Exam Suggestion
printf("Infinite-loop\n");
} while (1);

return 0;
}

9. What are header files and their uses?


C language has numerous libraries which contain predefined functions to make programming easier.
Header files contain predefined standard library functions. All header files must have a ‘.h’ extension.
Header files contain function definitions, data type definitions, and macros which can be imported
with the help of the preprocessor directive ‘#include’. Preprocessor directives instruct the compiler
that these files are needed to be processed before the compilation.
There are two types of header files i.e, User-defined header files and Pre-existing header files. For
example, if our code needs to take input from the user and print desired output to the screen then
‘stdio.h’ header file must be included in the program as #include<stdio.h>. This header file contains
functions like scanf() and printf() which are used to take input from the user and print the content.

10. What are the functions and their types?


The function is a block of code that is used to perform a task multiple times rather than writing it out
multiple times in our program. Functions avoid repetition of code and increase the readability of the
program. Modifying a program becomes easier with the help of function and hence reduces the
chances of error. There are two types of functions:

• User-defined Functions: Functions that are defined by the user to reduce the complexity of
big programs. They are built only to satisfy the condition in which the user is facing issues
and are commonly known as “tailor-made functions”.

• Built-in Functions: Library functions are provided by the compiler package and consist of
special functions with special and different meanings. These functions give programmers an
edge as we can directly use them without defining them.

11. How to convert a string to numbers in C?


In C we have 2 main methods to convert strings to numbers i.e, Using string stream, Using stoi()
library Function or using atoi() library function.

• sscanf(): It reads input from a string rather than standard input.

• stoi() or atoi(): These functions takes a string literal or a character array as an argument and
an integer value is returned.

• What are reserved keywords?


• Every keyword is meant to perform a specific task in a program. Their meaning is already
defined and cannot be used for purposes other than what they are originally intended for.
Structural Programming Language with C
Final Exam Suggestion
C Programming language supports 32 keywords. Some examples of reserved keywords are
auto, else, if, long, int, switch, typedef, etc.

12. What are reserved keywords?


Every keyword is meant to perform a specific task in a program. Their meaning is already defined
and cannot be used for purposes other than what they are originally intended for. C Programming
language supports 32 keywords. Some examples of reserved keywords are auto, else, if, long, int,
switch, typedef, etc

13. What is a structure?


The structure is a keyword that is used to create user-defined data types. The structure allows
storing multiple types of data in a single unit. The structure members can only be accessed
through the structure variable.
Example:
struct student
{
char name[20];
int roll_no;
char address[20];
char branch[20];
};// C Program to show the
// use of structure
#include <stdio.h>
#include <string.h>// Structure student declared
struct student {
char name[20];
int roll_no;
char address[50];
char branch[50];
};// Driver code
int main()
{
struct student obj;
strcpy(obj.name, "Kamlesh_Joshi");
obj.roll_no = 27;
strcpy(obj.address, "Haldwani");
strcpy(obj.branch, "Computer Science And Engineering"); // Accessing members of student obj
printf("Name: %s\n", obj.name);
printf("Roll_No: %d \n", obj.roll_no);
printf("Address: %s\n", obj.address);
printf("Branch: %s", obj.branch);

return 0;
}
Structural Programming Language with C
Final Exam Suggestion

14. What is the difference between call by value and call by reference?

Call by value Call by Reference

Values of the variable are passed while function The address of a variable(location of
calls. variable) is passed while the function call.

Dummy variables copy the value of each variable in Dummy variables copy the address of
the function call. actual variables.

Changes made to dummy variables in the called


We can manipulate the actual variables
function have no effect on actual variables in the
using addresses.
calling function.

A simple technique is used to pass the values of The address values of variables must be
variables. stored in pointer variables.

15. Write a C program to check whether a number is prime or not.


Structural Programming Language with C
Final Exam Suggestion

Number Prime or not

❖ Number Prime or not


#include <math.h>
#include <stdio.h> // Driver code
int main()
{
int num;
int check = 1;
printf("Enter a number: \n");
scanf("%d", &num); // Iterating from 2 to sqrt(num)
for (int i = 2; i <= sqrt(num); i++) {
// If the given number is divisible by
// any number between 2 and n/2 then
// the given number is not a prime number
if (num % i == 0) {
check = 0;
break;
}
}
if (num <= 1) {
check = 0;
}
if (check == 1) {
printf("%d is a prime number", num);
}
Structural Programming Language with C
Final Exam Suggestion
else {
printf("%d is not a prime number", num);
}
return 0;
}

16. How is source code different from object code?

Source Code Object Code

object code is generated by a compiler or


Source code is generated by the programmer.
another translator.

Low-level code is not human-


High-level code which is human-understandable.
understandable.

Source code can be easily modified and contains Object code cannot be modified and
less number of statements than object code. contains more statements than source code.

Source code can be changed over time and is not Object code can be modified and is system
system specific. specific.

Object code is more close to the machine


Source code is less close to the machine and is
and is the output of the compiler or any
input to the compiler or any other translator.
other translator.
Structural Programming Language with C
Final Exam Suggestion
Source Code Object Code

Language translators like compilers, assemblers,


Object code is machine code so it does not
and interpreters are used to translate source
require any translation.
code to object code.

17. What is static memory allocation and dynamic memory allocation?


• Static memory allocation: Memory allocation which is done at compile time is known as
static memory allocation. Static memory allocation saves running time. It is faster than
dynamic memory allocation as memory allocation is done from the stack. This memory
allocation method is less efficient as compared to dynamic memory allocation. It is mostly
preferred in the array.

• Dynamic memory allocation: Memory allocation done at execution or run time is known as
dynamic memory allocation. Dynamic memory allocation is slower than static memory
allocation as memory allocation is done from the heap. This memory allocation method is
more efficient as compared to static memory allocation. It is mostly preferred in the linked
list.

18. Write a C program to swap two numbers without using a third variable.

Swap Two Number

// C program to swap two variables


// without using a third variable
#include <stdio.h>

int main()
{
// Variable declaration
Structural Programming Language with C
Final Exam Suggestion
int var1 = 50;
int var2 = 60;

printf(
"Values before swap are var1 = %d and var2 = %d\n",
var1, var2);

// var1 = 110 ( 50 + 60)


var1 = var1 + var2;

// var2 = 50 (110 - 50)


var2 = var1 - var2;

// var1 = 60 (110 - 50)


var1 = var1 - var2;

printf("Values after swap are var1 = %d and var2 = %d",


var1, var2);

return 0;
}

19. Write a program to reverse a given number.

// C program to reverse digits


// of a number
#include <stdio.h>

// Driver code
int main()
{
int n, rev = 0;

printf("Enter Number to be reversed : ");


scanf("%d", &n);

// r will store the remainder while we


// reverse the digit and store it in rev
int r = 0;
while (n != 0)
{
r = n % 10;
rev = rev * 10 + r;
n /= 10;
}
Structural Programming Language with C
Final Exam Suggestion
printf("Number After reversing digits is: %d", rev);

return 0;
}

20. Mention file operations in C.

In C programming Basic File Handling Techniques provide the basic functionalities that programmers
can perform against the system.
Structural Programming Language with C
Final Exam Suggestion

File Operations in C

You might also like