Chapter 7 Introduction to ‘C’

Introduction to ‘C’ Long Answer Type Questions (8 Marks)

Question 1.
Explain the structure of a ‘C’ program with example. (UP 2019)
Answer:
Structure of ‘C’ Program:
Before understanding the structure of a ‘C’ program you must know these important points:

  • ‘C’ is a case sensitive language, it means all the keywords must be written in lower case.
  • Key-words can’t be used as a variable or function name.
  • Every instruction should end with (;) sign.
  • Preprocessor directives (required) should be there in the beginning.
  • Function main () is must for a program.

Now to write a program in ‘C’ we need to follow these steps:
Define preprocessor directives (include header files according to prototype functions, standard I/O library functions to be used).

  • Open function main ()
  • Assign data types and variables.
  • Define the body of the function
  • End the function main ()

Example

/* harsh.c : first example for students */

# include

void main ()

{

int a = 10, b = 15, c;

printf (“Hellow students \n”);

c = a + b ;

printf (“The sum of % d and % d is % d”, a, b, c);

}

In the above example the first line
/*harsh.c: first example for students */
is a comment and non-executable. Comments in ‘C’ begins with (/*) and ends with (*/).

Second-line
#include
is a preprocessor directive. The preprocessor processes the ‘C’ program before the compiler. Here stdio.h is a header file consists of standard I/O functions. A header file related to the functions used in program should be included at the beginning.
The line
void main()
indicates “the beginning of the program”. The compilation of the program starts from main () function.
{, the symbol indicates the beginning of the main () function

The line
int a = 10, b = 15, C;
is for declaration of variables and data types.

Lines
printf (“Hellow students \n”);
C = a + b.
printf (“The sum of % d and % d is % d”, a, b, c);
are the body of the program.
and symbol ‘}’ indicates end of main ( ) function.

Question 2.
What is looping? Write about the different statements used to erect loop in ‘C’?
Answer:
Looping
Looping Statements: A computer program is a set of statements, which is normally executed sequentially. But in most of the cases, it is necessary to repeat certain steps to meet a specific condition. This repetitive operation is done through a loop control structure.
A loop is basically the execution of the sequence of statements repeatedly until a particular condition is true or false. In ‘C’ language following looping statements are used:

while Statements:
The while loop repeats a statement or a set of statements until a certain condition is true.
Syntax

while (condition)

{

statements

}

Here, the condition may be any expression having a non-zero value. The loop continues until the condition is true. When the condition fails, the program body attached with loop (statements), will not be executed.

Example: To print the first 20 natural numbers.

#include<stdio.h>

#include<conio.h>

void main ()

{

int a = 1;

while (a < = 20)

{

printf (“%d”, a);

a++;

}

getch ();

}

do-while statement:
Working of this statement is similar to the working of while statement but in this, at least one time the attached loop (statements) is executed, no matter whether the condition is true or false.
Synax:

do

{

statements

}

while (condition);

Example: To print first 10 odd numbers.

#include<stdio.h>

#include<conio.h>

void main ()

{

int a = 1;

do

{

printf (“% d”, a);

a+ = 2;

{while (a <=19)};

}

for loop statement:
The for loop is an ideal looping statement when we know how many times the loop will be executed.
Syntax:

for (initialization; condition; counter)

{

statements

}

Here,

  • Initialization is generally an assignment which is used to set the loop control variable, e.g., a = 0.
  • Condition always tells the limit of the loop or determines when to exit the loop. e.g., a < 10
  • Counter defines how the loop control variable will change each time the loop is repeated. This may be incremented or decremented. e.g., a++, a–.

Example: To print your name 10 times.

#include<stdio.h>

#include<conio.h>

void main ()

{

int i;

char name [20];

clrscr ();

printf (“Enter your name”);

gets(name);

for (i = 1; i <= 10; i++)

puts (name);

}

getch ();

}

Introduction to ‘C’ Short Answer Type Questions (4 Marks)

Question 1.
Explain the character set in ‘C’.
Answer:
Character Set in ‘C’. The set of characters that may appear in a legal ‘C’ program is called the character set for ‘C’. These include some graphics as well as non-graphic characters. The ‘C’ character set consists of upper case and lower case alphabets, special characters, digits and white spaces. The alphabets and digits together are called the alphanumeric characters.
Alphabets
A, B, C, ………, Y, Z.
a, b, c, ………, y, z.
‘C’ is case sensitive language, it means lower and upper case are different.

Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Special Characters

  • , → Comma
  • % → Percent sign
  • . → Period
  • ? → Question mark
  • ; → Semicolon
  • & → Ampersand
  • : → Colon
  • ^ → Caret
  • # → Number sign (Hash)
  • * → Asterisk
  • ‘ → Apostrophe
  • – → Minus sign
  • ” → Quotation mark
  • + → Plus sign
  • ! → Exclamation mark
  • / → Slash
  • | → Vertical bar
  • \ → Backslash
  • ~ → Tilde
  • < → Opening angle bracket
  • > → Closing angle bracket
  • _ → Underscore
  • $ → Dollar sign
  • ( → Left parenthesis
  • ) → Right parenthesis
  • [ → Left bracket
  • ] → Right bracket
  • { → left brace
  • } → Right brace

