0% found this document useful (0 votes)
6 views31 pages

2. C_programming1

A C program consists of preprocessor commands, functions, variables, statements, expressions, and comments. Key components include the mandatory main() function, variable declarations, and the use of built-in functions like printf(). Additionally, C supports various data types, identifiers, keywords, and escape sequences for enhanced functionality.

Uploaded by

resam.zaha.hp
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)
6 views31 pages

2. C_programming1

A C program consists of preprocessor commands, functions, variables, statements, expressions, and comments. Key components include the mandatory main() function, variable declarations, and the use of built-in functions like printf(). Additionally, C supports various data types, identifiers, keywords, and escape sequences for enhanced functionality.

Uploaded by

resam.zaha.hp
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/ 31

Structure of C Program

A C program basically consists of the following parts:


• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments

1
Structure of C Program
Preprocessor Commands

Main Function

Comment
Library Function

End of Program

2
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.

3
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.

4
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


5
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.

6
Structure of C Program
Preprocessor Commands

Main Function

Comment
Library Function

End of Program

7
C’s Basic Data Type
Type Keyword format Memory
Specifier Requirements
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 ---

8
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 (;).

9
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.)

10
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.

11
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.

12
Variable (Cont.)
Can not Use blank space

Can use letter of


Can not Use any keyword
alphabet (A-Z, a-z)

The first character must


Variable Digits (0-9)
be a letter

Can not start a variable


name with digit Underscore (_)
13
Is it Valid Variable Name?

Apon 1joty

joty-5
apon

apon123 this_is_a_long_name

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


15
Identifiers
• Identifiers are names that are given to various program
elements, such as variables, functions and arrays.

16
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

17
Keywords

18
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 ==, !=, <, >, <=, >=

19
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
;
20
21
Input Numbers From Keyboard

• There are several methods


• The easiest is – scanf() function.

• Input from Keyboard

22
23
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).

24
Escape Sequences

25
Defining Constants
• There are two simple ways in C to define constants:
• Using #define preprocessor.
• Using const keyword.

26
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

27
Using #define preprocessor

28
Symbolic Constants

30
31

You might also like