Computer Assignment
Computer Assignment
Computer Assignment
In C language, header files contain the set of predefined standard library functions. The
“#include” preprocessing directive is used to include the header files with “.h” extension in
the program.
The functions used for standard input and output are present in the stdio.h header file.
Hence to use the functions we need to include the stdio.h header file in our program.
2. What is the purpose of control string in a scanf function? What type of information
does it convey? Of what is the control string composed? How can short integer, long
integer, double, float and long float argument be indicated with in the control string
of a scanf function?
(b) Conditional Statements in C programming are used to make decisions based on the
conditions. Conditional statements execute sequentially when there is no condition
around the statements. If you put some condition for a block of statements, the
execution flow may change based on the result evaluated by the condition. This
process is called decision making in ‘C.’
In ‘C’ programming conditional statements are possible with the help of the
following two constructs:
1. If statement
2. If-else statement
It is also called as branching as a program decides which statement to execute based
on the result of the evaluated condition.
(c) Iteration statements are most commonly know as loops. Also the repetition process in C
is done by using loop control instruction. There are three types of looping statements:
For Loop
While Loop
Do-while loop
6. What is the purpose of switch statement? How does this statement differ from the
other statements in C? Summarize the syntactic rules associated with the use of
switch statement.
Answer 6) Switch case statements are a substitute for long if statements that compare a
variable to several integral values .
The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression.
Switch is a control statement that allows a value to change control of execution.
Syntax:
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
1. If-else statement is used to select among two choices while the switch case
statement is used to select among multiple choices.
2. If-else values are based on constraints while switch case values are based on user
choices.
3. If-else statements are used to implement a linear search while switch case
statement to implement binary search.
4. Tend to be quite tough to edit if-else statement when the nested if-else statement is
used while the switch case statement is simple to edit.
Rules for switch statement in C language
7. What is the purpose of break and continue statements? Within which control
statements can the break and continue statements be included?
Answer 7)
strcpy(str1, "tutorialspoint");
strcpy(str2, "compileonline");
puts(str1);
puts(str2);
return(0);
}
(e) #include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}