Chp_2 - Data Types, Variables
Chp_2 - Data Types, Variables
CHAPTER 2.
Data Types, Variables
CENGİZ RİVA
Tokens in C
• A C program consists of various tokens and a token is either a
keyword, an identifier, a constant, a string literal, or a symbol.
• For example, the following C statement consists of five tokens:
printf
(
"Hello, World! \n“
)
;
Tokens in C
Semicolons:
• In C program, the semicolon is a statement terminator.
• That is, each individual statement must be ended with a
semicolon.
• It indicates the end of one logical entity.
• For example, following are two different statements:
/* my first program in C */
• The header file float.h defines macros that allow you to use
these values and other details about the binary representation
of real numbers in your programs.
C Variables
type variable_list;
type variable_list;
int i, j, k;
char c, ch;
float f, salary;
double d;
Variable Definition in C
int i, j, k;
char c, ch;
float f, salary;
double d;
• Note that small and capital letters have different ASCII code.
Variable Declarations in C
• A variable declaration provides assurance to the compiler that
there is one variable existing with the given type and name so
that compiler proceed for further compilation without needing
complete detail about the variable.
• A variable declaration has its meaning at the time of compilation
only, compiler needs actual variable declaration at the time of
linking of the program.
• A variable declaration is useful when you are using multiple files
and you define your variable in one of the files, which will be
available at the time of linking of the program.
• You can use extern keyword to declare a variable at any place.
Though you can declare a variable multiple times in your C
program but it can be defined only once in a file, a function or a
block of code.
Variable Declarations in C
• For example:
#include <stdio.h>
int main ()
{
// Variable definition:
int a, b;
float f;
// actual initialization
a =10;
b=3;
f=2.829;
Qualifiers
• In addition to the basic data types , there are a number of
qualifiers that can be applied to these basic types.
• short and long apply to integers as:
short int sh;
long int counter;
• The intent is that short and long should provide different lengths
of integers where practical;
• int will normally be the natural size for a particular machine
either 16 or 32 bits.
• short is often 16 bits long, and int long is at least 32 bits.
• The constants refer to fixed values that the program may not
alter during its execution.
• These fixed values are also called literals.
• Constants can be of any of the basic data types like an integer
constant, a floating constant, a character constant, or a string
literal.
• The constants are treated just like regular variables except that
their values cannot be modified after their definition.
Integer Literals
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Floating Point Literals
• A floating-point literal has:
• integer part,
• decimal point,
• fractional part, and
• an exponent part.
• You can represent floating point literals either in decimal form
or exponential form.
• While representing using decimal form, you must include the
decimal point, the exponent, or both and while representing
using exponential form, you must include the integer part, the
fractional part, or both.
• The signed exponent is introduced by e or E.
Floating Point Literals
3.14159 /* valid */
314159E-5L /* valid */
510E /* invalid: incomplete exponent */
210f /* invalid: no decimal or exponent */
.e55 /* invalid: missing integer or fraction */
Type Conversions
(type) expression;
(float) x/2;
Casts
• Casts are often considered operators.
• As an operator, a cast is unary and has the same precedence as
any other unary operator.
• Unary operators are the operators that perform operations on a
single operand to produce a new value.
• Although casts are not usually used much in programming, there
are times when they can be very useful.