Computer Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Assignments/Tutorials-4

(Input/output and control statements in C)


1. What are the commonly used input/output functions in C? How they are accessed?
What is the standard header files called in most versions of C? How is the file
included within a program?

Answer 1) The basic input/output functions  are getchar, putchar, puts, scanf and printf.


The first two functions, getchar and putchar, are used to transfer single characters. The next
function puts is used to output strings, and the last two functions, scanf and printf, permit
the transfer of single characters, numerical values and strings.

An input/output function can be accessed from anywhere within a program simply by


writing the function name, followed by a list of arguments enclosed in parentheses. The
arguments represent data items that are sent to the function. 

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?

Answer 2) The control string part of the scanf function tells the computer what type of data


will be read from the keyboard. The type is designated, as in the printf function, by a set
of conversion specifications. A conversion specification begins with a % and ends with
a conversion character. The conversion character you choose is related to the type of data
you will read from the keyboard.
The control string consists almost entirely of conversion specifiers, which determine
the type of data read from the keyboard. Second, the other arguments portion of the
statement consists of a comma-separated list of pointer expressions, or addresses.
a. Short int – scanf(“%hd”)
b. Long integer – scanf(“%ld”)
c. Double – scanf(“%lf”)
d. Float – scanf(“%f”)
e. Long float - scanf(“%lf”)
3. What is the purpose of printf function in C? How does it used within a c program?
In what ways does the control string within a printf function differ from the control
string within a scanf function?

Answer 3) In C programming language, printf() function is used to print the (“character,


string, float, integer, octal and hexadecimal values”) onto the output screen.
We use printf() function with %d format specifier to display the value of an integer variable.
In the case of scanf function the control string is used to determine what type of data will
be read from the keyboard. But in printf function the control string is used to display the
information entered in it.

4. Describe various control statement.


a. Sequential.
b. Selection/branching (if, if-else, switch).
c. Iterative/looping (for, while, do-while).
Answer 4)
(a) Sequence of statements are written to accomplish a specific activity. So, statements
are executed in the order they are specified in the program. This way of executing
statements sequentially is known as Sequential control statements. 

(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

A loop basically consists of three parts: initialization, test


expression, increment/decrement, or update value. For the different type of loops, these
expressions might be present at different stages of the loop.
5. What is the purpose of for statement? How does it differ from the while statement
and do-while statement? Can any of the three initial expressions in the for statement
be omitted? If so, what are the consequences of each omission?

Answer 5) A for loop is a repetition control structure that allows you to efficiently


write a loop that needs to execute a specific number of times.
Syntax:
The syntax of a for loop in C programming language is −
for (initialisation; condition; increment)
{
statement(s);
}

A while loop in C programming repeatedly executes a target statement as long as a given


condition is true. The syntax is like below.
while(condition)
{
   statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may
be any expression, and true is any nonzero value. The loop iterates while the condition is
true.
When the condition becomes false, the program control passes to the line immediately
following the loop.
Unlike for and while loops, which test the loop condition at the top of the loop, the
do...while loop checks its condition at the bottom of the loop.
A do...while loop is like a while loop, except that a do...while loop is guaranteed to execute
at least one time.
do {
   statement(s);
}
while(condition);
C allows any or all of the expressions that control a for statement to be omitted.
• If the first expression is omitted, no initialization is performed before the loop is executed:
i = 10;
for (; i > 0; --i)
printf("T minus %d and counting\n", i);
• If the third expression is omitted the loop body is expression is omitted, the loop body is
responsible for ensuring that the value of the second expression eventually becomes false:
for (i = 10; i > 0;)
printf("T minus %d and counting\n", i--);
• When the first and third expressions are both omitted the resulting loop is nothing more
than a omitted, the resulting loop is nothing more than a while statement :
for (; i > 0;)
printf("T minus %d and counting\n", i--); is the same as
while (i > 0)
printf("T minus %d and counting\n", i--);
• If the second expression is missing, it defaults to a true value, so the true value, so the for
statement doesn’t statement doesn’t terminate (unless stopped in some other fashion).

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

1) The switch expression must be of an integer or character type.

2) The case value must be an integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in switch case is not must. It is optional. If there is no break


statement found in the case, all the cases will be executed present after the matched
case. It is known as fall through the state of C switch statement.

7. What is the purpose of break and continue statements? Within which control
statements can the break and continue statements be included?

Answer 7)

Break and control statements can be included within conditional statements.


8. Describe with examples:
a. getchar ().
b. putchar ().
c. gets ()
d. puts ()
e. Nested if-else
Answer 8)
(a) #include <stdio.h>  
#include <conio.h>      
void main()  
{  
    char c;   
    printf ("\n Enter a character \n");  
    c = getchar(); // get a single character  
    printf(" You have passed ");  
    putchar(c); // print a single character using putchar  
  
    getch();  
}  
(b) #include <stdio.h>
int main()
{
char ch = 'G';
putchar(ch);
return (0);
}
(c) #include <stdio.h>
int main ()
{
char str[50];
printf("Enter a string : ");
gets(str);
printf("You entered: %s", str);
return(0);
}
(d) #include <stdio.h>
#include <string.h>
int main () {
char str1[15];
char str2[15];

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;
}

You might also like