1 There are two kinds of variables in c programs those to st

1. There are two kinds of variables in c programs: those to store _______, and those to store ________.

2. List some common primitive data types in c:

3. Explain why data type is important for a computer to correctly interpret stored information in the main memory.

4. Give the prototype of malloc function:

5. Why the return value which is an address of malloc function call is usually casted to a specific address type?

6. Assume that MAX_SIZE is a constant defined in your c program: #define MAX_SIZE 100. Write a statement to define a struct type that has the following fields: an int field, a double field, a char field, and an array field of MAX_SIZE entries, each entry of the array can store an integer value. You may name the field names of your choice.

7. Declare a variable of the struct type defined in 6.

8. Declare a pointer variable and assign to it the address of the variable declare in 7.

9. Write statements to store the following info in the structure using the variable declared in 7.

10

3.14

‘A’

0, 1, 2, …, MAX_SIZE-1

10. Redo 9. Use the pointer variable declared in 8.

11. Trace the following code. What will be displayed?

int x = 5;

int *ptr_x;

ptr = &x;

*ptr_x = x + x;

printf(\"%d\", x);

Solution

1. There are two kinds of variables ( Numeric and Characterstic variables) in c programs: those to store ____NUMBERS____, and those to store __CHARACTERS______.

2. Primitives data types in C:-

a) int (Numeric-Integer)
b) short (Numeric-Integer)
c) long(Numeric-Integer)
d) float(Numeric-Real)
e) double(Numeric-Real)
f) char(Character)


3.The data type is important for a computer to correctly interpret stored information in the main memory because it helps in determining the way of the storage of the data in the main memory.It determines the variables characterstics. Different data types have different way of allocating the requirements i.e. diffrenet data ypes use different amount of bytes. So for specific information that is stored in particular variable, proper data type is required.

4. Prototype of malloc function:

void *malloc(size_t size);

5. There is no need to cast a return value of malloc in C as the return type is void *.
But for C++, casting is required as it allows the programs to compile as c++ and also helps in identifying the inconsistencies for developer in type sizing.

6. #define MAX_SIZE 100
struct size_demo {
       int field_1;
       double field_2;
       char field_3;
       int field_4[MAX_SIZE];
   };

7. struct size_demo s1;

8. struct size_demo *s2;
s2 = &s1;

9. s1.field_1 = 10;
s1.field_2 = 3.14;
s1.field_3 = \'A\';
int i;
for(i=0; i<MAX_SIZE; i++)
s1.field_4[i] = i;

10. (*s2).field_1 = 10;
(*s2).field_2 = 3.14;
(*s2).field_3 = \'A\';
int i;
for(i=0; i<MAX_SIZE; i++)
(*s2).field_4[i] = i;

11. int x = 5;
int *ptr_x;
ptr = &x;
*ptr_x = x + x;
printf(\"%d\", x);

The output will be 10 because ptr_x have address of x. so the value of ptr_x is modified means the value at the address of ptr_x is modified which is none other than address of x.

1. There are two kinds of variables in c programs: those to store _______, and those to store ________. 2. List some common primitive data types in c: 3. Explai
1. There are two kinds of variables in c programs: those to store _______, and those to store ________. 2. List some common primitive data types in c: 3. Explai

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site