Chapter 3: Basic C Concepts
Chapter 3: Basic C Concepts
C Fundamentals
3 Hours
~~ 4-5 marks
Chapter Outlines
1. Character Set
2. Identifiers and Keywords
3. Data types and Modifiers
4. Constants and Variables
5. Declaration and initialization of variables
6. Escape Sequences
7. Preprocessor Directives
8. Typedef Statements
9. Symbolic Constants
3.1 Character Sets
• C language contains a set of characters used to construct words,
statements, expressions, etc.
2. Digits
• C language supports 10 digits which are used to construct numerical
values in C language.
• Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
3. Special Characters
• Special symbols to perform mathematical operations, to check conditions, etc.
• Special Symbols - ~ ! @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ |
• Some symbols such as ?, \, ‘, “ cannot be used openly for display purpose, as
they mean other operations too. Hence they require use of white spaces if they
are to be displayed.
4. White Spaces
• Combination of characters that help to provide structure to the programming
code as per user’s ease.
• E.g. \n new line, \t horizontal tab, \? Question mark
5
3.2 Keywords and Identifiers
1. Keywords
• Keywords are system reserved words that cannot be used for other purposes (i.e.
naming variables)
8
Rules for naming identifiers
• A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
• The first letter of an identifier should be either a letter or an
underscore.
• You cannot use keywords as identifiers.
• There is no rule on how long an identifier can be. However, you may
run into problems in some compilers if the identifier is longer than
31 characters.
3.3 Data types and specifiers
• Data types specify the nature of data being stored in a variable.
• Data types are used to declare the type of variable being used in the program.
• Data types specify how the user data are entered and stored in the program.
4. Void A data type that has no value. This is used to specify functions
that return or denote nothing. Variable cannot be created using this data
type. (keyword void, specifier no specifier)
12
Derived Data Type
13
3.4 Variable and constants
• Constants are the quantities that don’t change during program execution.
• These store fixed values that cannot be altered by the program during execution.
• Keyword for constant is const. This keyword can be placed before or after data type.
• E.g. const int mark=10 and int const mark=10 are both valid approach.
• Constants can also be categorized as literal constants (directly typed number values) and
symbolic constants (#define pi 3.14) lowercase letters.
• Variables are the elements that can store data for further operations.
These variables can change the value whenever the programmer specifies or
wherever required by the program.
Since C program is case sensitive, the naming and usage of variables and
constants is a very important issue.
Once defined in a certain way, you need to use same name for that variable, and in
the same way that it is written.
For e.g. int roll; and int Roll; are treated two different variables for C.
• Since variable is an identifier ,the rules for naming a variable are similar to
those of an identifier.
Rules for naming a variable:
• A variable name can have only letters (both uppercase and lowercase letters), digits and
underscore.
• The first letter of a variable should be either a letter or an underscore.
• There is no rule on how long a variable name can be. However, you may run into
problems in some compilers if the variable name is longer than 31 characters.
3.5 Declaration and initialization of variables
Variable Declaration
<Data Type> VariableName ;
For e.g.
int roll ; float percent;
Multiple variables of same data type can be declared in same line using comma operator
between them.
For e.g.
int roll ; int id; int xyz;
can also be written as
int roll, id, xyz;
Data initialization
<Data Type> VariableName = value;
For e.g.
int roll = 5;
char result = ‘p’;
float science = 52.5, english = 22, nepali = 44.5;
• Commands used in preprocessor are called preprocessor directives and they begin with
hash “#” symbols.
• A C pre-processor is just a text substitution tool and it instructs the compiler to do required
pre-processing before the actual compilation.
• Syntax:
typedef existing_data_type new_name_to_data_type;
• Example:
typedef int INTEGER;
• After this type definition, the identifier INTEGER can be used as an abbreviation for the
type int .
• We can also use typedef to give a name to user defined data types as well like structure
and union.
3.9 Symbolic Constants
• A name that is used in place of a sequence of characters.
• The characters may represent a numeric constant or a character constant or a string constant.
• Syntax:
#define symbolic_constant_name value
• Example:
#define COUNT 5
End of Chapter
Important Questions