Sindh Madressatul Islam University
Faculty of Computer Science
1st Semester Fall 2018 Class 1A,1B,1C,1D
Course: Programming Fundamentals CS-103
Faculty Name: Syeda Nazia Ashraf
Assignment 1 Solution
1. What do you mean by variable definition and variable assignment? Give examples. 1 mark
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization/assignment */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
Output:
value of c : 30
value of f : 23.33333
2. Differentiate between \r , \n , \t , \f 1 mark
\r code moving the cursor to the start of the same line, without moving down to the next line.
\n code moving the cursor to the start of the next line
\t is used for horizontal tab(move 8 characters forward)
\f is form feed, page eject, page or section separator (move active position to the initial position of next
logical page)
3. What is the purpose of &. 1 mark
To input an integer or character data(but not the strings), you must use the address operator with the
variable and not just the variable name itself.
4. Differentiate between getch() and getche() with examples. 1 mark
These functions use to inputs a single character the instant it is typed without waiting for the enter key to
be [Link] get means “get some input” and ch means “it get a character”. The difference b/w two
characters is that the function getche() echoes(displays) the character that you typed to the screen (letter
e stands for echo) whereas getch() just returns the character that you typed without echoing it on the
screen.
e.g: ch = getch(); or ch = getche();
5. Write a program which takes user marks for 5 subjects and then calculate total marks obtained
by user. 2 marks
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,sum;
clrscr();
printf("\n Enter Subject 1 Marks : ");
scanf("%d",&s1);
printf("\n Enter Subject 2 Marks : ");
scanf("%d",&s2);
printf("\n Enter Subject 3 Marks : ");
scanf("%d",&s3);
printf("\n Enter Subject 4 Marks : ");
scanf("%d",&s4);
printf("\n Enter Subject 5 Marks : ");
scanf("%d",&s5);
sum = s1+s2+s3+s4+s5;
printf("\n\n THE SUMATION IS %d ",sum);
getch();
}
Output:
Enter Subject 1 Marks : 41
Enter Subject 2 Marks : 58
Enter Subject 3 Marks : 60
Enter Subject 4 Marks : 70
Enter Subject 5 Marks : 60
THE SUMATION IS 289
6. Write a program using == , ++ , % , <= operators. 1 marks
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int num =2;
printf("number = %d \n",num++);
printf("number = %d \n",num--);
printf("number = %d \n",num);
printf("Is number less than and equal to two? %d \n",num<=2);
printf("number divided by 2 is %d \n",num%2);
getch();
}
Output:
7. Differentiate between Backspace and Del key. 1 mark
Both the Del or Delete key and Backspace key are used to delete text. However, when dealing with text
pressing the del key deletes text to the right of the cursor and pressing the backspace key deletes text to
the left (backwards) of the cursor. For example, if you were to click and make the cursor at the end of
the example below text and press delete nothing will happen because there is no text to the right.
However, if you press the backspace key the end of the text would begin to be deleted.
8. Why not simply put decimal integer in original string as compare to format specifier? 1 mark
You can certainly do that ...
printf ("This is a number: 12345\n");
... but that does not have the same value as placing the value in a variable and converting the variable
into a string ...
int i = 12345;
printf ("This is a number: %d\n", i);
That's the whole point of format specifiers - to initiate a conversion from one place to another.
9. Differentiate between Typedef and #define. 1 mark
Typedef: Type Definition
The typedef operator is used for creating alias of a data type. E.g:
typedef int integer;
Now we can use integer in place of int i.e instead of declaring int a;, we can use:
integer a;
This is applied for structures too. e.g:
typedef struct {int age; char *name} person; person people;
#define: Preprocessor directive
You can define a macro in C using #define preprocessor directive.
A macro is a fragment of code that is given a name. You can use that fragment of code in your program
by using the name. For example,
#define c 299792458 // speed of light
Here, when we use c in our program, it's replaced by 3.1415.
You can also define macros that works like a function call, known as function-like macros. For example,
#define circleArea(r) (3.1415*r*r)
Every time the program encounters circleArea(argument), it is replaced
by (3.1415*(argument)*(argument)).
Suppose, we passed 5 as an argument then, it expands as below:
circleArea(5) expands to (3.1415*5*5)