Uploaded by ColdRage Music

MCL ASSIGNMENT-1

advertisement
ANS-1
Sure, here are some examples of decision control instructions in C programming:
1. If statement
The if statement is used to execute a block of code if a condition is true. The syntax for
an if statement is as follows:
C
if (condition) {
// Block of code to be executed if condition is true
}
Use code with caution. Learn more
content_copy
For example, the following code will print "The number is greater than 5" if the variable
number is greater than 5:
C
int number = 10;
if (number > 5) {
printf("The number is greater than 5");
}
Use code with caution. Learn more
content_copy
2. If-else statement
The if-else statement is used to execute a different block of code depending on whether
a condition is true or false. The syntax for an if-else statement is as follows:
C
if (condition) {
// Block of code to be executed if condition is true
} else {
// Block of code to be executed if condition is false
}
Use code with caution. Learn more
content_copy
For example, the following code will print "The number is greater than 5" if the variable
number is greater than 5, and "The number is less than or equal to 5" if the variable
number is less than or equal to 5:
C
int number = 10;
if (number > 5) {
printf("The number is greater than 5");
} else {
printf("The number is less than or equal to 5");
}
Use code with caution. Learn more
content_copy
3. Conditional operators
Conditional operators are used to evaluate a condition and return a Boolean value (true
or false). The most common conditional operators are:

<
(less than)

>
(greater than)

<=
(less than or equal to)

>=
(greater than or equal to)

==
(equal to)

!=
(not equal to)
For example, the following code will return true if the variable number is greater than 5,
and false otherwise:
C
int number = 10;
bool isNumberGreaterThan5 = number > 5;
Use code with caution. Learn more
content_copy
Here are some other examples of decision control instructions in C programming:

Nested if statements: Nested if statements are used to nest multiple if statements
within each other. This allows for more complex decision making.

Else-if statements: Else-if statements are used to provide multiple options for
what to do if a condition is not met.

Switch statements: Switch statements are used to compare a variable to a set of
values and execute a different block of code for each value.
ANS-2
Here are some examples of C codes that illustrate the use of for loop and do-while loop:
For loop
C
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d\n", i);
}
return 0;
}
Use code with caution. Learn more
content_copy
This code will print the numbers from 0 to 9. The for loop has three parts:

The initialization part, which is executed once before the loop starts. In this case,
the variable i is initialized to 0.

The condition part, which is evaluated before each iteration of the loop. In this
case, the condition is i < 10, which means that the loop will continue to execute
as long as i is less than 10.

The increment part, which is executed after each iteration of the loop. In this
case, the variable i is incremented by 1.
Do-while loop
C
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 10);
return 0;
}
Use code with caution. Learn more
content_copy
This code is similar to the previous example, but it uses a do-while loop instead of a for
loop. The do-while loop will execute the body of the loop at least once, even if the
condition is false. In this case, the body of the loop will be executed once, and then the
condition i < 10 will be evaluated. If the condition is true, the body of the loop will be
executed again, and this process will continue until the condition is false.
ANS-3
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
break;
}
return 0;
}
This code will print the name of the day of the week corresponding to the value of the
variable day. The switch statement has the following syntax:
C
switch (expression) {
case value1:
// block of code to be executed if expression == value1
break;
case value2:
// block of code to be executed if expression == value2
break;
...
default:
// block of code to be executed if expression does not match any of the
cases
break;
}
The expression in the switch statement must be of an integer or character type. The
cases are constants of the same type as the expression. If the expression matches a
case, the block of code associated with that case is executed. If the expression does
not match any of the cases, the default block of code is executed.
The break statement is used to exit from the switch statement. If the break statement is
not used, the next case will also be executed.
ANS-4
Here are the different data types in C programming:

Integer: An integer is a whole number, without decimals. The size of an integer
depends on the compiler and the architecture of the machine. For example, on a
32-bit machine, an integer is typically 4 bytes long.

Floating-point: A floating-point number is a number with a decimal point. The size
of a floating-point number also depends on the compiler and the architecture of
the machine. For example, on a 32-bit machine, a floating-point number is
typically 8 bytes long.

