Special Thanks to
Nakib Hayat Chowdhury Sir
Fundamentals
1
2 Structure of C Program
A C program basically consists of the following parts:
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
3
Structure of C Program
Preprocessor Commands
Main Function
Comment
Library Function
End of Program
4 Preprocessor Commands
These commands tells the compiler to do preprocessing
before doing actual compilation.
Like #include <stdio.h> is a preprocessor command
which tells a C compiler to include stdio.h file before going
to actual compilation.
5 Functions
Functions are main building blocks of any C Program.
Every C Program will have one or more functions and there
is one mandatory function which is
called main() function.
The C Programming language provides a set of built-in
functions.
In the above example printf() is a C built-in function which
is used to print anything on the screen.
6 Comments
Comments are used to give additional useful information
inside a C Program.
All the comments will be put inside /*...*/ as given in the
example above.
A comment can span through multiple lines.
/* comment for multiple line */
// single line comment
7
Variable and Variable Declaration
Variable is a named memory location that can hold
various values.
All variables must be declared before they can be
used.
When we declare a variable, we tell the compiler what
type of variable is being used.
A declaration associates a group of variables with a
specific data type.
8 C’s Fundamental Data Type
9 C’s Basic Data Type
Type Keyword format Memory
Specifier Requiremen
ts
Character data char %c 1 Byte
Signed whole numbers int %d 2 or 4 Byte
Floating-point numbers float %f 4 Byte
Double-precision floating- double %lf 8 Byte
point number
valueless void ---
10 How to Declare Variables
To declare a variable, use this general form:
type var-name;
In C, a variable declaration is a statement and
it must end in a semicolon (;).
11 Variable
Variables consist of letters and digits, in any
order, except that the first character must be a letter.
Both upper-and lowercase letters are permitted,
though common usage favors the use of lowercase
letters for most types of variables.
Upper- and lowercase letters are not interchangeable
(i.e., an uppercase letter is not equivalent to the
corresponding lowercase letter.)
12 Variable (cont.)
The underscore character (_) can also be included, and
is considered to be a letter.
An underscore is often used in the middle of an
variable.
A variable may also begin with an underscore, though
this is rarely done in practice.
13 Variable (cont.)
Case-sensitive
COUNT and count are not same
As a rule, an identifier should contain enough
characters so that its meaning is readily apparent.
On the other hand, an excessive number of characters
should be avoided.
Variable (Cont.)
14
Can not Use blank space
Can use letter of
Can not Use any keyword
alphabet (A-Z, a-z)
The first character must Variabl
Digits (0-9)
be a letter e
Can not start a variable
name with digit Underscore (_)
Dollar sign ($)
15 Is it Valid Variable Name?
Apon 1joty
joty-5
apon
apon123 this_is_a_long_name
_sojeb_1 VaReNdRa
16 Is it Valid Variable Name?
4th The first character must be letter
“x” Illegal characters (“)
Order-no Illegal characters (-)
My variable Illegal characters (blank space)
17 Where to Declare?
There are three places where variables are
declared:
1. Inside a function (local variable)
2. Outside all functions (global variable)
3. As function’s parameter
18 Identifiers
Identifiers are names that are given to various
program elements, such as variables, functions and
arrays.
19 Keywords
There are certain reserved words, called Keywords,
that have standard, predefined meaning in C
Can be used only for their intended purpose
Can't use as identifiers
20 Keywords
21 Statements
A statement causes the computer to carry out some
action.
There are three different classes of statements in C.
1. expression statements,
2. compound statements and
3. control statements.
22 Statements (cont.)
An expression statement consists of an expression followed by a
semicolon.
The execution of an expression statement causes the expression
to be evaluated.
Several expression statements are shown below.
a = 3;
c = a+b;
i++;
printf ("Area = %f”, area) ;
;
23 Expressions
An expression is a combination of operators and
operands.
C expressions follow the rule of algebra
Expression Operator
Arithmetic Expression +, -, *, /, %
Logical Expression AND, OR, NOT
Relational ==, !=, <, >, <=, >=
24 Assign value to variable
To assign a value to a variable, put its name to the left of an equal sign (=).
Put the variable you want to give the variable to the right of the equal sign.
It is a statement, so end with a ‘;’
variable value
;
25
26 Constants
There are four basic types of constants in C.
1. integer constants,
Numeric type constant
2. floating-point constants,
3. character constants and
4. string constants.
There are also enumeration constants
27 Integer Constants
An integer constant is an integer-valued number.
Thus it consists of a sequence of digits.
Integer constants can be written in three different
number systems: decimal (base 10), octal (base 8) and
hexadecimal (base 16).
Beginning programmers rarely, however, use anything
other than decimal integer constants.
28 Decimal integer constant
A decimal integer constant can consist of any
combination of digits taken from the set 0 through 9.
If the constant contains two or more digits, the first
digit must be something other than 0.
29 Integer Constants
Several valid decimal integer constants are shown
below.
0 1 743 5280 32767 9999
30 Integer Constants
The following decimal integer constants are written
incorrectly for the reasons stated.
12,245 illegal character (, ).
36.0 illegal character (.).
10 20 30 illegal character (blank space).
123-45-6789 illegal character
0900 the first digit cannot be a zero.
31 Octal integer constant
An octal integer constant can consist of any
combination of digits taken from the set 0 through
7.
However the first digit must be 0,in order to identify
the constant as an octal number.
Several valid octal integer constants are shown below.
0 01 0743 077777
32 Hexadecimal integer constant
A hexadecimal integer constant must begin with
either Ox or OX.
It can then be followed by any combination of digits
taken from the sets 0 through 9 and A through F
(either upper- or lowercase).
Several valid hexadecimal integer constants are shown
below.
ox ox1 OX7FFF Oxabcd
33 Floating-point constant
A floating-point constant is a base-10 number that
contains either a decimal point or an exponent (or
both).
Several valid floating-point constants are shown below.
0. 1. 0.2
827.602
50000. 0.000743 12.3
315.0066
2 E-8 0.006e-3 1.6667E+8 .12121212e12
34 Character Constants
A character constant is a single character, enclosed in
single quotes.
Several character constants are shown below.
'A' 'X' '3' '?’ ‘'
Notice that the last constant consists of a blank space,
enclosed in apostrophes.
35 Character Constants
Character constants have integer values that are
determined by the computer's particular character set.
Constant Value Constant Value
'A' 65 ‘%’ 37
‘Z’ 90 ‘0' 48
‘a’ 97 ‘‘ 32
‘z’ 122 '7' 55
Program!
36
37 String Constants
A string constant consists of any number of consecutive
characters (including none), enclosed in (double) quotation
marks.
Several string constants are shown below.
"green" "Washington, D.C. 20005H”
"270-32-3456"
"$19.95" "THE CORRECT ANSWER IS:” “2* (
I+3)/J"
“ ” "Line l\nLine 2\nLine 3" ““
38 Input Numbers From Keyboard
There are several methods
The easiest is – scanf() function.
Input from Keyboard
39
40 Escape Sequences
There are certain characters in C when they are
preceded by a backslash (\) they will have special
meaning.
They are used to represent like newline (\n) or tab (\t).
Escape Sequences
41
42 Defining Constants
There are two simple ways in C to define constants:
• Using #define preprocessor.
• Using const keyword.
43 Using #define preprocessor
Following is the form to use #define preprocessor to
define a constant:
Symbolic constants are usually defined at the
beginning of a program.
• Name of the constant • Constant value
• Can be any valid identifier name • Can be character/numeric/string
44 Using #define preprocessor
45 Using const keyword
You can use const prefix to declare constants with a
specific type as follows:
Example
46 Errors!
A C program may have one or more of four types of errors:
Syntax errors (Compiler errors or Compile-time errors)
Linker Errors
Runtime Errors
Semantic Errors
Logic Errors
http://www.cprogramming.com/tutorial/common.html
(Some common programming Errors)
47 Syntax errors
Syntax errors represent grammar errors in the use of the
programming language. Common examples are:
Misspelled variable and function names
Missing semicolons
Unmatched parentheses, square brackets, and curly braces
Using a variable that has not been declared
Incorrect format in selection and loop statements
48 Linker errors
Linker errors are generated when the linker encounters
what looks like a function call; but it cannot find a
function with that name.
This is usually caused by misspelling a C standard
function (like main) or not including the header file for a
function.
49 Runtime errors
A type of error that occurs during the execution of a
program is known as run-time error.
Runtime errors may crash your program when you run it.
Runtime errors occur when a program with no syntax
errors directs the computer to execute an illegal
operation.
Common examples are:
•Trying to divide by a variable that contains a value of zero
•Trying to open a file that does not exist
50
Logic errors
Logic errors occur when a programmer implements the algorithm for solving a problem incorrectly. A
statement with logical error may produce unexpected and wrong results in the program. Common
examples are:
Multiplying when you should be dividing
Adding when you should be subtracting
Opening and using data from the wrong file
Displaying the wrong message
Logic errors are the hardest to find and fix because:
The compiler does not detect these errors
There is no indication of error when the program is executed.
The program may produce correct results for some input data and wrong results for other input data.