Friday, December 2, 2016

Answer - Identify errors in C program code, correct it and predict the output.

Answer: Corrected C program with Output.

Program-1

#include<stdio.h>
int main()
{
            char str1[10]="ABCDE", str2[10]="XYZAB";
            printf("String1:%c\n", str1[2+1]);
            printf("String2:%s\n", str2);
            printf("String1 = %d \n", strlen(str1));
            printf("String2 = %d \n", strlen(str2));
            return 0;
}

Output of the program:
String1:D
String2:XYZAB
String1 = 5
String2 = 5

Program-2

#include<stdio.h>
void main() {
            int i, max, marks[5]={5,2,3,6,8};
            max = marks[0];
            for(i=0; i<5; i++)
            {
                        if(marks[i] < marks[i+1])
                                    max = marks[i+1];
            }
            printf("Max marks = %d \n", max);
}

Output of the program:
Max marks = 8

Program-3

#include<stdio.h>
void main() {
            char name[10]="ABCEDFGH";    
            name[0] = 'A';    
            name[2] = 'H';
            name[0] = name[2];
            printf("%c %d", name[0], name[1]);   
}

Output of the program:
H 66


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete