Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In LINUX by using c++ or c language 1) Explain the result for following code x=\

ID: 3737032 • Letter: I

Question

In LINUX by using c++ or c language

1) Explain the result for following code

   x='a'; y=98;

   printf("%d %c %d %c ", x, x, y, y);

   char * x1 = "hello";

   printf("%s %p %s %p ", x1, x1, "hello", "hello");

2) How to declare a character array with 128 rooms. Store 0 to 127 in this array and print the corresponding character for each ascii code in the array.

3) Write a program that keeps reading a string, displaying its length, making a copy of it, and checking whether it is "hello". If the input string is "hello", the program stops.

           Enter a string

           hi

           You entered hi. length=2

           After making a copy. x:hi y:hi

           No it is not hello

           Enter a string

           hello

           You entered hello. length=5

           After making a copy. x:hello y:hello

           Yes it is hello

4) A string constant such as "hello" is an address. Explain the result of following code.

     char *x, *y, *z;

     x="hello"; y="hi"; z="bye";

     printf("%s %s %s ", x, y, z);

     printf("%p %p %p ", x, y, z);

5) Try below and explain why we have an error.

     char x[20];

     strcpy(x, "hello"); // this is ok

     x="hello"; // this is an error

Explanation / Answer

Answer 1)

char x = 'a'; int y = 98;

Character variable x stores constant character ‘a’.

Integer variable y stores constant value 98

%d for integer

%c for character

printf("%d %c %d %c ", x, x, y, y);

First output for variable x will print: 97, which is the ASCII value of constant character ‘a’, because format character %d is for integer.

Second output for variable x will print: a, which is the character value stored in variable x, because format character %c is for character.

Third output for variable y will print: 98 which the integer value stored in the variable y, because format character %d is for integer.

Fourth output for variable y will print: b, which is the character representation of ASCII value 98 stored in variable y, because format character %c is for character.

char * x1 = "hello";

Character pointer x1 will contain the starting address allocated string constant “hello”. Assume the address is: 00403031.

%s for string

%p for ponter

printf("%s %p %s %p ", x1, x1, "hello", "hello");

First output for variable x1 will print: hello, which is the contents of pointer x1, because format character %s is for string.

Second output for variable x1 will print: 00403031, which is the address stored in variable x1, because format character %p is for pointer.

Third output will print: hello, because format character %s for string.

Fourth output will print: 00403031, which is the address of the constant “hello”, because format character %p is for pointer.

Output:

97 a 98 b

hello 00403031 hello 00403031

------------------------------------------------------

Answer 2)

#include<stdio.h>

// main function definition
int main()
{
// Loop variable
int c;
// Character array of size 128 to store 0 to 127
char array[128];
// Loops till 127
for(c = 0; c < 128; c++)
// Stores the counter variable value c in array c index position
array[c] = c;
// Loops till 127
for(c = 0; c < 128; c++)
// Displays the ASCII value with character form of the ASCII value
printf(" ASCII value = %d Character = %c", array[c], array[c]);
}// End of main function

Sample Output:

ASCII value = 0 Character =
ASCII value = 1 Character =
ASCII value = 2 Character =
ASCII value = 3 Character =
ASCII value = 4 Character =
ASCII value = 5 Character =
ASCII value = 6 Character =
ASCII value = 7 Character =
ASCII value = 8 Character =
ASCII value = 9 Character =
ASCII value = 10 Character =

ASCII value = 11 Character =
ASCII value = 12 Character =
ASCII value = 13 Character =
ASCII value = 14 Character =
ASCII value = 15 Character =
ASCII value = 16 Character =
ASCII value = 17 Character =
ASCII value = 18 Character =
ASCII value = 19 Character =
ASCII value = 20 Character =
ASCII value = 21 Character =
ASCII value = 22 Character =
ASCII value = 23 Character =
ASCII value = 24 Character =
ASCII value = 25 Character =
ASCII value = 26 Character =
ASCII value = 27 Character =
ASCII value = 28 Character =
ASCII value = 29 Character =
ASCII value = 30 Character =
ASCII value = 31 Character =
ASCII value = 32 Character =
ASCII value = 33 Character = !
ASCII value = 34 Character = "
ASCII value = 35 Character = #
ASCII value = 36 Character = $
ASCII value = 37 Character = %
ASCII value = 38 Character = &
ASCII value = 39 Character = '
ASCII value = 40 Character = (
ASCII value = 41 Character = )
ASCII value = 42 Character = *
ASCII value = 43 Character = +
ASCII value = 44 Character = ,
ASCII value = 45 Character = -
ASCII value = 46 Character = .
ASCII value = 47 Character = /
ASCII value = 48 Character = 0
ASCII value = 49 Character = 1
ASCII value = 50 Character = 2
ASCII value = 51 Character = 3
ASCII value = 52 Character = 4
ASCII value = 53 Character = 5
ASCII value = 54 Character = 6
ASCII value = 55 Character = 7
ASCII value = 56 Character = 8
ASCII value = 57 Character = 9
ASCII value = 58 Character = :
ASCII value = 59 Character = ;
ASCII value = 60 Character = <
ASCII value = 61 Character = =
ASCII value = 62 Character = >
ASCII value = 63 Character = ?
ASCII value = 64 Character = @
ASCII value = 65 Character = A
ASCII value = 66 Character = B
ASCII value = 67 Character = C
ASCII value = 68 Character = D
ASCII value = 69 Character = E
ASCII value = 70 Character = F
ASCII value = 71 Character = G
ASCII value = 72 Character = H
ASCII value = 73 Character = I
ASCII value = 74 Character = J
ASCII value = 75 Character = K
ASCII value = 76 Character = L
ASCII value = 77 Character = M
ASCII value = 78 Character = N
ASCII value = 79 Character = O
ASCII value = 80 Character = P
ASCII value = 81 Character = Q
ASCII value = 82 Character = R
ASCII value = 83 Character = S
ASCII value = 84 Character = T
ASCII value = 85 Character = U
ASCII value = 86 Character = V
ASCII value = 87 Character = W
ASCII value = 88 Character = X
ASCII value = 89 Character = Y
ASCII value = 90 Character = Z
ASCII value = 91 Character = [
ASCII value = 92 Character =
ASCII value = 93 Character = ]
ASCII value = 94 Character = ^
ASCII value = 95 Character = _
ASCII value = 96 Character = `
ASCII value = 97 Character = a
ASCII value = 98 Character = b
ASCII value = 99 Character = c
ASCII value = 100 Character = d
ASCII value = 101 Character = e
ASCII value = 102 Character = f
ASCII value = 103 Character = g
ASCII value = 104 Character = h
ASCII value = 105 Character = i
ASCII value = 106 Character = j
ASCII value = 107 Character = k
ASCII value = 108 Character = l
ASCII value = 109 Character = m
ASCII value = 110 Character = n
ASCII value = 111 Character = o
ASCII value = 112 Character = p
ASCII value = 113 Character = q
ASCII value = 114 Character = r
ASCII value = 115 Character = s
ASCII value = 116 Character = t
ASCII value = 117 Character = u
ASCII value = 118 Character = v
ASCII value = 119 Character = w
ASCII value = 120 Character = x
ASCII value = 121 Character = y
ASCII value = 122 Character = z
ASCII value = 123 Character = {
ASCII value = 124 Character = |
ASCII value = 125 Character = }
ASCII value = 126 Character = ~
ASCII value = 127 Character =

----------------------------------------------------------------

Answer 3)

#include<stdio.h>
#include<string.h>
#define MAX 100

// main function definition
int main()
{
// Declares two array of size MAX
char x[MAX];
char y[MAX];
// Loops till user enters string "hello"
do
{
// Accepts a string from the user
printf(" Enter a string: ");
scanf("%s", x);
// Displays the entered string and its length. Length is displayed
// by using built in function strlen() of string.h header file
printf(" You entered %s. length = %d", x, strlen(x));
// Copies the second parameter string to first parameter string
// by using built in function strcpy() of string.h header file
strcpy(y, x);
// Displays the entered string and copies string
printf(" After making a copy. x: %s y: %s", x, y);
// Checks if the entered string is "hello" or not
// by using built in function strcmp() of string.h header file
// If both the strings are equal then the function will return zero
if(strcmp(x, "hello") == 0)
{
// Displays the message
printf(" Yes it is hello");
// Come out of the loop
break;
}// End of if condition
// Otherwise, enter string is not "hello"
else
printf(" No it is not hello");
}while(1); // End of do - while
}// End of main function

Sample output:

Enter a string: this

You entered this. length = 4
After making a copy. x: this y: this
No it is not hello
Enter a string: checkit

You entered checkit. length = 7
After making a copy. x: checkit y: checkit
No it is not hello
Enter a string: hello

You entered hello. length = 5
After making a copy. x: hello y: hello
Yes it is hello

---------------------------------------------------------

Answer 4)

// Three character pointer declared

char *x, *y, *z;

// Assigns constants to the pointers

// Stores the starting address of the string constants

x="hello"; y="hi"; z="bye";

%s format character for string

printf("%s %s %s ", x, y, z);

First result for variable x is: hello, because variable x content is string constant “hello”, %s is for string.

Second result for variable y is: hi, because variable y content is string constant “hi”, %s is for string.

Third result for z is: bye, because variable z content is string constant “bye”, %s is for string.

%p format character for pointer which will display the address

Assume the starting address of string constant “hello” is: 00403024

Assume the starting address of string constant “hi” is: 0040302A

Assume the starting address of string constant “bye” is: 0040302D

printf("%p %p %p ", x, y, z);

First result for variable x is: 00403024, which is the address stored in pointer variable x, %p is for pointer.

Second result for variable y is: 0040302A, which is the address stored in pointer variable y, %p is for pointer.

Third result for variable z is: 0040302D, which is the address stored in pointer variable Z, %p is for pointer.

Output:

hello hi bye

00403024 0040302A 0040302D

-----------------------------------------------------------

Answer 5)

// Declares a character array of size 20

char x[20];

// Built in function strcpy() of string.h header file will copy the contents of the second parameter to the first parameter.

// strcpy() takes two pointer type parameter and return a character pointer
// Syntax: char * strcpy(char *destination, char *source);

// As we know array name is a pointer to the starting to the starting index position.

// So there is no error

strcpy(x, "hello"); // this is ok

// “hello” is a string constant and it is assigned to an array.

// Character array can store data one character at a time using index position, but string constant will give the starting address given by the compiler which is not compatible,

// So it will generate an error

x="hello"; // this is an error

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote