2. About C
Developed in 1972 By Dennis Ritchie at AT & T Bell Lab in
USA
Many ideas took from BCPL and B languages so gave name
C.
Structured programming is possible by using Functions
Extension is easily possible by introducing new libraries
In 1989, C is accepted by ANSI (American national
standardization institute)
2
3. Why C if we have C++,C# and Java
1. Very difficult to learn C++,C# and Java without knowing
C.
2. Major parts of OS like Windows,UNIX, Linux and Device
drivers etc. are still in C because performance wise C is
better than other.
3. C programs are comparatively time and memory efficient
that’s why programs related to Mobile Devices,
microwave ovens, washing machine etc. written in C.
4. Professional 3D games also written in C because of speed.
3
7. Preprocessor Directive
The text inside /* and */ is called comment or documentation.
The statement starting with # (hash sign) is called pre-processor statement.
The #include is a "preprocessor" directive that tells the compiler to put code
from the header called stdio.h into our program before actually creating the
executable.
By including header files, you can gain access to many different functions--both
the printf and scanf functions are included in stdio.h.
getch has its prototype in conio.h header file.
stdio.h is a header file which has:
o Prototype or declaration only of the library functions
o Predefined constants
Note: Header file does not contain the code of library functions.
It only contains the header or prototype.
7
8. main Function
A program may contain many functions, but it essentially
contains a main function.
The return type of main function is kept int type. A program
should return value.
o 0 (zero) in case of normal termination.Non-zero in case of
abnormal termination, i.e. termination due to some error.
9. Pre-processing:
o Pre-processing statements are processed first
o All the statements starting with #(hash) sign are preprocessing
statements
o eg. #include and #define
Compilation: The syntactical validity of the complete program is
checked
o If there is no error the compiler generates the object code (.obj)
Linking: Symbolic Links are resolved in this phase
o A function call is called symbolic link
o A function can be user defined or ready made function in the library
o The linker substitutes the name of the function with the address of the
first instruction of the function
Execution: The Instruction of object code are executed one by one.
Steps of Program Execution
10. Steps of Program Execution
Pre-processing
Compilation
Linking
Loading
Execution
Alt + F9
Ctrl+ F9
11. Types of Errors
Syntax error: When there is the violation of the grammatical (
Syntax) rule. Detected at the compilation time.
o Semicolon not placed
o Standard Construct not in proper format
o Identifier not declared
Linker Error: When definition or code of a program is not
found.
o Path of header not properly defined
o Library file not found
o The function name is misspelled
12. Types of Errors Cont...
Runtime error: It is generated during execution phase. Due to
mathematical or some operation generated at the run time.
o Division by zero
o Square root of negative number
o File not found
o Trying to write a read only file
Logical Error: Produced due to wrong logic of program. Tough to
identify and even tougher to remove.
o Variables not initialized
o Boundary case errors in loops
o Misunderstanding of priority and associativity of operators
13. Types of Statements in C Prog.
Preprocessing Statements: Also called compiler directives. The
purpose depends on the type of commands.
o # include statement inserts a specified file in our program
o They don't exist after compilation
Declarative Statements: Used to declare user identifier i.e. to
declare data types for example: int a, b;
o These statements do not exist in the object code.
14. Types of Statements in C Prog. Cont...
Executable Statements: The statements for which the executable or
binary code is generated. For
o Input/Output Statements like printf(), scanf()
o Assignment Statements. The syntax is lvalue=rvalue
o Conditional Statements like if(a>b) max =a; else max=b;
o Looping Statements. Also called Iterative statements or repetitive
statements.
For
while
do while
o Function Call like y=sin(x);
15. Types of Statements in C Prog.
Cont...
Special Statements: There are four special statements
o break
o continue
o return
o exit
16. Keywords & Identifiers
Every C word is classified as either a keyword or an
identifier.
Keywords: The meaning of some words is reserved in a language
which the programmer can use in predefined manner. Theses are
called keywords or reserve words. For example: do, while, for, if,
break, etc…
Identifiers refers to the names of variables , function, structure and
array.
Examples: main, printf, average, sum etc.
17. 32 -Reserved Words (Keywords)
in C
auto
break
case char
const continue
default do
double else
enum extern
float for
goto if
int long
register return
short signed
sizeof static
struct switch
typedef union
unsigned void
volatile while
18. Rules of Making Identifier or variable
Rule1. Identifier name can be the combination of alphabets (a – z and A - Z),
digit
(0 -9) or underscore (_). E.g. sum50, avgUpto100 etc.
Rule 2. First character must be either alphabet or underscore
E.g. _sum, class_strength,height are valid
123sum, 25th
_var are invalid
Rule 3. Size of identifier may vary from 1 to 31 characters but some compiler
supports bigger size variable name also.
Rule-4. No space and No other special symbols(!,@,%,$,*,(,),-,+,= etc) except
underscore are allowed in identifier name.
Valid name: _calulate, _5,a_, __ etc.
Invalid name: 5@, sum function, Hello@123 etc.
Rule 5. Variable name should not be a keyword or reserve word
Invalid name: interrupt, float, asm, enum etc.
19. Rule 6: Name of identifier cannot be exactly same as
of name of function or the name of other variable
within the scope of the function.
What will be output of following program?
#include<stdio.h>
int sum();
int main(){
int sum;
sum=sum();
printf("%d",sum);
return 0;
}
int sum(){
int a=4,b=6,c;
c=a+b;
return c;}
Output: Compiler error
19
20. What Are Variables in C?
Variables are named memory location. It may be used for storing a
data value. A variable may take different values at different time
during execution. Example int avg,length etc.
Naming Convention for Variables:
C programmers generally agree on the following conventions for
naming variables.
1. Use meaningfulidentifiers
eg for storing sum use variable name sum
2. Separate “words” within identifiers with underscores or mixed upper
and lower case.
Examples: surfaceArea, surface_Area
surface_area etc
21. Case Sensitivity
C is case sensitive
It matters whether an identifier, such as a variable name, is uppercase
or lowercase.
Example:
area
Area
AREA
ArEa
are all seen as different variables by the compiler.
22. Identify valid variable name
John
X1
_
Group one
Int_type
Price$
char
(area)
1ac
i.j
if
22
Valid
Valid
Valid
Invalid- no white space allowed
Valid
Invalid- no special symbol allowed other than _
Invalid- no keyword allowed
Invalid- no special symbol allowed other than _
Invalid- numeral cant be 1st
character
Invalid- . Is special symbol
Invalid- no keyword allowed
23.
24. The C Character Set
A character denotes any alphabet, digit or special symbol
used to represent information.
Figure 1 shows the valid alphabets,numbers and special
symbols allowed in C.
25. Constants and Variables
The alphabets, numbers and special symbols when properly
combined form constants, variables and keywords.
A constant is an entity that doesn’t change whereas a
variable is an entity that may change.
In any program we typically do lots of calculations. The
results of these calculations are stored in computers memory.
Like human memory the computer memory also consists of
millions of cells. The calculated values are stored in these
memory cells. To make the retrieval and usage of these
values easy these memory cells (also called memory
locations) are given names. Since the value stored in each
location may change the names given to these locations are
called variable names.
26. Example of Variable and Constant
Here 3 is stored in a memory location and a name x is given to it.Then we are
assigning a new value 5 to the same memory location x. This would overwrite
the earlier value 3, since a memory location can hold only one value at a time.
Since the location whose name is x can hold different values at different times
x is known as a variable. As against this, 3 or 5 do not change, hence are
known as constants.
27. Types of C Constants
C constants can be divided into two major categories:
(a) Primary Constants
(b) Secondary Constants
These constants are further categorized as shown in Figure
28. Integer Constants
Rules for Constructing Integer Constants
1. An integer constant must have at least one digit.
2. It must not have a decimal point.
3. It can be either positive or negative.
4. If no sign precedes an integer constant it is assumed to be
5. positive.
6. No commas or blanks are allowed within an integer constant.
7. The allowable range for integer constants is -32768 to 32767.
Ex. 426
+782
-8000
-7605
25,000, 67 000 are invalid integers28
29. Real or Floating Point Constants
Rules for Constructing Real Constants
Real constants are often called Floating Point constants. The real constants
could be written in two forms—Fractional form and Exponential form.
Following rules must be observed while constructing real
constants expressed in fractional form:
1. A real constant must have at least one digit.
2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive.
5. No commas or blanks are allowed within a real constant.
Ex.: +325.34, 426. , -32.76 , -48.5792, +.5
29
30. Exponential Form of Real constants
Used if the value of the constant is either too small or too large.
In exponential form of representation, the real constant is
represented in two parts:
The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called
exponent.
Rules:
1. The mantissa part and the exponential part should be separated by a letter e.
2. The mantissa part may have a positive or negative sign.
3. Default sign of mantissa part is positive.
4. The exponent must have at least one digit, which must be a
5. positive or negative integer. Default sign is positive.
6. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex.: +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5
7500000000 will be 7.5E9 or 75E8
30
31. Character Constant
Rules for Constructing Character Constants
1. A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single inverted commas.
2. Both the inverted commas should point to the left.
For example, ’A’ is a valid character constant whereas ‘A’ is not.
3. The maximum length of a character constant can be 1
character.
Ex.: 'A‘, 'I‘, '5‘, '='
31
32. Declaring Variables
Before using a variable, you must give the compiler some
information about the variable; i.e., you must declare it.
The declaration statement has following format
<Data Type> <Variable name>
Data type will indicate the type of variable just like variable
may integer type so data type int will be used.
Examples of variable declarations:
int length ;
float area ;
char ch;
33. Declaring Variables (con’t)
When we declare a variable
Space is reserved in memory to hold a value of the specified data type
That space is associated with the variable name.
That space is associated with a unique address.
If we are not initializing variable than space has no known value(garbage value).
Visualization of the declaration
int a ;
a
2000
3718
int
35. Notes About Variables
You must not use a variable until you somehow give it a
value.
You can not assume that the variable will have a value before
you give it one.
Some compilers do, others do not! This is the source of many
errors that are difficult to find.
Assume your compiler does not give it an initial
value!
36. Using Variables: Initialization
Variables may be be given initial values, or initialized, when
declared. Examples:
int length = 7 ;
float diameter = 5.9 ;
char initial = ‘A’ ;
7
5.9
‘A’
length
diameter
initial
37. Data Types
To deal with some data, we have to mention its type (i.e.
whether the data is integral, real, character or string etc.) So data
types are used to tell the types of data.
Data Types Categories
48. Overflow in Unsigned int
If number is X where X is greater than 65535 then
New value = X % 65536
If number is Y where Y is less than 0 then
New value = 65536– (Y% 65536) (Take Y without sign)
Example:
unsigned int X=67777;
unsigned int Y= -10;
X=2241
Y=65526
50. Range of int
If number is X where X is greater than 32767 then
p = X % 65536
if p <=32767 then New value = p
else New value = p - 65536
If number is Y where Y is less than -32768 then
p = Y % 65536 (Take Y without sign)
If p <= 32767 then New value = -p
else New value = 65536 -p
Example:
int X=32768; =-32768
int Y= -65537; = -1