20CS241 - Programming Foundations
20CS241 - Programming Foundations
(Autonomous Institution, Reaccredited by NAAC with 8A+9 GRADE Approved by AICTE and Permanently affiliated to Anna University, Chennai)
PART A
1. c) 11110100
2. b) -127 and 127
3. b) Automatic and static variables
4. b) NULL
5. b) II only
PART B
6. Exchange of two variables without temporary variable:
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
7. Big endian and Little Endian difference
Big-endian is an order in which the "big end" (most significant value in the sequence) is
stored first, at the lowest storage address. Little-endian is an order in which the "little end"
(least significant value in the sequence) is stored first.
8. Break and continue statements
Break statement stops the entire process of the loop. Continue statement only stops the
current iteration of the loop. Break also terminates the remaining iterations. Continue
doesn't terminate the next iterations; it resumes with the successive iterations.
9. Largest of two numbers
#include<stdio.h>
int main(void)
{
int a,b,max;
printf("Enter values for a and b : ");
scanf("%d%d", &a, &b);
max = a>b ? a : b;
printf("Larger of %d and %d is %d \n", a, b, max);
return 0;
}
10. Ans: Base address: 101Bh
Part C
Compulsory
11. i) Pseudo code to compute a factorial of a given number.
INPUT number
SET factorial := 1, i := 1
WHILE i <= number DO
COMPUTE factorial := factorial * i
INCREASE i by 1
END LOOP
PRINT factorial
ii) Flow Chart to find the largest of 3 numbers
In Hexadecimal
(768)10 = (300)16
Step by step solution
Step 1: Divide (768)10 successively by 16 until the quotient is 0:
768/16 = 48, remainder is 0
48/16 = 3, remainder is 0
3/16 = 0, remainder is 3
Step 2: Read from the bottom (MSB) to top (LSB) as 300. So, 300 is the
hexadecimal equivalent of decimal number 768 (Answer).
Bitwise Operators:
The following 6 operators are bitwise operators (also known as bit operators as they work
at the bit-level). They are used to perform bitwise operations in C.
The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every
bit of two numbers. The result of AND is 1 only if both bits are 1.
The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit
of two numbers. The result of OR is 1 if any of the two bits is 1.
The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every
bit of two numbers. The result of XOR is 1 if the two bits are different.
The << (left shift) in C or C++ takes two numbers, the left shifts the bits of the first
operand, and the second operand decides the number of places to shift.
The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand,
and the second operand decides the number of places to shift.
The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.
ii) Difference between while loop and Do while loop
1. While the loop is an entry control loop The do-while loop is an exit control
because firstly, the condition is checked, loop because in this, first of all, the
then the loop's body is executed. body of the loop is executed then the
condition is checked true or false.
2. The statement of while loop may not be The statement of the do-while loop
executed at all. must be executed at least once.
3. The while loop terminates when the As long as the condition is true, the
condition becomes false. compiler keeps executing the loop in
the do-while loop.
4. In a while loop, the test condition In a do-while loop, the variable of test
variable must be initialized first to check condition Initialized in the loop also.
the test condition in the loop.
5. In a while loop, at the end of the In this, at the end of the condition, there
condition, there is no semicolon. is a semicolon.
Syntax: Syntax:
while (condition) while (condition);
6. While loop is not used for creating menu- It is mostly used for creating menu-
driven programs. driven programs because at least one
time; the loop is executed whether the
condition is true or false.
A text segment, also known as a code segment or simply as text, is one of the sections
of a program in an object file or in memory, which contains executable instructions.
As a memory region, a text segment may be placed below the heap or stack in order
to prevent heaps and stack overflows from overwriting it.
2. Initialized data segment
Initialized data segment, usually called simply the Data Segment. A data segment is a
portion of the virtual address space of a program, which contains the global variables
and static variables that are initialized by the programmer.
Note that, the data segment is not read-only, since the values of the variables can be
altered at run time.
3. Uninitialized data segment (bss)
Uninitialized data segment often called the <bss= segment, named after an ancient
assembler operator that stood for <block started by symbol.= Data in this segment is
initialized by the kernel to arithmetic 0 before the program starts executing
uninitialized data starts at the end of the data segment and contains all global
variables and static variables that are initialized to zero or do not have explicit
initialization in source code.
4. Heap
Heap is the segment where dynamic memory allocation usually takes place.
The heap area begins at the end of the BSS segment and grows to larger addresses
from there. The Heap area is managed by malloc, realloc, and free, which may use the
brk and sbrk system calls to adjust its size (note that the use of brk/sbrk and a single
<heap area= is not required to fulfill the contract of malloc/realloc/free; they may also
be implemented using mmap to reserve potentially non-contiguous regions of virtual
memory into the process’ virtual address space).
5. Stack
The stack area traditionally adjoined the heap area and grew in the opposite direction;
when the stack pointer met the heap pointer, free memory was exhausted. (With
modern large address spaces and virtual memory techniques they may be placed
almost anywhere, but they still typically grow in opposite directions.)
ARRAY STRUCTURE
Structure refers to a collection
Array refers to a collection consisting of
consisting of elements of
elements of homogeneous data type.
heterogeneous data type.
Array uses subscripts or <[ ]= (square Structure uses <.= (Dot operator) for
bracket) for element access element access
Array is pointer as it points to the first
Structure is not a pointer
element of the collection.
ARRAY STRUCTURE
Instantiation of Array objects is not Instantiation of Structure objects is
possible. possible.
Array size is fixed and is basically the Structure size is not fixed as each
number of elements multiplied by the element of Structure can be of
size of an element. different type and size.
Bit field is not possible in an Array. Bit field is possible in an Structure.
Array declaration is done simply using [] Structure declaration is done with the
and not any keyword. help of <struct= keyword.
Arrays is a non-primitive datatype Structure is a user-defined datatype.
Array traversal and searching is easy and Structure traversal and searching is
fast. complex and slow.
struct sruct_name{ data_type1 ele1;
data_type array_name[size];
data_type2 ele2; };
Structure elements may or may not be
Array elements are stored in contiguous
stored in a contiguous memory
memory locations.
location.
Array elements are accessed by their Structure elements are accessed by
index number using subscripts. their names using dot operator.
Output:
o In call by value method, we can not modify the value of the actual parameter by the
formal parameter.
o In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
o The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the valu
e of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual pa
rameters do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters
, a = 20, b = 10
}
Call by reference in C
In call by reference, the address of the variable is passed into the function call as the actual
parameter.
The value of the actual parameters can be modified by changing the formal parameters since the
address of the actual parameters is passed.
In call by reference, the memory allocation is similar for both formal parameters and actual
parameters. All the operations in the function are performed on the value stored at the address of
the actual parameters, and the modified value gets stored at the same address.
Example:
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the valu
e of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual p
arameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
// Formal parameters, a = 20, b = 10
}
17. i) Array and Pointers
Notice that, the address of &x[0] and x is the same. It's because the variable
name x points to the first element of the array.
return 0;
}
When you run the program, the output will be:
Enter 6 numbers: 2
3
4
4
12
4
Sum = 29
int main() {
...
}
It can also given command-line arguments in C. Command-line arguments are the values given
after the name of the program in the command-line shell of Operating Systems. Command-line
arguments are handled by the main() function of a C program.
To pass command-line arguments, we typically define main() with two arguments: the first
argument is the number of command-line arguments and the second is a list of command-line
arguments.
Syntax:
int main(int argc, char *argv[]) { /* ... */ }
or
int main(int argc, char **argv) { /* ... */ }
Here,
argc (ARGument Count) is an integer variable that stores the number of command-line
arguments passed by the user including the name of the program. So if we pass a value to
a program, the value of argc would be 2 (one for argument and one for program name)
The value of argc should be non-negative.
argv (ARGument Vector) is an array of character pointers listing all the arguments.
If argc is greater than zero, the array elements from argv[0] to argv[argc-1] will contain
pointers to strings.
argv[0] is the name of the program , After that till argv[argc-1] every element is
command -line arguments.
For better understanding run this code on your Linux machine.
Example:
In C
Terminal Input:
$ g++ mainreturn.cpp -o main
$ ./main geeks for geeks
Output:
You have entered 4 arguments:
./main
geeks
for
geeks
Note : Other platform-dependent formats are also allowed by the C and C++ standards; for
example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving
the program’s environment, otherwise accessible through getenv in stdlib.h. Refer C program to
print environment variables for details.
3. They are used to control programs from outside instead of hard coding those values
inside the code.
4. argv[argc] is a NULL pointer.
5. argv[0] holds the name of the program.
6. argv[1] points to the first command line argument and argv[argc-1] points to the last
argument.
Note: You pass all the command line arguments separated by a space, but if the argument itself
has a space, then you can pass such arguments by putting them inside double quotes <= or single
quotes =.
Example:
In C program:
// C program to illustrate
// command line arguments
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Program name is: %s", argv[0]);
if (argc == 1)
printf("\nNo Extra Command Line Argument Passed "
"Other Than Program Name");
if (argc >= 2) {
printf("\nNumber Of Arguments Passed: %d", argc);
printf("\n----Following Are The Command Line "
"Arguments Passed----");
for (int i = 0; i < argc; i++)
printf("\nargv[%d]: %s", i, argv[i]);
}
return 0;
}
Terminal Input:
$ ./a.out
Output:
Program Name Is: ./a.out
No Extra Command Line Argument Passed Other Than Program Name
2. Three arguments: When the above code is compiled and executed with three arguments, it
produces the following output.
Terminal Input:
$ ./a.out First Second Third
Output:
Program Name Is: ./a.out
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third
3. Single Argument: When the above code is compiled and executed with a single argument
separated by space but inside double quotes, it produces the following output.
Terminal Input:
$ ./a.out "First Second Third"
Output:
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third
4. A single argument in quotes separated by space: When the above code is compiled and
executed with a single argument separated by space but inside single quotes, it produces the
following output.
Terminal Input:
$ ./a.out 'First Second Third'