Monday, April 11, 2016

C Building Blocks



C Building 
In any language there are certain building blocks:
• Constants
• Variables
• Operators
• Methods to get input from user(scanf( ), getch( ) etc.)
• Methods to display output (Format Specifier, Escape Sequences etc.) etc.
Format Specifiers
Format Specifiers tell the printf statement where to put the text and how to display the text. The various format specifiers are:
%d => integer
%c => character
%f => float etc.
Variables and Constants
If the value of an item is to be changed in the program then it is a variable. If it will not change then that item is a constant. The various variable types (also called data type) in C are:
int, float, char, long ,double etc they are also of the type signed or unsigned.
Escape Sequences
Escape Sequence causes the program to escape from the normal interpretation of a string, so that the next character is recognized as having a special meaning. The back slash “\” character is called the Escape Character”. The escape sequence includes the following:
\n => new line
\t => tab
\b => back space
\r => carriage return
\” => double quotations
\\ => back slash etc.
Taking Input From the User
The input from the user can be taken by the following techniques: scanf( ), getch( ), getche( ), getchar( ) etc.

Operators
There are various types of operators that may be placed in three categories:
Basic: + - * / %
Assignment: = += -= *= /= %=
(++, -- may also be considered as assignment operators)
Relational: < > <= >= == !=
Logical: && , || , !
EXERCISE
1. Write a program which shows the function of each escape sequence character.
eg printf(“alert ring bell rings like \a\a\a\a\a\a\a\a\a\a\a\a\a\a”);
printf(“the tab is inserted like \t this”);etc
Ans:
Output:
(i)  With pop sound, alert ring bell like
(ii) the tab is inserted like this

2. Write down C statements to perform the following operations:
i. z = 4.2((x+y)5/z – 0.52x/(y+z))/(x+y)2
Ans :
(i)4.2*((x+y)*5/z-0.52*x/(y+z))/(x*x+2*x*y+y*y)
ii. x = a2 +2ab+b
Ans :
x=a*a+2*a*b+b*b
3. What will be the output of the mix mode use of integers and float.
a=5/9;
b=5.0/9;
printf(“%f,%f”,a,b);
Ans:
       Output:
       a=0.000000
       b =0.555556

4. What will be the output if a=5,
(i) printf(“%d”,++a);
(ii) printf(“%d”,a++);
Ans:
Output:
      (i)  a=6
      (ii) b =5

5. Write an output of the following program.
i. #include<stdio.h>

void main()
      {
      int a,b,c,d;
      float x,y,z,l;
     
char ch;
      printf("\nThe value of a=\n");
      scanf("%d",&a);

      printf("\nThe value of b=\n");
      scanf("%d",&b);

      printf("\nThe value of c=\n");
      scanf("%d",&c);
     
      printf("\nThe value of d=\n");
      scanf("%d",&d);

      x=a+b;
      y=a-b;
      z=a*b;
      l=a*b*c*d;

      printf("\na + b = %f",x);
      printf("\na - b = %f",y);
      printf("\na * b = %f",z);
      printf("\n\na*a*c*d = %f",l);
      }

Ans:
Output:
The value of a=1
The value of b=2
The value of c=3
The value of d=2
a + b =3
a - b= -1
a * b= 2
a*b*c*d=12

ii. #include<stdio.h>
void main()
{
int i,j,k;
i=5;
j=10;
k=15;
printf(“\n%d %d %d”,i,j,k);
i++;
j++;
k++;
printf(“\n%d %d %d”,i,j,k);
printf(“\n%d %d %d”,i++,j++,k++);
printf(“\n%d %d %d”,i,j,k);
}
Ans:
Output:
7
12
17

No comments:

Post a Comment