Content:

Reading: Textbook Chapter 2

 

Lecture 2:

Structure of a C program

The first program

Comments and Program Documentation

Rules for naming identifiers

Data Types

 

Lecture 3:

Variables

Constants

Ways of coding constants in a program

Brief introduction of formatted I/O


Lecture 2

Structure of a C program

Powerpoint file 02 slide 3 (fig. 2-2)

2-2.gif (39604 bytes)

 


The first program (Program 2-1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* The greeting program. This program demonstrates
      some of the components of a simple C program.
      Written by: (your name here)
      Date written: (today's date here)
*/
#include <stdio.h>

int main (void)
{
/* Local Declarations */

/* Statements */

   printf (
Hello World!\n);

   return 0;
} /* main */

Preprocessor directives

Powerpoint file 02 slide 4 (fig. 2-3)

2-3.gif (23716 bytes)


Comments and Program Documentation

Powerpoint file 02 slide 5 (fig. 2-4)

  2-4.gif (12328 bytes)

 

Powerpoint file 02 slide 6 (fig. 2-5)

2-5.gif (14157 bytes)


Rules for naming identifiers


Data Types

 

void

 

int

 

char

                                'A'                                     65

                                '1'                                      49

float

2-float.gif (7237 bytes)

 

Summary

void

void

character

char

integer

unsigned short int

unsigned int

unsigned long int

short int

int

long int

float

float

double

long double


Lecture 3

Variables

Declaration and definition

Powerpoint file 02 slide 10 (fig. 2-9)

  2-9.gif (43196 bytes)

short int maxItems; /* Word seperator: Capital */

long int national_debt; /* Word separator: underscore */

float payRate; /* Word separator: Capital */

double tax;

char code, kind; /* Poor style: multi-declarations in a line */

int a, b; /* Poor style */

 

Initialization

int count = 0; /* initialize count */
int count, sum = 0; /* initialize sum only ! */
int count = 0, sum = 0; /* initialize both */
int count = 0; /* preferred version */

int sum = 0;

top


Constants

Integer constants

 

 

Literal

Value

Type

+123

-378

-32271L

76542LU

123

-378

-32,271

76,542

int

int

long int

unsigned long int

 

Float constants

 

Literal

Value

Type

0.

.0

2.0

3.1416

-2.0f

3.1415926536L

0.0

0.0

2.0

3.1416

-2.0

3.1415926536

double

double

double

double

float

long double

 

Character constants

 

ASCII Character

Symbolic Name

Null character

alert (bell)

backspace

horizontal tab

new line

vertical tab

form feed

carriage return

single quote

backslash

‘\0’

‘\a’

‘\b’

‘\t’

‘\n’

‘\v’

‘\f’

‘\r’

‘\’’

‘\\’

 

String constants

“” (null string)

“h”

“Hello World”


Ways of coding constants in a program

 

Literal constants

 

Defined constant

 

Memory constants

 


Brief introduction of formatted I/O

Powerpoint file 02 slide 13 (fig. 2-12)

  2-12.gif (33174 bytes)

 

printf

  2-13.gif (9721 bytes)

Code

Meaning

Argument Type

Example

c

character

char

%c

d

integer

int

%d

f

floating point

float

double

%f

%f

 

printf (“%d\t%c\t%5.1f\n”, 23, ‘Z’, 14.2);

printf (“%d\t%c\t%5.1f\n”, 107, ‘A’, 53.6);

printf (“%d\t%c\t%5.1f\n”, 1754, ‘F’, 122.0);

printf (“%d\t%c\t%5.1f\n”, 3, ‘P’, 0.1);

 

23 Z 14.2

107 A 53.6

1754 F 122.0

3 P 0.1

 

printf (“The number%dis my favorite number.”, 23);

 

The number23is my favorite number.

 

printf(“The number is %6d”, 23);

 

2-fex.gif (2118 bytes)
printf(“The tax is %6.2f this year.”, 233.12);

 

The tax is 233.12 this year.

 

printf(“The tax is %8.2f this year.”, 233.12);

 

The tax is   233.12 this year.

 

printf(“The tax is %08.2f====this year.”, 233.12);

 

The tax is 00233.12= = = = this year.

 

printf(“\”%8c===%d\””, ‘h’, 23);

 

2-pfex2.gif (1749 bytes)

 

 

scanf

 2-scanf.gif (8480 bytes)

 

214 156 14Z

 

scanf(“%d%d%d%c”, &a, &b, &c, &d); (note: no space before %c)

 

2314 15 2.14

 

scanf(“%d %d %f”, &a, &b, &c); (note: the spaces in format list can be omitted)

 

14/26 25/66

 

scanf(“%2d/%2d %2d/%2d”, &num1, &den1, &num2, &den2);

 

11-25-56

 

scanf(“%d-%d-%d”, &a, &b, &c);

 

 

- End of Lectures 2 and 3-