Fundamentals of The C Programming Language
Fundamentals of The C Programming Language
Fundamentals of the
C Programming Language
1
3.1 INTRODUCTION
This chapter serves as a formal introduction
to the C programming language.
4
Design
5
Implementation
8
2. Identifiers
programmer-defined words. Needed for program va
riables, functions, and other program constructs. gross_i
ncome and city_tax are examples. Must be unique withi
n the same scope
6. Operators
result in some kind of computation or action
city_tax = CITY_TAX_TATE * gross_income ;
12
3.4 THE STRUCTURE OF A C PROGRAM
C program consists of following component
s:
1. Program comments
2. Preprocessor directives
3. Type declarations
4. Named constants
5. Statements
6. Function declarations (prototypes)
7. Function definitions
8. Function calls
13
1. Program Comments
use /* and */ to surround comments, or // to begin comment lines.
2. Preprocessor Directives
Lines that begin with a pound sign, #,
A preprocessor directive is can instruction to the preprocessor. Na
med file inclusion is concerned with adding the content of a header
file to a source program file. Standard header files. For example,
#include <stdio.h>
#include causes a headerfile to be copied into the code.
programmer-defined header file surrounded by double quotation m
arks. #include <d:header1.h>
to advantage in partitioning large programs into several files.
14
3. Data Types and Type Declarations
double gross_income;
double city_tax;
15
declare a variable to be of type integer, the compiler allo
cates a memory location for that variable. The size of thi
s memory location depends on the type of the compiler.
int is 2 bytes the range –32768 through 32768 designed t
o perform arithmetic operations and assignment operatio
ns. Two classes of data types:
1. Fundamental data types
2. Programmer-defined data types
to classes of built-in data types:
1. Fundamental data types
2. Derived data types
Examples of derived data types are arrays, strings, and st
ructures.
16
Data Type int
Data Type char
Data Type double
Data initialization
be initialized in two ways,
1. Compile-time initialization
2. Run-time initialization
Strings as a Derived Data Type
A string is a sequence of characters that is trea
ted as a single data item. A string variable is a vari
able that stores a string constant.
17
how to declare string variables.
18
4. Named Constants
const double CITY_TAX_RATE = 0.0175;
is an identifier whose value is fixed and does not
change during the execution of a program in
which it appears.
Compound Statements
is a list of statements enclosed in braces, { }
20
3.5 A FIRST LOOK AT FUNCTIONS
as a block of code that performs a specific task.
23
Example 3.2
24
The Standard Output Function printf
int %d
double %f or % lf
char %c
26
Variable Type scanf Format Specifier
int %d
double %lf
char %c
1 #include <stdio.h>
2
3 int main (void) {
4 double number;
5
6 printf(“Enter a number : “)
7 scanf(“%lf” , &number);
8 Inverse = 1.0 / number ;
9 printf(“Inverse of %f is %f” , number, inverse);
32
---Configuration : debug – Win32 Debug ---
Compiling …
Debug.c
D:\cprogs\debug.c(7) : error C2146: cyntax error : m
issing ‘;’ before identifier ‘scanf’
D:\cprogs\debug.c(8) : error C2065 ‘inverse’ : undec
lared identifier.
D:\cprogs\debug.c(8) : warning C4244 : ‘=‘ : conver
sion from ‘const double ‘ to ‘ int ‘ , possible loss o
f data.
D:\cprogs\debug.c(10) : fatal error C1004 : unexpect
ed end of file found
Error executing c1.exe
Debug.exe – 3 error(s), 1 warning(s)
33
Debugging for Warning Diagnostics
do not force it to stop the compilation.
Enter a number : 0
Floating point error : Divide by 0 .
Abnormal program termination .
34
if number is equal to zero
print “Zero does not have a finite inverse.”
else
compute inverse = 1 / number
end_if
35