# C Programming Exercises, Practice, Solution : Variable Type

## C Variable Type [18 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a C program that invokes the command processor to execute a command.

Expected Output :

```Is command processor available?
Command processor available!
Executing command DIR
00c40280-5e27-11e6-bd4f-71e8825f8ea3
01691610-41e1-11e6-901d-35b72ececc72
...........
ff827330-443a-11e6-9820-23e2f60d924e
file.txt
logging_example.out
test.txt
Returned value is: 0.
```

Click me to see the solution

2. Write a C program to convert a string to an unsigned long integer.

Test Data and Expected Output :

```Input an unsigned number: 25
Output: 25
```

Click me to see the solution

3. Write a C program to convert a string to a long integer.

Expected Output :

```In decimals: 2016, 4235440, -3624422, 5947391.
```

Click me to see the solution

4. Write a C program to convert a string to a double.

Expected Output :

```Output= 4.00
```

Click me to see the solution

5. Write a C program to generate a random number.

Test Data and Expected Output :

```Guess the number (1 to 10): 6
The number is higher

Guess the number (1 to 10): 7
That is correct!
```

Click me to see the solution

6. Write a C program to sort the elements of an array.

Test Data and Expected Output :

```Input the number of elements to be stored in the array :5
Input 6 elements in the array :
element - 0 : 15
element - 1 : 26
element - 2 : 42
element - 3 : 82
element - 4 : 35

After sorting the array are :
15
26
35
42
82
```

Click me to see the solution

7. Write a C program to calculate the integral quotient and remainder of a division.

Test Data and Expected Output :

```Input numerator : 2500
Input denominator : 235
quotient =  10, remainder = 150
```

Click me to see the solution

8. Write a C program to return the absolute value of a long integer.

Test Data and Expected Output :

```Input 1st number (positive or negative) : 25
Input 2nd number (positive or negative) : -125
The absolute value of 1st number is : 25
The absolute value of 2nd number is : 125
```

Click me to see the solution

9. Write a C program to get the environment string.

Expected Output :

```The set path is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
:/bin:/usr/games:/usr/local/games
```

Click me to see the solution

10. Write a C program to return the quotient and remainder of a division.

Test Data and Expected Output :

```Input numerator : 2000
Input denominator : 235
quotient =  8, remainder = 120.
```

Click me to see the solution

11. Write a C program to allocate a block of memory for an array.

Test Data and Expected Output :

```Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element 1 : 25
element 2 : 30
element 3 : 35
element 4 : 20
element 5 : 40
Values entered in the array are :
25  30  35  20  40
```

Click me to see the solution

12. Write a C program to perform a binary search in an array.

Test Data and Expected Output :

```Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 1 : 25
element - 2 : 20
element - 3 : 18
element - 4 : 13
element - 5 : 15
Input a value to search : 18
18 is found in the array.
```

Click me to see the solution

13. Write a C program to convert a string to an integer.

Test Data and Expected Output :

```Input a number : 1972
The value Input is 1972.```

Click me to see the solution

14. Write a C program to convert a string to a double.

Test Data and Expected Output :

```Input a number : 25
The original number is : 25.000000
After division by 2 the number is : 12.500000
```

Click me to see the solution

15. Write a C program to set a function that will be executed on termination of a program.

Expected Output :

```This is the message from main function.
Here is the message returning from newFunctionTwo.
Here is the message returning from newFunctionOne.```

Click me to see the solution

16. Write a C program to return the absolute value of an integer.

Test Data and Expected Output :

```Input a positive or negative number :-25
The absolute value of the given number is : 25```

Click me to see the solution

17. Write a C program to abort the current process.

Expected Output :

``` File does not exist or error, in opening the file.
timeout: the monitored command dumped core
Aborted
```

Click me to see the solution

18. Write a C program to demonstrate the working of the keyword long.

Expected Output :

``` The size of int = 4 bytes
The size of long = 8 bytes
The size of long long = 8 bytes
The size of double = 8 bytes
The size of long double = 16 byte
```

Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

﻿

## C Programming: Tips of the Day

Returning an array using C

You can't return arrays from functions in C. You also can't (shouldn't) do this:

```char *returnArray(char array []){
char returned ;
//methods to pull values from array, interpret them, and then create new array
return &(returned); //is this correct?
}
```

returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.

You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.

Dynamically allocate the memory inside of the function (caller responsible for deallocating ret)

```char *foo(int count) {
char *ret = malloc(count);
if(!ret)
return NULL;

for(int i = 0; i < count; ++i)
ret[i] = i;

return ret;
}
```

Call it like so:

```int main() {
char *p = foo(10);
if(p) {
// do stuff with p
free(p);
}

return 0;
}
```

Ref : https://bit.ly/3yFIeao

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook