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

Constants, Variables, & Data Types: Prepared By:vipul Vekariya

The document provides an overview of constants, variables, and data types in C programming. It discusses integer constants, float constants, character constants, and string constants. It also covers variable declaration and definition, standard data types like int, char, float, and void. Additionally, it summarizes formatted input/output functions like printf and scanf, and their format codes. User-defined data types using typedef and enumerated data types are also introduced.

Uploaded by

Jay Tadhani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views

Constants, Variables, & Data Types: Prepared By:vipul Vekariya

The document provides an overview of constants, variables, and data types in C programming. It discusses integer constants, float constants, character constants, and string constants. It also covers variable declaration and definition, standard data types like int, char, float, and void. Additionally, it summarizes formatted input/output functions like printf and scanf, and their format codes. User-defined data types using typedef and enumerated data types are also introduced.

Uploaded by

Jay Tadhani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

Lecture 2: Constants, Variables, & Data Types

Prepared by :Vipul Vekariya

Character Set
Letters Digits Special Characters White Spaces Note: for c character set follow the page no 24.

Trigraph sequence
If any keyboard not support the special character then apply follow method. Trigraph sequence Translation ??= # ??( [ ??) ] ??~

C Tokens

Keywords & Identifier


Every C word is classified as either a keyword or an identifier. All keyword have fixed meaning and these can not be changed. Example : IF, FOR, break, else, char, int, const, main, void, Identifiers refers to the names of variable, function, and arrays. These are user defines.

Rules for Identifiers


First character must be alphabetic or underscore.

Must consist only of alphabetic characters, digits, or underscores.


Only the first 31 characters of an identifier are significant and are recognized by the compiler. Cannot use a keywords or reserved word (e.g. main, include, printf & scanf etc.). C is case sensitive, e.g. My_name my_name

Constants

Constants

Constants are data values that cannot be changed during the execution of your program. Like variables, constants also have a type. There are four kinds of constants in C :

Integer Constants Floating-point Constants Character Constants String Constants

Integer Constant

Although integers are always stored in their binary form, they are simply coded as you would use them in everyday life. (e.g. value of fifteen is coded as 15). If you code the number as a string of digits, its type is signed integer, or long integer if the number is large. You can override the default type by specifying unsigned (u or U) and long (l or L) after the number.

Examples of Integer Constants


Literal +123 -378 -32271L 76542LU Value 123 -378 -32,271 76,542 Type int int long int unsigned long int

Float Constants

Float constants are numbers with decimal parts. They are stored in memory as two parts : the significant and the exponent. The default form for float constants is double. If you want the resulting data type to be float or long double, you must specify the desired data type (e.g. f and F are used for float and l and L are used for long double).

Examples of Float Constants


Literal 0. .0 2.0 3.1416 -2.0 3.1415926536L Value 0.0 0.0 2.0 3.1416 -2.0 3.1415926536 Type double double double double float long double

Character Constants
Constants are enclosed between two single quotes e.g. A (apostrophes). In addition to character, there can be a backslash (\) known as escape character between the quote marks. It is used when the character you need to represent does not have any graphic associated with it, i.e. it cannot be printed or when it cannot be entered from the keyboard. Most computers use the ASCII alphabet, or ASCII character set.

Symbolic Names for Special Characters


ASCII Character Null character Alert (bell) Backspace Horizontal tab Newline Vertical tab Form feed Carriage return Single quote Backslash Symbolic Name \0 \a \b \t \n \v \f \r \ \\

String Constants
A string constant is a sequence of zero or more characters enclosed in double quotes .

Examples of Strings:
/* A null string */ h Hello World\n HOW ARE YOU GOOD MORNING! Department of Information & Communications Technology

Coding Constants
Three ways to code constants in C :

Literal constants Defined constants Memory constants

Literal Constants
A literal is an unnamed constant used to
specify data. It is by far the most common form of constants. Literal Constant Examples
A 5 a+5 3.1416 Hello /* /* /* /* /* a character literal */ numeric literal 5 */ another numeric literal (5) */ a float literal */ a string literal */

Symbolic Constants

Use the preprocessor command, define to designate. It is prefaced with the pound sign (#), e.g. : #define SALES_TAX_RATE .0826

Usually placed at the beginning of the program. The expression that follows the name replaces the name wherever it is found in the source program. This action is just like the search and replace command found in the text editor. The preprocessor does not evaluate the code in any way, it just blindly makes the substitution.

Memory constants
Memory constants uses a C type qualifier to fix the content of certain memory location. The type qualifier is const e.g. : const float pi = 3.1416; Once assigned, value of the constant can not be changed.

Variables

In C, variables is data name that store the data value. Variables take different value at different times. A variable can be declared and defined at the same time. When we create variables, the declaration gives them a symbolic name and the definition reserves memory for them. Once defined, variables are used to hold the data that are required by the program for its operation.

Variables in Memory

Data in memory is accessed through their symbolic names, or identifiers by the programmer, not by their physical addresses in memory.

Data Types
A type defines a set of values and a set of operations that can be applied on those values. The set of values for type is known as the domain for the type. C contains four standard types : void char (short for character) int (short for integer) float (short for floating point)

Standard Data Types

Standard type cannot be broken down further into sub-types. Rather, they can be used to build more complex data types call Derived Types (e.g. pointers, array, union etc.).

void

The void type has no values and no operations. Both the set of values and the set of operations are empty. A number without a fraction part : integral number. C supports three different sizes of the integer data type : short int int long int

integer

Integer Types (int)

Typical Integer Sizes


Type Sign Byte Number Minimum value Maximum value Size of bits 2 2 4 16 16 32 -32,768 0 -32,768 0 -2,147,483,648 0 32,767 65,535 32,767 65,535 2,147,483,647 4,294,967,295

short int int long

signed unsigned signed unsigned signed unsigned

1. If the integer is signed, then one bit must be used for the sign (0 is plus, 1 is minus). 2. The unsigned integer can therefore store a positive number that is twice as large as the signed integer of the same size.

Floating Point

A floating-point type is a number with a fractional part, e.g. 56.78 The C language supports three different sizes of floating-point data type : float, double, long double. size of (float) <= size of (double) <= size of (long double). Floating-point is always signed.

Typical Floating-Point Sizes


Type float double long double Byte Size 4 8 10 Number of Bits 32 64 80

Floating-Point Types

Type Summary
void character integer float void char unsigned short int; unsigned int; unsigned long int short int; int; long int Float; double; long double

Variables Declaration

A variables type can be any of the data types such as char, int, and float except void. To create a variable, you must specify the type and then its identifier :
Data-type v1,v2.vn; float price; int count; char code;

Examples of variable definition


int a,b.c =10; float a,b,c=5.88; float price, float max_rate; char c,d;

Formatted Input and Output

Format of printf Statement

Format Codes for Input & Output


Code c d f s Meaning character integer float point String Argument Type char int float double String Example %c %d %f %f %s

SIZE
Examples: %hd %ld /* short integer */ /* long integer */

%Lf

/* long double */

Example 1
printf (%d\t%c\t%5.1f\n, 23, Z, 14.2 ); printf (%d\t%c\t%5.1f\n, 107, A, 53.6); printf (%d\t%c\t%5.1f\n, 1754, F, 122.0); printf (%d\t%c\t%5.1f\n, 3, P, 0.1); NOTE : Results : \t tab character 23 Z 14.2 to separate by tab 107 A 53.6 \n newline character 1754 F 122.0 to print on new 3 P 0.1 line

Formatted Input (scanf)


The standard formatted input function in C
is scanf (scan formatted). scanf consists of : a format string that describes the data; an address list that identifies where data are to be placed in memory.
scanf ( format string, address list ); (%c.%d..%f.., &a,.&i,..,&x..)

Format of scanf Statement

Format Codes for Input & Output

Code c d f

Meaning character integer float point

Argument Type char int float double

Example %c %d %f %f

Example 1
214 156 14Z

scanf (%d%d%d%c, &a, &b, &c, &d);

Note that a space between the 14 and the Z would create an error because % does not skip whitespace ! To prevent this program, put a space before the %c code as shown below : scanf (%d%d%d %c, &a, &b, &c, &d);

Example 2
2314 15 2.14 scanf (%d %d %f, &a, &b, &c);

Note the white-space between the field specifi-cation These spaces are not necessary, but it is a good practice to include them

Example 3
14/26 25/66

scanf ( %2d/%2d %2d/%2d, &num1, &den1, &num2, &den2 );


Note the slashes (/) in the format string. They are not part of field specification. They must be entered exactly as shown. Or scanf will stop reading.

Example 4
11-25-56
scanf (%d-%d-%d, &a, &b, &c);

Again, we see some required user input between the month, day, and year.

User Define data types


C supports the future known as typedef. syntax: typedef type identifier; typedef int units; typedef float marks; now u can use like this unit i,j,k; marks a,b,c,;

User Define data types (cont.)


Enumerated data types. syntax: enum identifier{v1,v2vn}; example: enum day {Monday,Tuesday,.Sunday}; enum day {Monday=1,Tuesday,.Sunday};

You might also like