Character: A character is a single letter, digit, or symbol. Characters are stored
as integers, but they are interpreted differently by the compiler. For example, the
character 'A' is stored as the integer 65.

String: A string is a sequence of characters. Strings are stored as arrays of
characters.

Enumeration: An enumeration is a list of named constants. Enumerations are
used to represent a set of related values. For example, the days of the week
could be represented by an enumeration.

Void: The void type is used to represent the absence of a value. The void type is
used in function declarations to indicate that the function does not return a value.
Here is a table that summarizes the different data types in C programming:
Data type
Size
Description
Integer
4 bytes
A whole number, without
decimals.
Floatingpoint
8 bytes
A number with a decimal
point.
Character
1 byte
A single letter, digit, or
symbol.
String
Variable
A sequence of characters.
Enumeration
Variable
A list of named constants.
Void
0 bytes
The absence of a value.
ANS-6
Here are the different storage classes in C programming:

Automatic storage class: Variables declared with the auto keyword are
automatically allocated on the stack when the variable is declared and
deallocated when the function containing the variable is exited. Automatic
variables are local to the function in which they are declared and cannot be
accessed from other functions.

External storage class: Variables declared with the extern keyword are declared
globally and can be accessed from any function in the program. External
variables are stored in the program's data segment and are initialized to zero
when the program starts.

Static storage class: Variables declared with the static keyword are also
declared globally, but they retain their value between function calls. Static
variables are stored in the program's data segment and are initialized to their
default value when the program starts.

Register storage class: Variables declared with the register keyword are stored
in a CPU register if possible. Register variables are faster than automatic
variables, but there are a limited number of registers available, so only a few
variables can be declared as register variables.
Here is a table that summarizes the different storage classes in C programming:
Storage
class
Keyword
Scope
Lifetime
Automatic
auto
Local
Within the function
External
extern
Global
Throughout the
program
Static
static
Global
Throughout the
program
Register
register
Local
Within the function
ANS-7
Preprocessor directives are instructions to the C preprocessor that are used to modify
the source code before it is compiled. They are preceded by a hash (#) symbol and are
typically used to define macros, include header files, and control the compilation
process.
Here are some of the most common preprocessor directives:

#define: This directive is used to define a macro. A macro is a named piece of
code that is expanded by the preprocessor before the code is compiled. Macros
are often used to define constants, functions, and conditional code.

#include: This directive is used to include a header file in the source code.
Header files contain declarations for functions, macros, and variables that are
used by multiple source files.

#undef: This directive is used to undefine a macro. This is useful if you want to
redefine a macro with a different definition.

#if: This directive is used to conditionally compile code. The code between the #if
directive and the corresponding #endif directive is only compiled if the condition
evaluates to true.

#else: This directive is used to provide an alternative block of code that is
compiled if the condition in the #if directive evaluates to false.

#elif: This directive is used to provide an additional condition that can be used to
control the compilation of code.

#endif: This directive marks the end of a conditional block of code.
The preprocessor directive MACRO-expansion is used to expand a macro. Macro
expansion is the process of replacing all occurrences of a macro name with the
definition of the macro. For example, the following code defines a macro called
MAX_VALUE and then uses the macro in an expression:
#define MAX_VALUE 100
int main() {
int x = MAX_VALUE;
printf("The maximum value is %d\n", x);
return 0;
}
When this code is compiled, the preprocessor will replace all occurrences of MAX_VALUE
with the value 100. This means that the expression MAX_VALUE will be evaluated as 100
and the value of x will be set to 100.
The syntax for the MACRO-expansion directive is as follows:
#define MACRO_NAME definition
where MACRO_NAME is the name of the macro and definition is the definition of the
macro. The definition of the macro can be any valid C code.
I hope this helps! Let me know if you have any other questions.
ANS-8
here are the different steps involved in converting a C program into an executable form,
with a flow diagram:
1. Write the C program. This is the first step, and it involves writing the C code in a
text editor.
2. Compile the C program. The next step is to compile the C program using a
compiler. The compiler will convert the C code into machine code that can be
executed by the computer.
3. Link the object files. The compiler will produce one or more object files. These
object files need to be linked together to create an executable file.
4. Create the executable file. The linker will create an executable file that can be
executed by the computer.
Here is a flow diagram that summarizes the steps involved in converting a C program
into an executable form:
flowchart
start
-> Compile C program
if errors then
-> Fix errors
else
-> Link object files
-> Create executable file
end
stop
The following are some of the important points to note about the steps involved in
converting a C program into an executable form:

The compiler is a program that converts C code into machine code.

The linker is a program that links object files together to create an executable file.

The executable file is a file that can be executed by the computer.
ANS-9
Here is the C code to illustrate the concept of passing array elements to a function in
two ways: call by value and call by reference:
#include <stdio.h>
void call_by_value(int *array, int size) {
for (int i = 0; i < size; i++) {
array[i] = i * 2;
}
}
void call_by_reference(int *array, int size) {
for (int i = 0; i < size; i++) {
*(array + i) = i * 2;
}
}
int main() {
int array[] = {1, 2, 3, 4, 5};
int size = sizeof(array) / sizeof(array[0]);
// Call by value
call_by_value(array, size);
printf("After call by value: ");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
// Call by reference
call_by_reference(array, size);
printf("After call by reference: ");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
This code will print the following output:
After call by value: 1 2 4 6 8
After call by reference: 2 4 6 8 10
The difference between call by value and call by reference is that in call by value, a
copy of the array elements is passed to the function, while in call by reference, the
address of the array elements is passed to the function. This means that in call by
value, any changes made to the array elements in the function will not be reflected in
the original array, while in call by reference, any changes made to the array elements in
the function will be reflected in the original array.
In the code above, the call_by_value function takes a pointer to an array and the size
of the array as input parameters. The function then iterates through the array and
multiplies each element by 2. The changes made to the array elements in the
function are not reflected in the original array because the array
elements are passed by value.
call_by_value
The call_by_reference function also takes a pointer to an array and the size of the
array as input parameters. However, the function takes the address of the array
elements, not the values of the array elements. This means that any changes made to
the array elements in the call_by_reference function will be reflected in the original
array.
ANS-10
#include <stdio.h>
int main() {
int x = 10;
printf("The value of x is %d\n", x);
return 0;
}
The format string in this example is "The value of x is %d\n". The %d format specifier
indicates that the value of x is to be formatted as an integer. The \n character at the end
of the format string indicates that a newline character is to be printed after the output.
The printf() function can be used to print a variety of different types of data, including
integers, floating-point numbers, strings, and characters. The following table lists some
of the most common format specifiers:
Format
specifier
Description
%d
Prints an integer
%f
Prints a floating-point
number
%s
Prints a string
%c
Prints a character
ANS-11
The printf() function in C programming is used to print formatted output to the console. It is a standard
library function, which means that it is included in the stdio.h header file.
The printf() function takes a format string as its first argument, and a variable number of arguments as its
remaining arguments. The format string specifies the format of the output, and the remaining
arguments are the values that are to be formatted and printed.
For example, the following code prints the value of the variable x to the console:
#include <stdio.h>
int main() {
int x;
printf("Enter an integer: ");
scanf("%d", &x);
printf("The integer you entered is %d\n", x);
return 0;
}
The format string in this example is "Enter an integer: %d". The %d format specifier indicates that the
input is to be formatted as an integer. The & character before the variable x indicates that the address of
the variable is to be passed to the scanf() function. This is necessary because the scanf() function stores
the input in the memory location pointed to by the variable.
ANS-12
To receive input in C programming, you can use the scanf() function. The scanf() function is a standard
library function, which means that it is included in the stdio.h header file.
The scanf() function takes a format string as its first argument, and a variable number of arguments as its
remaining arguments. The format string specifies the format of the input, and the remaining arguments
are the variables that are to be used to store the input.
For example, the following code receives an integer input from the user:
#include <stdio.h>
int main() {
int x;
printf("Enter an integer: ");
scanf("%d", &x);
printf("The integer you entered is %d\n", x);
return 0;
}
The format string in this example is "Enter an integer: %d". The %d format specifier indicates that the
input is to be formatted as an integer. The & character before the variable x indicates that the address of
the variable is to be passed to the scanf() function. This is necessary because the scanf() function stores
the input in the memory location pointed to by the variable.
Download