What will be output of following program segment:
A,B,C-9,12,3
X-A-B/3+C*2-1
Y-A-B/(3+C) (2-1) Z-A-(B/(3+C) 2)-1
print("X="X) print("Y="Y)
print("Z="Z)
Answers
Answer:
This chapter explains the features, technical details and syntaxes of the C programming language. I assume that you could write some simple programs. Otherwise, read "Introduction to Programming in C for Novices and First-time Programmers".
1. Introduction to C
C Standards
C is standardized as ISO/IEC 9899.
K&R C: Pre-standardized C, based on Brian Kernighan and Dennis Ritchie (K&R) "The C Programming Language" 1978 book.
C90 (ISO/IEC 9899:1990 "Programming Languages. C"). Also known as ANSI C 89.
C99 (ISO/IEC 9899:1999 "Programming Languages. C")
C11 (ISO/IEC 9899:2011 "Programming Languages. C")
C Features
[TODO]
C Strength and Pitfall
[TODO]
2. Basic Syntaxes
2.1 Revision
Below is a simple C program that illustrates the important programming constructs (sequential flow, while-loop, and if-else) and input/output. Read "Introduction to Programming in C for Novices and First-time Programmers" if you need help in understanding this program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Sum the odd and even numbers, respectively, from 1 to a given upperbound.
* Also compute the absolute difference.
* (SumOddEven.c)
*/
#include <stdio.h> // Needed to use IO functions
int main() {
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int upperbound; // Sum from 1 to this upperbound
int absDiff; // The absolute difference between the two sums
// Prompt user for an upperbound
printf("Enter the upperbound: ");
scanf("%d", &upperbound); // Use %d to read an int
// Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
int number = 1;
while (number <= upperbound) {
if (number % 2 == 0) { // Even number
sumEven += number; // Add number into sumEven
} else { // Odd number
sumOdd += number; // Add number into sumOdd
}
++number; // increment number by 1
}
// Compute the absolute difference between the two sums
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// Print the results
printf("The sum of odd numbers is %d.\n", sumOdd);
printf("The sum of even numbers is %d.\n", sumEven);
printf("The absolute difference is %d.\n", absDiff);
return 0;
}
Enter the upperbound: 1000
The sum of odd numbers is 250000.
The sum of even numbers is 250500.
The absolute difference is 500.