0% found this document useful (0 votes)
31 views35 pages

C Types, Variables and Constants

This document discusses C data types, variables, constants, and input/output. It covers the basic C data types including integers, floats, characters, and void. Variables are declared with a type and can be initialized. Constants cannot be changed once declared. The document provides examples of declaring variables and constants of different types and rules for identifier names. It also introduces basic input/output statements.

Uploaded by

Vinod Kondawar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views35 pages

C Types, Variables and Constants

This document discusses C data types, variables, constants, and input/output. It covers the basic C data types including integers, floats, characters, and void. Variables are declared with a type and can be initialized. Constants cannot be changed once declared. The document provides examples of declaring variables and constants of different types and rules for identifier names. It also introduces basic input/output statements.

Uploaded by

Vinod Kondawar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

C Types, Variables and Constants

Objectives
❏ To be able to create good identifiers for objects in a program.
❏ To be able to list, describe, and use the C basic data types.
❏ To be able to create and use variables and constants.
❏ To understand input and output concepts.
❏ To be able to use simple input and output statements.

Programming Concepts and Methodology 1


1. Identifiers

One feature present in all computer languages is the


identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address. If we didn’t
have identifiers that we could use to symbolically
represent data locations, we would have to know and
use object’s addresses. Instead, we simply give data
identifiers and let the compiler keep track of where
they are physically located.

Programming Concepts and Methodology 2


Table 1 Rules for Identifiers

Programming Concepts and Methodology 3


Note
An identifier must start with a letter or underscore:
it may not have a space or a hyphen.

Programming Concepts and Methodology 4


Note
C is a case-sensitive language.

Programming Concepts and Methodology 5


Table 2 Examples of Valid and Invalid Names

Programming Concepts and Methodology 6


2. C Types
A type defines a set of values and a set of operations
that can be applied on those values. For example, a
light switch can be compared to a computer type. It
has a set of two values, on and off. Only two
operations can be applied to a light switch: turn-on
and turn-off.

Topics discussed in this section:


Void Type
Integral Type
Floating-Point Types
Programming Concepts and Methodology 7
FIGURE 1 Data Types

Programming Concepts and Methodology 8


FIGURE 2 Character Types

Programming Concepts and Methodology 9


FIGURE 3 Integer Types

Programming Concepts and Methodology 10


Note
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)

Programming Concepts and Methodology 11


Table 3 Typical Integer Sizes and Values for Signed Integers

Programming Concepts and Methodology 12


FIGURE 4 Floating-point Types

Programming Concepts and Methodology 13


Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)

Programming Concepts and Methodology 14


Table 4 Type Summary

Programming Concepts and Methodology 15


3. Variables

Variables are named memory locations that have a type,


such as integer or character, which is inherited from
their type. The type determines the values that a variable
may contain and the operations that may be used with
its values.

Topics discussed in this section:


Variable Declaration
Variable Initialization

Programming Concepts and Methodology 16


FIGURE 5 Variables

Programming Concepts and Methodology 17


Table 5 Examples of Variable Declarations and Definitions

Programming Concepts and Methodology 18


FIGURE 6 Variable Initialization

Programming Concepts and Methodology 19


Note
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.

Programming Concepts and Methodology 20


PROGRAM Print Sum of Three Numbers

Programming Concepts and Methodology 21


PROGRAM Print Sum of Three Numbers (continued)

Programming Concepts and Methodology 22


PROGRAM Print Sum of Three Numbers (continued)

Programming Concepts and Methodology 23


4 Constants

Constants are data values that cannot be changed


during the execution of a program. Like variables,
constants have a type. In this section, we discuss
Boolean, character, integer, real, complex, and string
constants.

Topics discussed in this section:


Constant Representation
Coding Constants

Programming Concepts and Methodology 24


Note
A character constant is enclosed in single quotes.

Programming Concepts and Methodology 25


Table 6 Symbolic Names for Control Characters
Programming Concepts and Methodology 26
Table 7 Examples of Integer Constants

Programming Concepts and Methodology 27


Table 8 Examples of Real Constants

Programming Concepts and Methodology 28


Note
The two components of a complex constant must be of the
same precision, that is, if the real part is type double,
then the imaginary part must also be type double.

Programming Concepts and Methodology 29


Table 9 Examples of Complex Constants

Programming Concepts and Methodology 30


FIGURE 7 Some Strings

Programming Concepts and Methodology 31


FIGURE 8 Null Characters and Null Strings

Programming Concepts and Methodology 32


Note
Use single quotes for character constants.
Use double quotes for string constants.

Programming Concepts and Methodology 33


PROGRAM Memory Constants

Programming Concepts and Methodology 34


PROGRAM Memory Constants (continued)

Programming Concepts and Methodology 35

You might also like