IT1016 Part2 Lesson3
IT1016 Part2 Lesson3
2
Constant declarations
• Constants are used to store values that never change during the
program execution.
• Using constants makes programs more readable and maintainable.
Syntax:
const type identifier = expression;
Examples:
const double US2HK = 7.8;
//Exchange rate of US$ to HK$
const double HK2TW = 3.98;
//Exchange rate of HK$ to TW$
const double US2TW = US2HK * HK2TW;
//Exchange rate of US$ to TW$
3
Variable declarations
• Variables are used to store values that can be changed during
the program execution.
• A variable is best thought of as a container for a value.
3445 y -3.14
Syntax:
< type > < identifier >;
< type > < identifier > = < expression >;
Examples:
int sum;
int total = 3445;
char answer = 'y';
double temperature = -3.14;
4
Variable declarations
• A variable has a type and it can contain only values of that
type. For example, a variable of the type int can only hold
integer values.
• Variables are not automatically initialized. For example, after
declaration
int sum;
the value of the variable sum can be anything (garbage).
• Thus, it is good practice to initialize variables when they are
declared.
• Once a value has been placed in a variable it stays there until
the program deliberately alters it.
5
Constant declarations
A convenient way to associate constant values with
names is using the #define statement.
#define
Examples
#define TRUE 1
#define TABLESIZE 100
Constants can be declared by const declaration.
They appear inside functions.
const data_type const_name = value
const int a=2, b=5;
6
Statements and Blocks
• An expression such as x = 0 or i++ or printf(. . .)
becomes a statement when it is followed by a semicolon.
• Block group any number of data definitions, declarations,
and statements into one statement.
• compound statement group statements into one statement
• Use a blocks or compound statements wherever a single
statement is allowed.
• We use braces {} to build block or compound -
statements
7
Input and output in C
• C programming provides a set of built-in functions
to read the given input and feed it to the program as
per requirement.
• C also provides a set of built-in functions to output
the data on the computer screen as well as to save it
in text or binary files.
• Typical input output functions are
• In <stdio.h>
• The scanf() and printf() Functions
• The getchar() and putchar() Functions
• In <conio.h>
• The getch() and putch() Functions
• The gets() and puts() Functions
8
Functions printf, scanf
• Input by using the scanf() function
9
The syntax of printf() function
printf(“[string]”[,list of arguments]);
List of arguments : expressions, separated by
commas.
The string is usually called the control string or the
format string.
Action:
◦ Scan the string from left to right
◦ Prints on the screen any characters it encounters -
except when it reaches a % character
10
How printf function works
11
The % Format Specifiers
12
Printing with Field Widths and Precisions
• Precision
• Meaning varies depending on data type
• Integers (default 1) - minimum number of digits to
print
• If data too small, prefixed with zeros
• Floating point - number of digits to appear after
decimal (e and f)
• For g - maximum number of significant digits
• Strings - maximum number of characters to be
written from string
Printing with Field Widths and Precisions
• Format
• Precision: use a dot (.) then precision number
after %
%.3f
• Can be combined with field width
%5.3f
• Can use integer expressions to determine field
width and precision
• Use *
• Negative field width - left justified
• Positive field width - right justified
• Precision must be positive
• Field width
• Size of field in which data is printed
• If width larger than data, default right justified
• If field width too small, increases to fit data
• Minus sign uses one character position in field
• Integer width inserted between % and conversion
specifier
• %4d: field width of 4
Example
1 #include <stdio.h>
2
3 int main()
4 {
5 printf( "%10s%10d%10c%10f\n\n", "hello", 7, 'a', 1.23 );
6 printf( "%-10s%-10d%-10c%-10f\n", "hello", 7, 'a', 1.23 );
7 return 0;
8 }
hello 7 a 1.230000
hello 7 a 1.230000
Using Flags in the printf Format-Control String
• Flags
• Supplement formatting capabilities
• Place flag immediately to the right of percent sign
• Several flags may be combined
Fla g Desc rip tion
1 #include <stdio.h>
2
3 int main()
4 {
5 int c = 1427;
6 double p = 1427.0;
7
8 printf( "%#o\n", c );
9 printf( "%#x\n", c );
10 printf( "%#X\n", c );
11 printf( "\n%g\n", p );
12 printf( "%#g\n", p );
13
14 return 0;
15 }
02623
0x593
0X593
1427
1427.00
Printing Literals and Escape Sequences
• Printing Literals
• Most characters can be printed
• Certain "problem" characters, such as the quotation
mark ” must be represented by escape sequences
• Represented by a backslash \ followed by an escape
character
Printing Literals and Escape Sequences
• Role
• Reading input values
• Capabilities
• Input all types of data
• Input specific characters
• Skip specific characters
• Format
scanf(format-control-string, other-arguments);
• format-control-string: describes formats of inputs
• other-arguments: pointers to variables where input will
be stored
Formatting Input
Example
• Scan sets
• Set of characters enclosed in square brackets []
• Preceded by % sign
• Scans input stream, looking only for characters in
scan set
• Whenever a match occurs, stores character in specified
array
• Stops scanning once a mismatch is found
• Inverted scan sets
• Use a caret ^: [^aeiou]
• Characters not in the scan set to be stored
Formatting Input
• Skipping characters
• Include characters to skip in format control
• Use: *
• %*c: skip character from the input
Example
2 #include <stdio.h>
3
4 int main()
5 {
6 char z[9] = {'\0'};
7
8 printf("Enter a string: ");
9 scanf("%[^aeiou]", z );
10 printf("The input was \"%s\"\n", z );
11
12 return 0;
13 }
Enter a string: String
The input was "Str"
Example
1 #include <stdio.h>
2
3 int main()
4 {
5 char x, y[ 9 ];
6
7 printf( "Enter a string: " );
8 scanf( "%c%s", &x, y );
9
10 printf( "The input was:\n" );
11 printf( "the character \"%c\" ", x );
12 printf( "and the string \"%s\"\n", y );
13
14 return 0;
15 }
1 #include <stdio.h>
2
3 int main()
4 {
5 int month1, day1, year1, month2, day2, year2;
6
7 printf( "Enter a date in the form mm-dd-yyyy: " );
8 scanf( "%d%*c%d%*c%d", &month1, &day1, &year1 );
9 printf( "month = %d day = %d year = %d\n\n",
10 month1, day1, year1 );
10 printf( "Enter a date in the form mm/dd/yyyy: " );
14 scanf( "%d%*c%d%*c%d", &month2, &day2, &year2 );
15 printf( "month = %d day = %d year = %d\n",
16 month2, day2, year2 );
17
18 return 0;
19 } Enter a date in the form mm-dd-yyyy: 11-18-2000
month = 11 day = 18 year = 2000
36
Solution
1. #include <stdio.h>
2. #include<math.h>
3. main()
4. {
5. float a,b,c,s,A;
6. puts("Enter three sizes of the
7. triangle, a,b,c:");
8. scanf("%f %f %f", &a,&b,&c);
9. s=(a+b+c)/2;
10. A= sqrt(s*(s-a)*(s-b)*(s-c));
11. printf ("Area of the triangle: %1.2f",A);
12.}
37