- 2067 Asar · 8 marks
Correct program: character arrays
Short answer
Errors: ar1 size 11 with 12 initializers, missing null terminator; similar issue for ar3; return 0 missing ';'; extra '{'. Correct by increasing ar1 and ar3 sizes to at least 13. Output: Array 1 = IoE PULCHOWK, Array 2 = IoE, Pulchowk, Array 3 = IOE PULCHOWK.
Rewrite program correctly and write output of the given program written in C programming language below:
# include<stdio.h>
# include<conio.h>
# include<string.h>
void main( )
{
char ar1[11] = {'I','o','E',' ','P','U','L','C','H','O','W','K'};
char ar2[15] = "IoE, Pulchowk";
char ar3[11] = {{'I'},{'O'},{'E'},
{' '},{'P'},{'U'},{'L'},{'C'},{'H'},{'O'},{'W'},{'K'}};
clrscr( );
{printf ("\nArray 1 = %s\n", ar1);
printf ("\nArray 2 = %s\n", ar2);
printf ("\nArray 1 = %s\n", ar3);
return 0
}
Errors in the given program
char ar1[11]is given 12 initialisers (IoE PULCHOWKhas 12 characters). Excess initialisers are an error, and even a size of 12 would leave no space for the null character'\0', so%scould run past the array (undefined behaviour). The size must be at least 13.ar3has the same size problem. Also, the braces around every single character ({'I'}) are unnecessary; they are allowed only with a warning ("braces around scalar initializer").return 0has no semicolon, andvoid main()cannot return a value. Useint main()or remove thereturn.- The extra
{before the firstprintfis never closed, so the closing}ofmainis missing. conio.handclrscr()are Turbo C only (not standard C). They may be kept in Turbo C, but are removed here.- The third
printfprints the label "Array 1" forar3; it should be "Array 3".
(gcc -Wall -std=c99 on the original reports the excess elements, the braces warnings and the missing ;.)
Corrected program
#include <stdio.h>
int main(void)
{
char ar1[13] = {'I','o','E',' ','P','U','L','C','H','O','W','K'};
char ar2[15] = "IoE, Pulchowk";
char ar3[13] = {'I','O','E',' ','P','U','L','C','H','O','W','K'};
printf("\nArray 1 = %s\n", ar1);
printf("\nArray 2 = %s\n", ar2);
printf("\nArray 3 = %s\n", ar3);
return 0;
}
Output
Array 1 = IoE PULCHOWK
Array 2 = IoE, Pulchowk
Array 3 = IOE PULCHOWK
ar2 is "IoE, Pulchowk" (13 characters plus '\0' in a 15-element array, so the last element is also '\0'). ar1 keeps the small o, while ar3 has capital O in "IOE".
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Correct program: do-while and pow · Computer Programming, chapter 3
- Correct program: Celsius to Fahrenheit · Computer Programming, chapter 3
- Rewrite string characters alphabetically · Computer Programming, chapter 7
- Output of arithmetic expression program · Computer Programming, chapter 3
- Correct program: case array and average · Computer Programming, chapter 3
- Pattern: PULCHOWK using unformatted output · Computer Programming, chapter 4
- Rewrite sentences correctly (pronoun, concord) set 2082 · Communication English, chapter 3
- Rewrite sentences correctly set 2081 · Communication English, chapter 3
- Redundant network for IOE Pulchowk campus · Internet and Intranet, chapter 5
- Pyramid pattern output program · Object Oriented Programming, chapter 8