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

Chapter 3: Basic C Concepts

This document provides an overview of key C programming fundamentals covered in Chapter 3, including character sets, data types, variables, constants, and preprocessor directives. It discusses the different categories of characters in C and rules for identifiers and keywords. It describes fundamental data types like int, float, char, and void, as well as derived types like arrays and pointers. It also covers declaring and initializing variables, escape sequences, and the typedef and #define preprocessor directives for defining macros and giving types new names.

Uploaded by

Ananta Adhikari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
34 views31 pages

Chapter 3: Basic C Concepts

This document provides an overview of key C programming fundamentals covered in Chapter 3, including character sets, data types, variables, constants, and preprocessor directives. It discusses the different categories of characters in C and rules for identifiers and keywords. It describes fundamental data types like int, float, char, and void, as well as derived types like arrays and pointers. It also covers declaring and initializing variables, escape sequences, and the typedef and #define preprocessor directives for defining macros and giving types new names.

Uploaded by

Ananta Adhikari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 31

Chapter 3

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.

• Has a set of characters which include alphabets, digits, and special


symbols.

• C language supports a total of 256 characters and grouped into FOUR


categories.
1. Alphabets or letters
2. Digits
3. Special Characters
4. White spaces
1. Alphabets
• C language supports all the alphabets from the English language.
• Lowercase letters - a to z
• Uppercase letters - A to Z

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)

• Keywords have predefined meanings and scopes.


• These scopes cannot be changed by the user(programmer)

• Since keywords are predefined, they use a specific syntax.

• Keywords are always written in lowercase letters.

• E.g. if, return, char, static, while


2. Identifiers
• Every word in C programming language is either an identifier, or a keyword

• Identifier is the name given to a variable, a constant, or a function.

• There are certain rules while naming identifiers:


• Identifier must always start with an alphabet. ‘Underscore’ is permitted too.
• All identifiers are associated with certain keyword(s).
• Identifier can have digits on the back of starting letter.

• Because C is case-sensitive, Uppercase and lowercase identifiers are treated as


different entities.
• E.g. sum, Babu_Rao, dataResult2, _division

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.

• Different data types have different memory requirements.

• There are three kinds of data types in C language:


1. Fundamental / Primitive / Basic data types  int, char, float, void
2. Derived data types  array, pointer
3. User defined data types  structure, union, enum
1. Fundamental Data Types
1. Int  denotes an integer number (+ve or –ve). Holds 2 bytes of space to
store a number. (keywordint , specifier  %d)

2. Float denotes a floating point number (decimal number). Holds 4


bytes of space to store a number. (keywordfloat, specifier  %f)

3. Char denotes a single alphabet or character. Holds 1 bytes of space to


store a character. (keyword char, specifier  %c)

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

5. Array A collection of similar data type elements.

6. pointer A variable that points to the address of another variable.

7. Structure A collection of dissimilar data type elements.

8. Union  similar to structure, but differs in terms of size and


implementation behavior.

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.

• There are 4 kinds of constants:


• Integer constants: 12345, 0526, 0x4AB
• Floating point constants: 87.9802, 123E05
• Character constants: ‘A’, ‘g’, ‘$’, ‘8’
• String constants: “ACME”, “Jai Nepal”, “Rs. 55”

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

• Each variable has a unique kind of identifier (name). Each variable is


associated with a data type, which represents the nature of the data that
it holds.
Some variables can store an integer number, some can store words, some can store
special characters, and so on.

 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;

The values can also be assigned to the variable later .


For e.g.
int marks;
marks = 72;
3.6 Escape Sequences
• These are the non-printing characters in C language.
• These are composed of two or more characters starting with back
slash ( \ )
• It causes escape from the normal interpretation of a string, so the
next character is recognized as the one having special meaning.
• E.g.:
Escape sequence Description Example Output
\n New line printf("Hello \n World"); Hello
World
\t Horizontal tab printf("Hello \t World"); Hello World
\' Single quote printf("Hello \'World\' "); Hello 'World'
\" Double quote prinf("Hello \"World\" "); Hello "World"
\\ Backslash printf("Hello \\World"); Hello \World
3.7 Preprocessor Directives
• Before a C-program is compiled ,source code is preprocessed by a program called
preprocessor and the process is called preprocessing.

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

• The C preprocessor is a macro processor that is used automatically by the C compiler to


transform user’s program before actual compilation.
• It is called a macro processor because it allows the user to define macros, which are brief
abbreviations for longer constructs. (E.g. #define pi 3.14159265)
• A C preprocessor makes 3 transformations on all kinds of input it receives:
1. Replacing comments with single spaces
2. Deleting Backslash-Newline sequences
3. Replacing macro names with their expansions

• Pre-processor directives are lines in the program that start with #.


• This # is followed by an identifier that is the directive name.
• For e.g. ‘#define’ is the directive that defines a macro.
3.8 typedef statements
• The C provides a keyword called typedef, which you can use to give a type(data type) a
new name.

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

• When a program is compiled ,each occurrence of symbolic constant is replaced by its


corresponding character sequence (macro substitution during preprocessing).

• Symbolic constants are defined before the main() function.

• Syntax:
#define symbolic_constant_name value
• Example:
#define COUNT 5
End of Chapter
Important Questions

• Very short Questions [2 marks]


1.
2.
3.

• Short Questions [4 marks]


1.
2.
3.

• Long Questions [8 marks]


1.
2.
3.

You might also like