You are on page 1of 3

Assignment #2

ITE 13 Intermediate Programming


1st Semester of AY 2023-2024
Due: October 4, 2023, at 08:00 AM

NAME: PRINCESS BEVERLY R. ALA-AN


SECTION: BJ1
1. What will be the output of the given program:

Answer:

t organized! learn C!!


Get organized! learn C!!
Get organized! learn C!!
t

Explanation:

t organized! learn C!!


- Since we point to the index 2 indicating that the compiler should start printing at the
character where its index is 2 in the ‘s’ array which is ‘t’.

Get organized! learn C!!


- We did not point to anything here but rather print the entire string stored in the s
array. When the ‘s’ array argument is being printed, it automatically starts from the
beginning of the string which has an index[0] and prints the entire string.

Get organized! learn C!!


- Here, we point to the ‘s’ array itself with no specific index which will print the entire
string as it is treated as a string, therefore, prints from first character index[0], so it
prints the entire string.
t
- This gets printed because of the format specifier %c for a single character with its
index s[2] referring to character ‘t’ at index[2] in the ‘s’ array.
2. Point out the errors, if any, in the following program:

Answer:
Output: It runs but there is no output
Explanation:
The code should have been like this:
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "United";
char str2[] = "Front";
char str3[20];

strcpy(str3, str1); // Copy str1 to str3


strcat(str3, str2); // Concatenate str2 to str3

printf("%s\n", str3);

return 0;
}

Where we declare arrays of strings to store the string United and Front, and declare another
array of string to append the overall strings. Where in the line ‘strcpy(str3,
str1);’ the string at ‘str1’ array is being copied by array ‘str3’ and appended
‘str2’ to the end of ‘str3’, which will then print ‘str3’ which is ‘United
Front’.
3. If the string "Alice in wonder land" is fed to the following scanf( ) statement,
what will be the contents of the arrays str1, str2, str3, and str4?
scanf( "%s%s%s%s", str1, str2, str3, str4 );

Answer:

str1: "Alice"

str2: "in"

str3: "wonder"

str4: "land"

4. Suppose that we call scanf( ) as follows:


scanf( "%d%s%d", &i, s, &j);
If the user enters 12abc34 56def78, what will be the values of i, s, and j after the call?
Assume that i and j are int variables and s is an array of characters.
i=12
s = abc34
j = 56

and def78 will not be stored to any of the variables.

You might also like