0% found this document useful (0 votes)
17 views47 pages

Slide 2 - Declare Variable and Assign Value (1)

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

Slide 2 - Declare Variable and Assign Value (1)

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

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 4 Byte
Floating-point numbers float %f 4 Byte
Double-precision floating- double %lf 8 Byte
point number
valueless void ---
10 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.)
11 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.
12 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.)
13
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 ($)
14 Is it Valid Variable Name?

Apon 1joty

joty-5
apon

apon123 this_is_a_long_name

_sojeb_1 VaReNdRa
15 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)


16 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 (;).
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 Integer Constants
 Several valid decimal integer constants are shown
below.
0 1 743 5280 32767 9999
29 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
30 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.
31 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
32
33 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" ““


34 Input Numbers From Keyboard

 There are several methods


 The easiest is – scanf() function.

 Input from Keyboard


35
36 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
37
38 Defining Constants
 There are two simple ways in C to define constants:
• Using #define preprocessor.
• Using const keyword.
39 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
40 Using #define preprocessor
41 Using const keyword
 You can use const prefix to declare constants with a
specific type as follows:

Example
42 Errors!
 A C program may have one or more of five types of errors:
 Syntax errors (Compiler errors or Compile-time errors)

 Linker Errors

 Runtime Errors

 Logic Errors

 Semantic Errors

 http://www.cprogramming.com/tutorial/common.html
(Some common programming Errors)
43 Syntax errors
Syntax errors represent grammar errors in the use of the
programming language. Common examples are:
 Misspelled variable and function names
int num1; num = 10;

 Missing semicolons int num1; num1 = 10

 Unmatched parentheses, square brackets, and World”


printf(“Hello curly braces
;

 Using a variable that has not been declared


int num1; num1 = a+10;

 Incorrect format in selection and loop statements


else if
int x = 10;
44 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
#include <stdio.h>
function. int Main() {
int a=78;
printf("The value of a is : %d", a);
return 0; }
45 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
int ab=2/0;
value of zero
46
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.
47
Semantic errors
Semantic errors are the errors that occurred when the statements are not
understandable by the compiler. The following can be the cases for the semantic error:
 Use of a un-initialized variable.

int i; i=i+2;
 Type compatibility
int b = "javatpoint"; if (5 > 2.5)
 Errors in expressions
int a, b, c;
a+b = c;
 Array index out of bound

int a[10]; a[10] = 34;

You might also like