Question 2.
What is a variable?
Answer:
Variable: A variable is an entity that has a value and is known to the program by a name. A variable definition associates a memory location with the variable name. A variable can have only one value assigned to it at any given time during the execution of the program. Its value may change during the execution of the program.
Example:
a = 20, 5;
b = 10;
here, a and b are variables.

Variable names are identifiers used to name variables. They are symbolic names given to memory locations. A variable name consists of a sequence of letters and digits, the first character being a letter. The rules that apply to identifiers also apply to variable names.
Examples of valid variable names are:
a → Class
abcl → employee
Stu-data → 12 MAX

Examples of invalid variable names are:
a’s → illegal character (’)
Stu data → blank space not allowed
2Max → first character should be a letter
Classmarks → comma not allowed

Question 3.
WAP to input 10 numbers and print its sum.
Answer:

#include<stdio.h>

#include<conio.h>

void main ()

int N, SUM = 0, i;

clrscr () ;

for (i=1; i <=10; i++)

{

printf (“Enter a number”);

scanf (“% d”, & N);

SUM = SUM + N;

printf (“\n The sum often numbers is % d”, SUM);

getch ();

}

Question 4.
WAP to find the sum of following series:
Answer:
Sum = [latex]\frac { 1 }{ 1 } +\frac { 1 }{ 2 } +\frac { 1 }{ 3 } +……….+\frac { 1 }{ n } [/latex]

#include<stdio.h>

#include<conio.h>

void main ()

{

int n, i;

float sum;

printf (“Enter the number of terms\n”);

scanf (“%. d”, & n);

i = 1;.

do

{

sum = sum + 1 /i;

i = i + 1;

}

while (i <= n);

printf (“% f\ sum);

getch ();

}

Question 5.
WAP to calculate factorial or any given number.
Answer:

#include<stdio.h>

#include<conio.h>

void main( )

{

int n, i;

long int fact = 1;

clrscr ();

printf(“\n Enter any number”);

scanf(“%d”, & n);

for (i=1; i < = n; i + +)

{

fact = fact *i;

}

printf (“The factorial of % d is % 1d”, n, fact);

getch ();

}

Question 6.
WAP to generate the following series:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Answer:

#include<stdio.h>

#include<conio.h>

void main ( )

{

int i, j;

clrscr ();

for (i=1; i <= 5; i + +)

{

for (j=1; j <= i; j++)

{

printf (“% d\t”, i);

}

printf (“\n”);

}

getch ();

Introduction to ‘C’ Very Short Answer Type Questions (2 Marks)

Question 1.
From where the compilation of the program starts?
Answer:
from main () function.

Question 2.
% operator returns what?
Answer:
Remainder.

Question 3.
Who developed ‘C’ language?
Answer:
Dennis Ritchie and Ken Thompson.

Question 4.
A loop within a loop is known as ……….
Answer:
Nested loop.

Question 5.
Which data type consists of fractional values?
Answer:
float.

Introduction to ‘C’ Objective Type Questions (1 Marks)

There are four alternative answers for each part of the questions. Select the correct one and write it in your answer-book.

Question 1.
A variable name must start with:
(a) A number
(b) An alphabet
(c) $ symbol
(d) # sign.
Answer:
(b) An alphabet

Question 2.
% s is used for:
(a) Character data type
(b) Numeric data type
(c) String data type
(d) Float data type.
Answer:
(c) String data type

Question 3.
‘!=’ is a/an:
(a) Arithmetical operator
(b) Relational operator
(c) Logical operator
(d) Assigning operator.
Answer:
(b) Relational operator

Question 4.
Which header file is used for scanf () and printf ()?
(a) conio.h
(b) stdio.h
(c) math.h
(d) Both (a) and (b).
Answer:
(d) Both (a) and (b).

Question 5.
Which of them is not a looping statement?
(a) for
(b) while
(c) if
(d) do-while.
Answer:
(c) if

Leave a Comment

Your email address will not be published. Required fields are marked *