0% found this document useful (0 votes)
8 views23 pages

Chapter 01

Uploaded by

MD HEZBULLAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views23 pages

Chapter 01

Uploaded by

MD HEZBULLAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

 The development of Programming language 1960

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

program functions statements expressions

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

Function: main( ), print(…)


 The First Program Is Always The Same
Print the words: "\Hello, world"
you have to:
 Create the program text
 Compile it successfully
 Run it
 Get the output
 “Hello, world.” Program
/* File: hello.c
This program prints
program comment
the message “Hello,
world.” on the screen.
*/
library inclusions
#include <stdio.h>

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

1.1 Getting Started


Write a program in C to print the words:
hello, world !
include information
#include <stdio.h> about standard library
main( ) main function with no arguments
{ statements of main are enclosed in brace
call library function
printf (“hello, world!”);
named printf

} “\n” is the notation for


newline character.
 \n represents only a single character. An
escape sequence like \n represents hard-to-
type or invisible characaters.
The C Chapter
Programming
1. A Tutorial
LanguageIntroduction
Chapter
1.1 Getting
1. A Tutorial
Started
Introduction

1.1 Getting Started


Write a program in C to print the words:
hello, world !

#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

Write a program in C to print the following table


of Fahrenheit temperatures and their centigrade
or Celsius equivalents: Fahrenheit Celsius
0 -17
20 -6
40 4
60 15
80 26
Formula: C=(5/9)(F-32) 100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
Types
• A type defines a set of values and a set of
operations that can be applied on those
values.
char
short
long
double
int
float
Chapter 1. A Tutorial Introduction
1.2 Variables and Arithmetic Expressions

Fahrenheit Celsius #include <stdio.h>


0 -17
/* printf Fahrenheit-Celsius table
20 -6 for fahr=0,20,40,…,300 */
40 4 main() fahr = lower
60 15 {
80 26 int fahr, celsius; no
fahr<=upper?
100 37 int lower,upper,step;
120 48 lower = 0; yes /*lower limit of T table*/
140 60 upper = 300; /*upper
temperature limit*/
convert
160 71 stepprint
= 20; /*step size*/
180 82 fahr = lower;
200 93
while(fahr <= upper) {
220 104 fahr=fahe+step
celsius = 5*(fahr-32)/9;
240 115
260 126
printf("%d\t%d\n", fahr,celsius);
280 137
fahr = fahr + step;
300 148
}
}
Chapter 1. A Tutorial Introduction
1.4 Symbolic Constants

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 Input and Output


Important View:
In C, the model of Input & Output are supported by
the standard library, and NOT the statements of C.
Tow simplest I/O functions:
 getchar()
read in a character at a time from text stream
c=getchar();
 putchar()
write one character at a time from text stream
putchar(c);
Chapter 1. A Tutorial Introduction
1.5 Character Input and Output

 File Copying Algorithm


An assignment can appear as
part of a larger expression.
#include <stdio.h>
/* copy input to output; 1st #include <stdio.h>
version*/ /* copy input to output; 2nd version*/
main()
read in a character main()
{ While ( character is{not end-of-file indicator)
int c;
int c; output the character just read
c=getchar(); while((c=getchar())!=EOF)
read in a
while(c!=EOF) { character putchar(c);
putchar(c); }
c=getchar();
}
}  What happens if
parentheses are omitted?
c=getchar()!=EOF
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

 a character between single


 Line Counting quotes, called character
constant, represents an integer
value equal to the numerical
#include <stdio.h> value of the character. For
/* count lines in input */ example, ‘A’‘s value is 65, ‘\n’,
main() a escape sequence , is also a
{ legal character constant.
int c, nl;
nl=0;
while(( c=getchar())!=EOF)
if(c==‘\n’)
++nl;  What value does the ‘\
printf(“%d\n”,nl); n’ has?
}
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

 a character between single


 Line Counting quotes, called character
constant, represents an integer
value equal to the numerical
#include <stdio.h> value of the character. For
/* count lines in input */ example, ‘A’‘s value is 65, ‘\n’,
main() a escape sequence , is also a
{ legal character constant.
int c, nl;
nl=0;
while(( c=getchar())!=EOF)
if(c==‘\n’)
++nl;  What value does the ‘\
printf(“%d\n”,nl); n’ has?
}
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);
}

You might also like