Chapter 01
Chapter 01
Algol-60
1950
Fortran 1963
CPL
1960 Algol-60 Cobol 1967
CPL BCPL
Simula BCPL
1970
1970 Pascal B
B
Smalltalk
C 1972
1980 Ada C
C++ 1985
ANSI C
1990 C++
Java
Overview of C Language
Characteristics of C
Small size
C is modular
Loose typing
Structured language
Low level programming readily available
C has a very powerful set of operators
Rich data structure
C efficient on most machines
Overview of C Language
C Program Structure
Characters Operands +
operators
Essential Elements
Numeral: 0 1 2 … 9
Letter: ABC…Z a b c…z
Character
Operator: + - = > < …
Others: \r \n \t …
Expression
branch: if-else; switch
Statement Control loop: for; while
Jump: break; goto…
Compound
void main ()
{ main function
printf (“Hello, world.\n”);
}
• The program is stored as a text file named hello.c
• .c identifies the file as a C program.
Compile it
Run it
The C Chapter
Programming
1. A Tutorial
LanguageIntroduction
Chapter
1.1 Getting
1. A Tutorial
Started
Introduction
#include <stdio.h>
main( )
{
printf (“hello,”);
printf (“world”);
printf(“\n”);
}
The C Chapter
Programming
1. A Tutorial
LanguageIntroduction
Chapter
1.1 Getting
1. A Tutorial
Started
Introduction
Symbolic Constants
It is a particular string of characters.
Format :
#define name replacement list
Function:
Any occurrence of name will be replaced by the
#define LOWER 0 /* lower limit of table */
corresponding replacement-text.
#define UPPER 300 /*UPPER limit of table */
#define STEP 20 /*step limit of table */
/* print Fahrenheit-Celsius table */
main()
{
There is no semicolon at
int fahr; the end of a #define line.
for (fahr=LOWER; fahr<=UPPER; fahr=fahr+STEP)
printf (“%3d %6.1f\n”, fahr, (5.0/9.0)*(fahr-32));
}
Chapter 1. A Tutorial Introduction
1.5 Character Input and Output
Character Counting
/*Version 2*/
/*Version 1*/ #include <stdio.h>
#include <stdio.h> main()
main() {
{ double nc;
long nc; for(nc=0;getchar()!=EOF;++nc)
nc=0; ;
while(getchar()! printf(“%.0f\n”,nc); null statement
=EOF) }
++nc;
printf(“%ld\n”,nc);
}
Chapter 1. A Tutorial Introduction
1.5 Character Input and Output
#include <stdio.h>
#define IN 1 /*inside a word */
Word Counting: #define OUT 0 /*outside a word */
/*count words in input */
main()
{
int c, nw, state;
state=OUT;
nw=0;
What is the while((c=getchar())!=EOF) {
function of the if(c==' '|| c=='\n'|| c=='\t')
state=OUT;
variable state? else if (state==OUT) {
state=IN;
++nw;
}
}
printf("%d\n", nw);
}
Chapter 1. A Tutorial Introduction
1.5 Character Input and Output
Character Counting
/*Version 2*/
/*Version 1*/ #include <stdio.h>
#include <stdio.h> main()
main() {
{ double nc;
long nc; for(nc=0;getchar()!=EOF;++nc)
nc=0; ;
while(getchar()! printf(“%.0f\n”,nc); null statement
=EOF) }
++nc;
printf(“%ld\n”,nc);
} ++ means increment by one.
++nc nc=nc+1
Chapter 1. A Tutorial Introduction
1.5 Character Input and Output
#include <stdio.h>
#define IN 1 /*inside a word */
Word Counting: #define OUT 0 /*outside a word */
/*count words in input */
main()
{
int c, nw, state;
state=OUT;
nw=0;
What is the while((c=getchar())!=EOF) {
function of the if(c==' '|| c=='\n'|| c=='\t')
state=OUT;
variable state? else if (state==OUT) {
state=IN;
++nw;
}
}
printf("%d\n", nw);
}