C – Constants and Variables       www.eshikshak.co.in
C - Constants                Constants    Primary                           Secondary   Constants                         ...
Rules for Constructing Integer Constants   An integer constant must have at least one digit.   It must not have a decima...
Rules for Constructing Real Constants  A real constant must have at least one digit.  It must have a decimal point.  It...
Rules for Constructing Character Constants • A characterspecial symbolsingle alphabet, asingle digit   or a single        ...
Variable Variables in C have the same meaning as variables in  algebra. That is, they represent some unknown, or  variabl...
Variable A variable is a used to store values. It has memory  location and can store single value at a time. A variable ...
Naming Variables• Variables in C may be given representations  containing multiple characters. But there are  rules for th...
Naming Conventions C programmers generally agree on the  following conventions for naming variables. o   Begin variable n...
Rules for Defining Variables•   A variable must begin with a character or an underscore without spaces.    The underscore ...
C Data Types                             C Data TypesDerived Data Type         Basic Data Type              User Defined D...
C Data TypesData Type       Size(bytes)                  Range            Format Stringchar                1              ...
C Data types • Integer Data Type       – int, short and long       – All C compilers offers different integer data types.S...
C Data Types Difference between signed and unsigned integersSigned Integer                         Unsigned IntegerOccupie...
C Data TypesSigned Character                          Unsigned CharacterOccupies 1 bytes in memory                Occupies...
C Data TypeFloating                                Double FloatingOccupies 4 bytes in memory              Occupies 8 bytes...
Initialization Variables•  Variable declared can be assigned operator ῾=᾽. The declaration and   initialization can also b...
Dynamic Initialization•   The initialization of variable at run time is called dynamic initialization.    Dynamic refers t...
Example :Type Modifiers                   void main()                                 {                                   ...
• Type Conversion• Wrapping Around• Constant and Volatile  Variables             www.eshikshak.co.in
Upcoming SlideShare
Loading in …5
×

Mesics lecture 3 c – constants and variables

976
-1

Published on

C constants and Variables

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
976
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
61
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mesics lecture 3 c – constants and variables

  1. 1. C – Constants and Variables www.eshikshak.co.in
  2. 2. C - Constants Constants Primary Secondary Constants ConstantsInteger Constants Array Real Constants Pointer Character Structure Constants Union Enum. etc www.eshikshak.co.in
  3. 3. Rules for Constructing Integer Constants  An integer constant must have at least one digit.  It must not have a decimal point.  1, 2, -3, 99, 1000 are valid integer constant  1.3, 2.00, 0.01, -99, 100.9 are invalid integer constant  It can be either positive or negative.  If no sign precedes an integer constant it is assumed to be positive.  No commas or blanks are allowed within an integer constant.  The allowable range for integer constants is -32768 to 32767.  Ex.: 426 +782 -8000 -7605 www.eshikshak.co.in
  4. 4. Rules for Constructing Real Constants  A real constant must have at least one digit.  It must have a decimal point.  It could be either positive or negative.  Default sign is positive.  No commas or blanks are allowed within a real constant. • Ex.: +325.34 426.0 -32.76 -48.5792
  5. 5. Rules for Constructing Character Constants • A characterspecial symbolsingle alphabet, asingle digit or a single constant is a enclosed within single inverted commas. • Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not. • The maximum length of a character constant can be 1 character. Ex.: A I 5 =
  6. 6. Variable Variables in C have the same meaning as variables in algebra. That is, they represent some unknown, or variable, value. •x=a+b • z + 2 = 3(y - 5) Remember that variables in algebra are represented by a single alphabetic character www.eshikshak.co.in
  7. 7. Variable A variable is a used to store values. It has memory location and can store single value at a time. A variable is a data name used for storing a data value. Its value may be changed during the program execution. The variable value keeps on changing during the execution of the program. www.eshikshak.co.in
  8. 8. Naming Variables• Variables in C may be given representations containing multiple characters. But there are rules for these representations.• Variable names (identifiers) in C o May only consist of letters, digits, and underscores o May be as long as you like, but only the first 31 characters are significant o May not begin with a digit o May not be a C reserved word (keyword)
  9. 9. Naming Conventions C programmers generally agree on the following conventions for naming variables. o Begin variable names with lowercase letters o Use meaningful identifiers o Separate “words” within identifiers with underscores or mixed upper and lower case. o Examples: surfaceArea surface_Area surface_area o Be consistent!
  10. 10. Rules for Defining Variables• A variable must begin with a character or an underscore without spaces. The underscore is treated as one type of characters. – It is advised that the variable names should not start with underscore because library routines mostly use such variable names.• The length of the variable varies from compiler to compiler. Generally, most of the compilers support eight characters excluding extension. – ANSI standard recognizes the maximum length of a variable up to 31 characters.• The variable should not be a C keyword.• The variable names may be a combination of uppercase and lowercase characters.• The variable name should not start with a digit.• Blanks and commas are not permitted within a variable name. www.eshikshak.co.in
  11. 11. C Data Types C Data TypesDerived Data Type Basic Data Type User Defined Data Type  Pointers  Pointers  Functions  Functions  Arrays  Arrays Integer Floating Point void  char  float  int  double www.eshikshak.co.in
  12. 12. C Data TypesData Type Size(bytes) Range Format Stringchar 1 -128 to 127 %cunsigned char 1 0 to 255 %cshort or int 2 -32768 to 32767 %i or %dunsigned int 2 0 to 65535 %ulong 4 -2147483648 to 2147483647 %ldunsigned long 4 0 to 4294967295 %lufloat 4 3.4 e-38 to 3.4 e+38 %f or %gdouble 8 1.7 e-308 to 1.7 e+308 %lflong double 10 3.4 e-4932 to 1.1 e + 4932 %lfenum 2* -32768 to 32767 %d www.eshikshak.co.in
  13. 13. C Data types • Integer Data Type – int, short and long – All C compilers offers different integer data types.Short Integer Long IntgerOccupies 2 bytes in memory Occupies 4 bytes in memoryRange : -32768 to 32767 Range : -2147483648 to 2147483647Program runs faster Program runs slowerFormat Specifier : %d or %i Format Specifier : %ldExample : Example :int a = 2; long b = 123456short int b = 2; long int c=1234567Note : when variable is declared without short or long keyword, the default is short-signed int. www.eshikshak.co.in
  14. 14. C Data Types Difference between signed and unsigned integersSigned Integer Unsigned IntegerOccupies 2 bytes in memory Occupies 4 bytes in memoryRange : -32768 to 32767 Range : 0 to 65535Format Specifier : %d or %i Format Specifier : %uBy default signed int is short signed int By default unsigned int is short unsigned intThere are also long signed integers having There are also long unsigned int withrange from -2147483648 to 2147483647 range 0 to 4294967295Example : Example:int a=2; unsigned long b=567898;long int b=2; unsigned short int c=223; When a variable is declared as unsigned the negative range of the data type is transferred to positive, i.e. doubles the largest size of possible value. This is due to delcaring unsigned int, the 16th bit is free and not used to store the sign of the www.eshikshak.co.in number
  15. 15. C Data TypesSigned Character Unsigned CharacterOccupies 1 bytes in memory Occupies 1 bytes in memoryRange : -128 to 127 Range : 0 to 255Format Specifier : %c Format Specifier : %cWhen printed using %d format specifier When printed using %d format specifier,prints ASCII character pirnts ASCII characterchar ch=‘b’ unsigned char = ‘b’; www.eshikshak.co.in
  16. 16. C Data TypeFloating Double FloatingOccupies 4 bytes in memory Occupies 8 bytes in memoryRange : 3.4e-38 to +3.4e+38 Range : 1.7 e-308 to +1.73+308;Format String : %f Format String : %lfExample : Example :float a; double y; There also exist long double having ranged 3.4 e-4932 to 1.1e + 4932 and occupies 10 bytes in memory Example : long double k; www.eshikshak.co.in
  17. 17. Initialization Variables• Variable declared can be assigned operator ῾=᾽. The declaration and initialization can also be done in the same line.Syntax : variable_name = constant; or data_type variable_name = constant;Example : x = 5; where is an integer variable.Example : int y = 4;Example : int x,y,z; www.eshikshak.co.in
  18. 18. Dynamic Initialization• The initialization of variable at run time is called dynamic initialization. Dynamic refers to the process during execution.• In C initialization can be done at any place in the program, however the declaration should be done at the declaration part only.• Example : void main() { int r=2; float area=3.14*r*r; clrscr(); printf(“Area = %g”, area); } OUTPUT: Area=12.56; www.eshikshak.co.in
  19. 19. Example :Type Modifiers void main() { short t = 1;• The keywords signed, long k = 54111; unsigned u = 10; unsigned, short and signed j = -10; long are type modifiers. clrscr();• A type modifier printf(“n t=%d”,t) changes the meaning of printf(“n k=%ld”, k); basic data type and printf(“n u=%u, u); printf(“n j=%d”, j); produces a new type. }• Each of these type OUTPUT modifiers is applicable t = 1; k = 54111; to the basic data type u = 10; int. j = -10 www.eshikshak.co.in
  20. 20. • Type Conversion• Wrapping Around• Constant and Volatile Variables www.eshikshak.co.in
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×