0% found this document useful (0 votes)
5 views

Chp_2 - Data Types, Variables

This document covers fundamental concepts of data types and variables in the C programming language, including tokens, identifiers, keywords, and the importance of semicolons and comments. It details the various data types, their sizes, value ranges, and how to declare and initialize variables, along with the use of constants and literals. Additionally, it discusses type conversions and casting in C, providing a comprehensive overview for beginners in programming.

Uploaded by

elyesaposlu31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chp_2 - Data Types, Variables

This document covers fundamental concepts of data types and variables in the C programming language, including tokens, identifiers, keywords, and the importance of semicolons and comments. It details the various data types, their sizes, value ranges, and how to declare and initialize variables, along with the use of constants and literals. Additionally, it discusses type conversions and casting in C, providing a comprehensive overview for beginners in programming.

Uploaded by

elyesaposlu31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

PROGRAMMING I

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");

• The individual tokens are:

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:

printf("Hello, World! \n");


return 0;

• This syntax is correct:


printf("Hello,
World! \n");
return 0;
Tokens in C
Comments:
• Comments are like helping text in your C program and they are
ignored by the compiler.
• They start with /* and terminates with the characters */ as
shown below:

/* my first program in C */

• Anything within those will be considered as comment.


• If only some part in one line to be commented then // signs to
be used as shown:

printf("Hello, World! \n"); // prints the text on screen


Tokens in C
Identifiers:
• A C identifier is a name used to identify a variable, function, or any
other user-defined item.
• An identifier starts with a letter A to Z or a to z or an underscore _
followed by zero or more letters, underscores, and digits (0 to 9).
• C does not allow punctuation characters such as @, $, and % within
identifiers.
• C is a case sensitive programming language.
• Thus, Manpower and manpower are two different identifiers in C.
• Here are some examples of acceptable identifiers:

Nbr_max ch_1 _temp name_abc str_char


Tokens in C
Keywords:
• The following list shows the reserved words in C. These
reserved words may not be used as constant or variable or any
other identifier names.
Tokens in C
Whitespace in C:
• A line containing only whitespace, possibly with a comment, is
known as a blank line, and a C compiler totally ignores it.
• Whitespace is the term used in C to describe blanks, tabs,
newline characters and comments.
• Whitespace separates one part of a statement from another
and enables the compiler to identify where one element in a
statement, such as int, ends and the next element begins.
• Therefore, in the following statement: int age

fruit = apples + oranges;

• No whitespace characters are necessary between fruit and =, or


between = and apples, although you are free to include some if
you wish for readability purpose.
C Data Types
• In the C programming language, data types refer to an extensive
system used for declaring variables or functions of different types.
• The type of a variable determines how much space it occupies in
storage and how the bit pattern stored is interpreted.
• The types in C can be classified as follows:
Basic Types
• Following table gives you details about standard integer types
with its storage sizes and value ranges:
Basic Types
• Following table gives you details about standard integer types
with its storage sizes and value ranges:

Type Storage size Value range


Char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
Int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
Short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
Long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
Floating Point Types
• Following table gives you details about standard floating-point
types with storage sizes and value ranges and their precision:

• 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

• A variable is nothing but a name given to a storage area that


our programs can manipulate.
• Each variable in C has a specific type, which determines the size
and layout of the variable's memory; the range of values that
can be stored within that memory; and the set of operations
that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and
the underscore character.
• It must begin with either a letter or an underscore.
• Upper and lowercase letters are distinct because C is case-
sensitive.
C Variables

• The basic variable types are:

• C programming language also allows to define various other


types of variables, which we will cover later.
Variable Definition in C
• A variable definition means to tell the compiler where and how
much to create the storage for the variable.
• A variable definition specifies a data type and contains a list of
one or more variables of that type as follows:

type variable_list;

• Here, type must be a valid C data type including char, int,


float, double, bool or any user-defined object, etc., and
variable_list may consist of one or more identifier names
separated by commas.
Variable Definition in C

type variable_list;

• Some valid declarations are shown here:

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;

• The line int i, j, k both declares and defines the variables


i, j and k; which instructs the compiler to create variables
named i, j and k of type int.
Variable Definition in C
• Variables can be initialized (assigned an initial value) in their
declaration.
• The initializer consists of an equal sign followed by a constant
expression as follows:

type variable_name = value;

• Some examples are:

int d = 3, f = 5; // definition and initializing d and f.

double z = 22,86; // definition and initializes z.


char ch = 'x'; // the variable x has the value 'x'.
Char Variables
• A char variable is an integer, written as one character within
single quotes, such as 'x'.
• The value of a character constant is the numeric value of the
character in the machine's character set, ASCII, but using
formatted output it will be displayed as a character 'x'.
• For example, in the ASCII character set the character constant
'0' has the value 48, which is unrelated to the numeric value 0.
x = ch - '0';

• Returns integer value of number stored as character.


• Character constants participate in numeric operations just as
any other integers, although they are most often used in
comparisons with other characters.
ASCII-Table
Variable Definition in C
• In order to print '!' we write the ASCII code of '!‘.

#include <stdio.h>int main()


{
printf("Hello World %c",33);
return 0;
}

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

• Each compiler is free to choose appropriate sizes for its own


hardware.
Qualifiers

• The qualifier signed or unsigned may be applied to char or


any integer.
• unsigned numbers are always positive or zero, and obey the
laws of arithmetic modulo 2n, where n is the number of bits in
the type.
• For instance, if chars are 8 bits, unsigned char variables have
values between 0 and 255, while signed chars have values
between -128 and 127.
• MSB is used for sign therefore 27 = 128.
C Constants and Literals

• 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

• An integer literal can be a decimal, octal, or hexadecimal


constant.
• A prefix specifies the base or radix:
• 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
• An integer literal can also have a suffix that is a combination of
U and L, for unsigned and long, respectively.
• The suffix can be uppercase or lowercase and can be in any
order.
• For example, decimal 31 can be written as 037 in octal and
0x1f or 0x1F in hex.
• 0XFUL is an unsigned long constant with value 15 decimal.
C Constants and Literals
• Following are other examples of various types of 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

• Here are some examples of 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 conversion refers to the situation in which variables of one


type are mixed with variables of another type.
• When this occurs in an assignment statement, the type
conversion rule is very easy:
• The value of the right side of the assignment is converted to the
type of the left side, target variable.
• If the conversion are between longer type to shorter type such as
integer to character, high order bits will be removed.
• If inside a type conversion expression one is longer type and the
other is shorter the result will be converted to longer type such
as addition of an integer and double.
Casts
• It is possible to force an expression to be of a specific type by
using a construct called a cast.
• The general form of a cast is:

(type) expression;

where type is one of the standard C data types.


• For example, if you wish to make sure the expression x/2 was
evaluated to type float while x is int, you could write it as follows:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy