0% found this document useful (0 votes)
29 views36 pages

Unit 1 - II Consts Vars and Data Types

This document discusses various fundamental concepts in C programming such as constants, variables, data types, and memory layout. It defines constants as fixed values that do not change during program execution. Variables are names that can store changing data values and must be declared with a data type. The primary data types in C are integer, character, floating point, double, and void. Integer types include signed and unsigned integers. Variables and constants occupy memory, and an executing C program's memory layout includes the executable file loaded into memory.

Uploaded by

Thomas Mathew
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)
29 views36 pages

Unit 1 - II Consts Vars and Data Types

This document discusses various fundamental concepts in C programming such as constants, variables, data types, and memory layout. It defines constants as fixed values that do not change during program execution. Variables are names that can store changing data values and must be declared with a data type. The primary data types in C are integer, character, floating point, double, and void. Integer types include signed and unsigned integers. Variables and constants occupy memory, and an executing C program's memory layout includes the executable file loaded into memory.

Uploaded by

Thomas Mathew
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/ 36

Constants,

Variables and Data


Types
C PROGRAMMING 🡺 UNIT 1 PART 2
General Structure of a C Program 2
Hello World C Program Including header file
that defines printf, in
to our program.
#include<stdio.h>
// This is a single line comment.
The mandatory main
/* This is a multiline function in every C
Comment. Compiler ignores us ☹ */
Program.

int main()
{ printf function called to display the
string “Hello World” to standard
printf(“Hello World”); output device (display).
return 0;
Main function returns value
} 0,with that the program
ends/terminates.
C Character Set 4

❑ Letters : Uppercase A…Z, Lowercase a…z


❑ Digits : All decimal digits 0 to 9
❑ White space Characters: Blank space (' '), new line(\n),
horizontal tab(\t) etc.
❑ Special Characters : ,(comma) .(period) ; (semicolon)
:(colon), & (ampersand), # (number sign or hash), ( (left
parenthesis), [ (left bracket), { (Left brace) etc. See the
table for complete list.
C Character Set – Special characters 5
Symbol Meaning
~ Tilde ) Right parenthesis } Right brace
! Exclamation mark _ Underscore [ Left bracket
# Number sign(hash) + Plus sign ] Right bracket
$ Dollar sign | Vertical bar : Colon
% Percent sign \ Backslash ” Quotation mark
^ Caret ` Apostrophe ; Semicolon
& Ampersand – Minus sign < Opening angle
bracket
* Asterisk = Equal to sign > Closing angle bracket

( Left parenthesis { Left brace ? Question mark


, Comma
. Period
/ Slash
C Tokens 6

C Token: Smallest individual meaningful units in a C program are called Tokens. A Token
consists of 1 or more characters from the C Character Set.

C Tokens

Operator
Keyword Constant
(+,*,-,/)
(e.g. int, void) (e.g. 65,’C’

Identifier String
(e.g. main, sum) “hello”
Keywords 7

❑ Keywords are the reserved words in C whose meanings are fixed by


the language.
❑ Keywords are case sensitive. i.e., while and WHILE are not the same.
❑ In C, All keywords are of lowercase.
❑ Keywords in C is analogous to words of a natural language like
English (how, when,…).
❑ Keywords cannot be used as variable names. ( E.g. int char = 10; is
not allowed in C since char is keyword)
❑ All keywords are made of C Character set only.
Keywords 8

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifier 9

❑ Names defined by programmer in a program


are called Identifiers.
❑ These names can be variable names, function
names etc.
❑ E.g.: x, y, sum, add() etc.
Rules for writing an identifier 10

1. A valid identifier can have letters (both uppercase and lowercase


letters), digits and underscore.
2. The first letter of an identifier should be either a letter or an
underscore. However, it is discouraged to start an identifier name
with an underscore.
3. There is no rule on length of an identifier. However, the first 31
characters of identifiers are discriminated by the
compiler.(Depends on the compiler)
4. Cannot use a keyword as identifier.
5. Identifier must not contain white spaces. ( student name is not
valid identifier whereas student_name is valid)
Identifiers 11

Question: Which are the valid identifiers in C in the


following??
❑ 9Sum ,
❑ sum
❑ _xyz
❑ result
❑ Result
❑ total amount
❑ loss$#amount
Constants 12
● Constants are fixed values that do not change during execution of the program.
● E.g: 5, 100, “hello” , 14.52, 'A', 'b' etc...
Variables 13

❑ A variable is a name in the program that may be


used to store a data value.
❑ Variable names fall into category of Identifiers.
❑ Unlike constants, which remain unchanged during
the execution of the program, a variable may take
different value at different time.
Rules to declare variable 14

❑ They must begin with a letter or underscore(_)


❑ Remaining characters may be letters,digits or underscore.
❑ Both lowercase and uppercase letters are distinct.
❑ Example: Total and total two different variable names.
❑ Variable name should not be a keyword in C.
❑ White space, Dollar sign etc. are not allowed.
❑ Example: John, delhi, First_tag, int_type ==> valid variable
names
❑ Price$, char, group one, (area) ==> Invalid variable names
DATA TYPES in C 15

❑ Every data used in a C program must a have type


associated with it. That is referred as data type.

❑ E.g: 25 is of integer data type. 2.53 is of floating


point type etc.

❑ Since data is normally stored in variables, we


specify data type of a variable when we declare the
variable name.
C data categories 16

The data type categories supported by ANSI C are:


❑ Primary(Fundamental/basic) data types
❑ Derived Data types
❑ User-defined data types
Primary datatypes 17
The 5 Primary Data Types are following (Keyword for each in
bracket):
Integral Types:
1. Integer (int) - E.g. 5,7,100,-55…..
2. Character (char) - 'A', 'b', '#', '7'.....'\n',
Floating Point types:
3. Floating point(float) - 5.67f, 3.24f,6.0f...
4. Double-precision floating point(double) - 5.67,5.43,...
Void Type:
5. void type(void)
Secondary
storage(Hard disk, hello.exe
Flash
memory,CD,DVD)

An executable file copied


into main memory before
execution.
Memory layout of an executing C pgm 19

Memory layout of running program


Not in syllabus. But good to know (source: https://www.geeksforgeeks.org/memory-layout-of-c-program/ )
Size of a datatype/variable 20
● Size of datatype/variable refers to number of bytes occupied in memory.

int x = 1;
1006
char y = ‘A’; 1005
31(decimal) = 0000000000011111(binary) 1004
8 bits = 1 Byte y 01000001 1003
1024 bytes = 1 KiloByte(KB) 1002
1024 KB = 1 Mega Byte(MB) 1001
x 00000000
1024 MB = 1 Giga Byte(GB) 1000
00000001
1024 GB = 1 Tera Byte (TB)

View of RAM/primary memory


Integer Type(int) 21

❑ Integers are whole numbers with a range of values supported by


a particular machine.
❑ There are signed integer and unsigned integer.
❑ Signed integer uses one bit for sign and other bits for magnitude
of the number.
❑ Unsigned integers are always positive (0 or above).

❑ They do not reserve any bit for representing sign, i.e., they use all
the bits for storing the magnitude of the number.
Integer Type(int) 22

❑ Different integer types are short int, int, long int, unsigned int etc.
Size of each will vary.
❑ Normally short int is smallest, then int and followed by long int,
the largest among three.
❑ short, long, unsigned etc. are called as modifiers for the basic
data type. These are keywords of C language.

❑ Size of data type is the number of bytes(memory space) reserve


for any variable of that type.
Primary data types 23
Integer Type(int) 24
Integer Type(int) 25

❑ Size of a data type determine the range of values that can be


stored in the variables of that data type.

❑ By using equation -2^n to + (2^n)-1 we can find out the range


of signed integer(int) data type.

❑ Where n is the number of bits available for storing the data part
(magnitude) of the number(Not the sign).
Integer Type (int) 26

❑ For unsigned numbers, the formula is 0 to + (2^n)-1. Here n is the


size of the data type in bits. For e.g.,
❑ On a typical 16-bit computer system, an int has the size 2 bytes(
i.e. 16 bits).
❑ On this system, For signed integers, n = 15 since 1 bit is used for
storing the sign.
❑ Hence the range is -2^15 to + (2^15)-1 (i.e -32768 to +32767)
❑ For unsigned integers, n = 16. Hence the range of values is 0 to +
(2^16)-1 (i.e . 0 to 65535)
❑ Then range of values supported by different Integer types is as
shown in Table.
Integer Type(int) 27
Character Type (char): 28

❑ Character type is used to represent single character in C. E.g. 'a'


, 'A', '1','$' ,'\n','\t'
❑ A character constant in C is represented by enclosing a
character in single quotes.
❑ Keyword for character data type is char
❑ E.g: char a = 'p', choice = '1';
❑ Declared 2 character variable a and choice and both initialized
with values
❑ Character can be signed or unsigned.
Character Type (char): 29

❑ unsigned char variable can store values from 0 to 255 using


it size of 8 bits(1 byte)
❑ signed char support range from -128 to 127
❑ Character type belongs to integral data type
Floating Point (float): 30

❑ Used to represent real numbers.


❑ Floating point numbers are defined in C by the keyword float.
Typical size of float type is 32 bits(4 bytes)
❑ E.g. float x = 5.2 ,y; // Declared two float variables. One is
initialized with a value.
❑ In fact, A floating point constant is represented as 5.2f ( with a
suffix f). Just 5.2 is treated as a double type by compiler. In
practice, this difference hardly matters.
❑ %f for float
Double type(double) 31

❑ When the accuracy provided by float is not sufficient


double data type is used. It uses 8 bytes(64 bits) giving a
precision of 14 digits.
❑ E.g. double p = 121.243 ,q; // Declared two double variables.
One is initialized with a value
❑ When you want to extend more precision you can use the
long double data type.
❑ E.g. long double r = 53434.234 ,s; // Declared two double
variables. One is initialized with a value
❑ %lf for double
Relative size of real datatypes in C 32
void data type 33

❑ void type has no values. Can not declare variables of


void type.
❑ Used to specify type of functions in C.
❑ Type of a function is said to be void when it does not
return any value to the calling function.
❑ E.g: void display(int a, int b)
❑ Keyword used is void
❑ void x; // wrong
Format Specifier for different Data types 34

tytypestypespiler(GCC) )
❑ short int ==> %hi or %hd
❑ unsigned short int ==> %hu
❑ char ==> %c
❑ int ==> %d
❑ unsigned int ==> %u
❑ long int ==> %ld
❑ unsigned long int ==> %lu
❑ float ==> %f double ==> %lf
❑ long double ==>%Lf
Note: w.r.t GNU C Compiler(GCC)
Declaration of Variables 35

❑ General Syntax is:


data-type v1,v2,...vn;
where data-type is any available data type in C, and v1,v2....vn are
variable names.
❑ E.g for Variable declaration(different ways)
int count; // simple decln. of 1 variable name as count. Not
initialized
int number, total; // Declaring 2 variables simultaneously
double d =3.25; //Declaring variable and initializing. Variable
d now stores values 3.25
Declaration of Variables 36

❑ Older versions of C compilers the insisted on declaring all the


variables in a function at the beginning of the function. But
recent versions (C99 onwards), variables can b declared
anywhere in the function, just before its first use.

❑ You cannot use a variable before it is declared.

❑ Can’t declare two variables of same name in one scope (e.g. in a


function).

You might also like