You are on page 1of 10

C-Programming

Homework-1

Submitted By:

Name: Suraj Kumar Singh

Section: E - 3004

Roll_No: RE3004B77

Group: 2

Course_code: 1604

Date_Of_Submission: 31/08/2010

Submitted To:

Miss Richa Malhotra


PART ‘A’

Q1. Explain the difference between associativity and precedence


with the help of an example?

Ans : -
Precedence: Precedence is the priority for grouping different types of
operators with their operands.

a+b*c/d

The ‘*’ and ‘/’ operations are performed before ‘+’ because of precedence

b is multiplied by c before it is divided by d because of associativity.

Associativity: Associativity is the left-to-right or right-to-left order for


grouping operands to operators that have the same precedence.

For example, in the following statements, the value of 5 is assigned to both


a and b because of the right-to-left associativity of the = operator. The value
of c is assigned to b first, and then the value of b is assigned to a.

b = 9;
c = 5;
a = b = c;
Q2. Differentiate between Implicit and Explicit Type Conversions?

Ans: -
Implicit Type Conversion: Implicit conversions do not require any
operator. They are automatically performed when a value is copied to a
compatible type.
Example:

double d;
long l;
int i;

if (d > i) d = i;
if (i > l) l = i;
if (d == l) d *= 2;

Explicit Type Conversions: Explicit type conversion is a type conversion


which is explicitly defined within a program
Example:

Int a, b, c;
Float avg;
Avg= (float) (a+b+c)/3;
Q3. Differentiate between the following
1. Prefix and Postfix increment operator.
2. Assignment and Equal To operator.
3. Expression and Statement
4. Identifiers and Keywords.
5. Signed and Unsigned Integer.

1.
Prefix: The prefix form is when we put the operator before the variable
containing the number. If we use the prefix form then the number is
incremented before it is evaluated
Example:
Int n=1, a;
a=++n;
printf (“%d”, a); Output: 2

Postfix: The postfix form is when we put the operator after the variable
containing the number. If we use the postfix form then the number is
incremented after it is evaluated
Example:
Int n=1, a;
a=n++;
printf (“%d”, a); Output: 1
2.
Assignment Operator: The Assignment Operator evaluates an expression
on the right of the expression and substitutes it to the value or variable on
the left of the expression. It is denoted by ‘=’.
Example:
Int a=5, b=6, x;
X=a+b;
Here the value of a + b is evaluated and substituted to the variable x.

Equal to Operator: The Equal to operator is denoted by ‘==’ it compare


between two or more variables and values that is equal or not.
Example:
Int a=5;
If(a%2==0)
Printf(“Even”);
Else
Printf(“Odd”);
Here the reminder of a is check it is equal to 0 or not.

3.

Expressions: An expression is a combine of operators, constants, variables


and function call. The expression can be arithmetic, logical or relational.
Examples:
X+Y - Arithmetic expression.
A==B - Logical expression.
Func(a,b) - Function call.
Statements: A statement is an executable part of the program and causes
the computer to carry out some action.
Categorized of statements:

i) Expression Statement. (x=5)


ii) Compound Statement. (within a brasses)
iii) Selection Statement. (if, if….else, switch)
iv) Iterative Statement. (for, while, do…while)
v) Jump Statement. (goto, continue, break)

4.

Identifiers: Identifiers are user define words that are used to give names
to entities like, variable, array, functions, structure, etc.
Rules for naming identifiers:

i) First character should be an alphabet.


ii) The name should not be a keyword.
iii) The identifiers are generally meaningful names.

Keywords: There are certain words that are reserved for doing special
tasks. These words are known as keywords and they have standard,
predefined meaning in C. They are always written in lowercase.
Types :
i) Standard: There are 32 standard keywords.
auto, char, int, float, .……… , etc
ii) Optional: There are 8 optional keywords.
Ada, asm, entry, pascal, fast, near, far,use
5. Signed & unsigned Integer:
Unsigned: When the qualifier ‘unsigned’ is used the number is always
positive, it’s range is 0 to 65535 and it stored 2 bytes.

signed: When the qualifier ‘signed’ is used the number may be positive or
negative, it’s range is -32768 to 32767 and it also stored 2 bytes.

Example:
#include <stdio.h>

void main()
{
short int i;
short unsigned int j;

j = 60000;
i =j;

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

PART ‘B’

4. Write a program that will examine each character in a character


type array and write the ASCII equivalent of each character. Size
of the array is known in advance by the integer variable n.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
char ch[50];
clrscr();
printf("Enter size of array(MAX 50): ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
ch[i]=getchar();
}
printf("\n\n");
for(i=1;i<=n;i++)
{
printf("%c\t%d\n",ch[i],ch[i]);
}
getch();
}

5. Write a program to display the day of week according to the day


no. Entered by the user.

#include<stdio.h>
#include<conio.h>
Void main()
{

int day;
Clrscr();

Printf(“Enter no of days: “);


Scanf(“%d”,&,day);
Switch(day)
{
Case 1:
Printf(“Sunday”);
Break;
Case 2:
Printf(“Monday”);
Break;
Case 3:
Printf(“Tuesday”);
Break;
Case 4:
Printf(“Wednesday”);
Break;
Case 5:
Printf(“Thrusday”);
Break;
Case 6:
Printf(“Friday”);
Break;
Case 7:
Printf(“Saturday”);
Break;
Default:
printf(“Invalid input”);

getch();
}

6. Write a program that receives a multi-word string and prints it in


the form of one word per line.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int k,i;
char str[50];
printf("Enter str: ");
gets(str);
k=strlen(str);
for(i=0;i<k;i++)
{
if(str[i]==' ')
printf("\n");
else
printf("%c",str[i]);
}
getch();
}

You might also like