Cprogramming ICTweek 4
Cprogramming ICTweek 4
Cprogramming ICTweek 4
Introduction
Week 4:Variables,
constant, Standard input
Lecturer : Do Quoc Huy
Dept of Computer Science
Hanoi University of
Technology
For HEDSPI Project
Topic of this week
• Variables
– Class Lecture Review
• Variables
• Basic data types
• Constants
• Standard input.
– Programming Exercises
Identifiers
• Names of things (variables, functions,
etc.)
int nMyPresentIncome = 0;
int DownloadOrBuyCD();
Identifier naming rules
• Letters, digits, underscores
– i
– CSE_5a
– a_very_long_name_that_isnt_very_useful
– fahrenheit
• First character cannot be a digit
– 5a_CSE is not valid!
• Case sensitive
– CSE_5a is different from cse_5a
What are variables?
• A named area in the computer memory,
intended to contain values of a certain
kind (integers, real numbers, etc.)
• They contain the data your program works
with
• They can be used to store data to be used
elsewhere in the program
• In short – they are the only way to
manipulate data
Variables
• Named region of storage
int nRow = 0;
• Type (size and meaning of the storage)
• Scope
– Block
– Function args
– Global
– Be careful not to “hide” a variable
• Lifetime (storage class)
– Automatic/temporary (block's lifetime)
– Globals (program's lifetime)
– Local static (program's lifetime)
Variables in memory
int my_int = 5;
double my_double = 3.5;
my_int 5
my_double 3.5
Declarations, definitions,
initialization
• Declarations that reserve storage are called
definitions
int j;
• Definitions may optionally assign a value
(initialization)
int j = 0;
• Declarations specify meaning but may not reserve
storage (e.g. extern)
extern int j;
• Release builds typically don't initialize
variables by default!
• Usage variables:
e.g: printf(“%d + %d = %d\n“, a, b, c);
Example: variable
declarations
• int i;
• char c;
• float f1, f2;
• float f1=7.0, f2 = 5.2;
• unsigned int ui = 0;
Example
12 c
1. #include <stdio.h> 7 b
2. 5 a
3. int main()
4. {
5. int a, b, c; /ngonnguC/bin/tong
6. printf(“The first number: “); The first number: 5
7. scanf(“%d”, &a);
The second number:7
8. printf(“The second number: “);
9. scanf(“%d”, &b); 5 + 7 = 12
10. c = a + b; /ngonnguC/bin/
• String literals
"You have fifteen thousand new messages."
"I said, \"Crack, we're under attack!\"."
"hello," "world" becomes "hello, world"
Basic data types (1)
• Sizes and limits (may vary for machine; CUNIX
is shown here):
type size in bits range
(on CUNIX)
char 8 -128...127
short 16 -32,768…32,767
int 32 -2,147,483,648…2,147,483,647
long 32 -2,147,483,648…2,147,483,647
float 32 10-38…3x1038
double 64 2x10-308…10308
• Look at /usr/include/limits.h
Formatting Input with Scanf
• scanf
– Input formatting
– 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
– can include field widths to read a specific
number of characters from the stream
Formatting Input with Scanf (II)
Conversion specifier Description
Integers
d Read an optionally signed decimal integer. The corresponding argument is a pointer to integer.
i Read an optionally signed decimal, octal, or hexadecimal integer. The corresponding argument is a pointer to integer.
o Read an octal integer. The corresponding argument is a pointer to unsigned integer.
u Read an unsigned decimal integer. The corresponding argument is a pointer to unsigned integer.
x or X Read a hexadecimal integer. The corresponding argument is a pointer to unsigned integer.
h or l Place before any of the integer conversion specifiers to indicate that a short or long integer is to be input.
Floating-point numbers
e, E, f, g or G Read a floating-point value. The corresponding argument is a pointer to a floating-point variable.
l or L Place before any of the floating-point conversion specifiers to indicate that a double or long double value is to be input.
Characters and strings
c Read a character. The corresponding argument is a pointer to char, no null ('\0') is added.
s Read a string. The corresponding argument is a pointer to an array of type char that is large enough to hold the string and a
terminating null ('\0') character—which is automatically added.
Scan set
[scan characters Scan a string for a set of characters that are stored in an array.
Miscellaneous
p Read an address of the same form produced when an address is output with %p in a printf statement.
n Store the number of characters input so far in this scanf. The corresponding argument is a pointer to integer
% Skip a percent sign (%) in the input.
• Example of scanf()
int d,m,y,x;
char ch1,ch2;
Result
float f;
scanf(“%d”, &x); 4
// x=4
scanf(“%2d%2d%4d”, &d,&m,&y); 22062007
// d=22, m=6, y=2007
scanf(“%d/%d/%d”, &d,&m,&y); 22/06/2007
// d=22, m=6, y=2007
scanf(“%c%c”, &ch1,&ch2); Ab
// ch1=‘A’, ch2=‘b’
scanf(“%f”, &f); 2.3
// f=2.300000
Formatting Input with Scanf (III)
• 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]
• Causes characters not in the scan set to be
stored
Formatting Input with Scanf (IV)
• Skipping characters
–Include character to skip in format
control
–Or, use * (assignment suppression
character)
•Skips any type of character without
storing it
Example 2
•Reading characters and strings
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}
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 4
•Reading and discarding characters from the input stream
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
BK Bookseller
VAT
You pay: