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

Worksheet for CS3

Exercise 1: This exercise demonstrates the basic datatypes

Task 1.1 :Refer to the program “Illustration of datatypes.c”

/* This program “Illustration of datatypes.c” demonstrates fundamental data types in C : char, int, float
and double */

#include <stdio.h>
main()
{
char ch = 'a' ;
int num = 1234;
float f_num = 12.56;
double d_num = 1234.0;

printf ( "character is %c \n", ch);


printf ( "Num is %d\n", num);
printf ( "float number is %f\n", f_num);
printf ( "double number is %lf\n", d_num);

}
Compile and execute the program and observe the output.

Task 1.2: Change the declaration of various variables as follows :


char ch = 97 ;
int num = ‘a’;
float f_num = 12;
double d_num = 1234;
Compile and execute the program and observe the output.

Task 1.3: Write a program to find the ASCII equivalent of ‘a’ –‘z’, ‘A’-‘Z’ and ‘1’ – ‘9’

Task 1.4: Consider the following program:


#include <stdio.h>
main()
{
short num1 = 32767;
unsigned short num2 = 32767;

printf ( "Num1 is %d\n", num1);


printf ( "Num2 is %d\n", num2)
}
Compile and execute the program and observe the output.
Next set num1 and num2 to 32768 and observe the result.
Worksheet for CS3

Task 1.4: Write a program to find the size of each int data type.

Exercise 2: This exercise demonstrates the use of modifiers. Program to be used is “modifier.c”

Write a program “Size_of_datatypes.c” to find the size of each data type.

Exercise 3: This exercise demonstrates assignment statement : Program to be used is


“assignment_statement.c”

Execute 3.1, 3.2, 3.3 and 3.4, observe the results.

Exercise 4: This exercise demonstrates the use of printf() and scanf() functions. Program to be
used is “Print_scan.c”

Task 1.1 : Write a program to display "welcome to SAP LABS"


printf("welcome to SAP LABS");
printf("\"welcome to SAP LABS\"");

Task 1.2:
printf("\"welcome to SAP LABS\b\b\b\b\"");
Task 2: What is the output for the following code fragments?
Code 1:
printf ("Welcome\n");
printf ("to \n");
printf ("SAP");

Code 2:
printf ("C = A\/B");

Code 3:
printf ("Welcome\b\b\b");
printf ("to\b");
printf ("SAP");
Code 4:
printf ("Welcome\b\b\b\n");
printf ("to\b\n");
printf ("SAP");

Task 3: What is the out of following code fragments


code 1:
int a;
printf("Enter some integer number\n");
scanf("%d",a);
printf("The value of a = %d", a);
Worksheet for CS3

Code 2:
int a;
printf("Enter some integer number\n");
scanf("%d",a);
printf("The value of a = %d", a);

Task 4: Declare two integer variables and find out the address where they are stored in the
memory
int a = 10, b = 30;
printf ("a = %d, b = %d\n", a, b );
printf ("address of a = %d, b = %d\n", &a, &b );

Task 5: Write a program to find hexadecimal equivalent of decimal number

Exercise 6: This exercise demonstrates 3 types arithmetic modes :


Integer mode arithmetic statement
Real mode arithmetic statement
Mixed mode arithmetic statement.
Program to be used is “Arithmetic_mode.c”

Task 1: What is the output for the following code fragments?


code 1:
int x = 10, y = 20, z;
float a = 10.5, b = 20, c;

z = x + y + 30;
printf("z = %d\n", z);
z = y / x;
printf("z = %d\n", z);
z = y/6;
printf("z = %d\n", z);

z = a + b + 30;
printf("z = %d\n", z);
Code 2:
int x, y = 10;
int z = 'a';
x = y + z;
printf("x = %d", x);

Exercise 7: Illustrates unary, binary and ternary operator. Program to be used is


Binary_Unary_Ternary.c
Task 1:
Worksheet for CS3

int result, data1 = 10, data2 = 20;


result = data1 + data2;

printf("result = %d", result);

Task 2:
int result, data1 = 0x1234, data2 = 0x8ABC;
result = data1 + data2;

printf("result = %d", result);

Change operators to |, &, || and &&

Task 3: Unary operands


int result, data1 = -10;
result = -data1 ;

printf("result = %d", result);

Demonstrate the use of -, + and !

Task 4: Ternary operator


int result = 0;

(result > 0 )? printf("result is greater than zero"): printf("result is less than or equal to
zero");

Task 5 : Write a program to find whether the given number is odd or even.
Exercise 8 : This exercise demonstrates shorthand notation
Task 1: what is the output for the following program?
Code 1:
int x = 20;
x-=10;
printf("%d",x);

Code 2:
int x = 20;
x=-10;
printf("%d",x);

Code 3:
int x = 20;
Worksheet for CS3

x= -10;
printf("%d",x);

Exercise 9 : This exercise demonstrates precedence and associativity of operators.


Task 1: What is the output for the following program?
Code 1:
int x;
x = 2 - 2^3*2;
printf("x = %d\n", x);

x = 2/3*4^2 ;
printf("x = %d\n", x);

x = 1+2/3*4+5;
printf("x = %d", x);
Task 2: What is the output for the following program?
Code 1:
int k ,i = 10; j = 10;
k = ++i + ++j;
printf (" i = %d, j = %d, k = %d", i, j, k);

Code 2:
int k ,i = 10; j = 10;
k = ++i + ++i;
printf (" i = %d, j = %d, k = %d", i, j, k);

Code 3:
int k ,i = 10; j = 10;
k = i++ + j++;
printf (" i = %d, j = %d, k = %d", i, j, k);

Code 4:
int k ,i = 10; j = 10;
k = ++i + j++;
printf (" i = %d, j = %d, k = %d", i, j, k);

You might also like