KEMBAR78
C Programming Lecture 3 - Elements of C.pptx
Programming in C
Core Course
Course Instructor
Dr. Murali Malijeddi
Professor
Dept. of ECE, CUTM AP
Elements of C
Elements of C
•Every language has some basic elements and grammatical
rules.
•Before understanding programming, it is must to know the
basic elements of C language.
Elements of C
•These basic elements are:
• Character set
• Variables
• Datatypes
• Constants
• Keywords (reserved words)
• variable declaration
• Expressions
• Statements, etc.
•All of these are used to construct a C program.
C Character Set
•The Characters that are used in C programs are given below:
• Alphabets
• Digits
• Special characters
Alphabets and Digits
A, B, C .......... Z
a, b, c .......... z
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Characters
Execution Characters/Escape Sequences
•Characters are printed on the screen through the keyboard
• But some characters cannot be printed like other normal characters
• such as, newline, tab, backspace
•C supports the combination of backslash () and some
characters from the C character set to print these characters.
• These character combinations are known as ”escape sequences”
Execution Characters/Escape Sequences
• They are represented by two characters.
• The first character is "" and second character is from the C character set.
Trigraph Characters
•There is a possibility that the keyboard doesn't print some
characters.
•C supports the facility of "trigraph sequence" to print these
characters.
•These trigraph sequences have three characters.
•First two are '??' and third character is any character from C
character set.
Trigraph Characters
Delimiters
•Delimiters are used for syntactic meaning in C.
Reserved Words / Keywords
•There are certain words that are reserved for doing specific
tasks.
•These words are known as keywords
•They have standard, predefined meaning in C.
•They are always written in lowercase.
•There are only 32 keywords available in C
Reserved Words / Keywords
Identifiers
•All the words that we'll use in our C programs will be either
• keywords or
• Identifiers
•Keywords are predefined and can't be changed by the user
•While identifiers are user defined words
•They are used to give names to entities like:
•variables, arrays, functions, structures, etc
Rules for Naming Identifiers
•The name should consist of
• only alphabets (both upper and lower case)
• digits and underscore sign( _ ).
•First character should be an alphabet or underscore.
•The name should not be a keyword.
Rules for Naming Identifiers
• Since C is case sensitive, the uppercase and lowercase letters are
considered different.
• For example: code, Code and CODE are three different identifiers.
• An identifier name may be arbitrarily long.
• Some implementations of C recognize only the first eight characters
• Although most implementations recognize 31 characters.
• ANSI standard compilers recognIzes 31 characters.
Rules for Naming Identifiers
•The identifiers are generally given meaningful names.
•Some examples of valid identifier names:
Value a Gross_pay
sum data MARKS
Username Roll_No
Rules for Naming Identifiers
•Some examples of invalid identifier names are:
Data Types
•C supports different types of data.
•Storage representation of these data types is different in
memory.
•There are four fundamental datatypes in C
• char
• int
• float
• double
Data Types
•'char' is used to store any single character
•'int' is used to store integer value
•'float‘ is used for storing single precision floating point
number
•'double' is used for storing double precision floating point
number
Data Types
Data Types
•We can use type qualifiers with these basic types to get
some more types.
•There are two types of type qualifiers-
1. Size qualifiers - short, long
2. Sign qualifiers - signed, unsigned
Data Types
• When the qualifier unsigned is used the number is always positive
• When signed is used number may be positive or negative.
• Note: If the sign qualifier is not mentioned, then by default signed
qualifier is assumed.
• The range of values for signed data types is less than that of unsigned
type.
• In signed type, the leftmost bit is used to represent the sign
• while in unsigned type this bit is also used to represent the value.
Data Types
The size and range of different data types on a 16-bit machine is given
in the following table.
* The size and range may vary on machines with different word sizes.
Constants
•Constant is a value that cannot be changed during execution
of the program.
•There are three types of constants:
Numeric Constants
•Numeric constants consist of numeric digits, they may or
may not have decimal points (.)
•These are the rules for defining numeric constants:
1. Numeric constant should have at least one digit.
2. No comma or space is allowed within the numeric constant.
3. Numeric constants can either be positive or negative but default
sign is always positive.
Numeric Constants
•There are two types of numeric constants:
• Integer constant
• Real constant
Integer Constant
•Integer constants are whole numbers which have no decimal
point (.)
•There are three types of integer constants based on different
number systems.
• The permissible characters that can be used in these constants are:
Decimal constants
•Some valid decimal integer constants are
0 123 3705 23759
•Some invalid decimal integer constants are:
Octal constants
•In octal integer constants; first digit must be 0.
•For example:
0
05
077
0324
Hexdecimal constants
•In hexadecimal integer constants,
•first two characters should be 0x or 0X.
•Some examples are as:
0x
0X23
0x515
0XA15B
0xFFF
0xac
Integer Constant
• By default the type of an integer constant is int.
• But if the value of integer constant exceeds the range of values
represented by ‘int’ type
•Then the type is taken to be unsigned int or long int.
• We can also explicitly mention the type of the constant by suffixing it
with
l or L (for long)
u or U (for unsigned)
ul or UL (for unsigned long)
Integer Constant
For example:
Real (floating point) Constants
•Floating point constants are numeric constants that contain
decimal point
•Some valid floating point constants are:
0.5
5.3
4000.0
0.0073
5597.0
39.0807
Real (floating point) Constants
•The number is written in the mantissa and exponent form
Real (floating point) Constants
•By default the type of a floating point constant is double.
•We can explicitly mention the type of constant by suffixing it
with:
f or F (for float type)
l or L ( for long double)
Character Constants
•A character constant is a single character that is enclosed
within single quotes ‘ ’.
•Some valid character constants are:
• '9' 'D‘ '$' ‘ ' '#'
•Some invalid character constants are:
Character Constants
• Every character constant has a unique integer value associated with it.
• This integer is the numeric value the character in the machine's
character code.
• If the machine is using ASCII (American Standard Code for Information
Interchange), Some ASCII values are:
String Constants
• A String constant has zero, one or more than one character.
• A string constant is enclosed within Double Quotes (" ")
• At the end of string, 0 is automatically placed by the compiler.
• Some examples of string constants are:
"Kumar" "593" "8" "A"
• Note that "A" and 'A' are different
• First one is a string constant which consists of character A and 0.
• Second one is a character constant which represents integer value 65
Symbolic Constants
• If we want to use a constant several times then we can provide it a
name.
For example:
• if we have to use the constant·3.14159265 at many places in our
program
• then we can give it a name PI and use this name instead of writing
the constant value everywhere.
• These types of constants are called symbolic constants or named
constants
Symbolic Constants
•These constants are generally defined at the beginning of the
program as:
#define name value
•'name' is the symbolic name for the constant,
•'name' is generally written in uppercase letters
•'value' can be numeric, character or string constant
•These symbolic constants improve the readability and
modifiability of the program.
Symbolic Constants
Variables
•Variable is a name that can be used to store values.
•Variables can take different values but one at a time.
•These values can be changed during program execution.
•A data type is associated with each variable.
Variables
•The data type of the variable decides what values it can take.
•The rules for naming variables are same as that for naming
identifiers.
Declaration of Variables
•We must to declare a variable before it is used in the
program.
•The syntax of declaration of a variable is:
datatype variablename;
Initialization of Variables
• When a variable is declared it contains undefined value commonly
known as garbage value.
• We can assign some initial value to the variable during the declaration
itself.
• This is called initialization of the variable.
Expressions
•An expression is a combination of:
• Operators
• Constants
• Variables
• Function calls
•The expression be:
• arithmetic
• logical
• relational
Expressions
• More on this in future classes 
Statements
•A statement is an executable part of a program
•This causes the computer to carry out some action
Expression Statement
•Expression statement consists of an expression followed by a
semicolon.
; /* null statement */
Compound Statement
•It consists of several statements enclosed within a pair of
curly braces { }.
• Compound statement is also known as block of statements.
{
int 1=4, b=2, h=3;
int area, volume;
area=2*(1*b+b*h+h*1);
volume=l*b*h;
}
Note:
• If any variable is to be declared inside the block then it can be declared only at the
beginning.
Comments
•Comments are used for increasing readability of the
program.
•Comments are written inside /* and */.
C Programming Lecture 3 - Elements of C.pptx

C Programming Lecture 3 - Elements of C.pptx

  • 1.
    Programming in C CoreCourse Course Instructor Dr. Murali Malijeddi Professor Dept. of ECE, CUTM AP
  • 2.
  • 3.
    Elements of C •Everylanguage has some basic elements and grammatical rules. •Before understanding programming, it is must to know the basic elements of C language.
  • 4.
    Elements of C •Thesebasic elements are: • Character set • Variables • Datatypes • Constants • Keywords (reserved words) • variable declaration • Expressions • Statements, etc. •All of these are used to construct a C program.
  • 5.
    C Character Set •TheCharacters that are used in C programs are given below: • Alphabets • Digits • Special characters
  • 6.
    Alphabets and Digits A,B, C .......... Z a, b, c .......... z 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • 7.
  • 8.
    Execution Characters/Escape Sequences •Charactersare printed on the screen through the keyboard • But some characters cannot be printed like other normal characters • such as, newline, tab, backspace •C supports the combination of backslash () and some characters from the C character set to print these characters. • These character combinations are known as ”escape sequences”
  • 9.
    Execution Characters/Escape Sequences •They are represented by two characters. • The first character is "" and second character is from the C character set.
  • 10.
    Trigraph Characters •There isa possibility that the keyboard doesn't print some characters. •C supports the facility of "trigraph sequence" to print these characters. •These trigraph sequences have three characters. •First two are '??' and third character is any character from C character set.
  • 11.
  • 12.
    Delimiters •Delimiters are usedfor syntactic meaning in C.
  • 13.
    Reserved Words /Keywords •There are certain words that are reserved for doing specific tasks. •These words are known as keywords •They have standard, predefined meaning in C. •They are always written in lowercase. •There are only 32 keywords available in C
  • 14.
  • 15.
    Identifiers •All the wordsthat we'll use in our C programs will be either • keywords or • Identifiers •Keywords are predefined and can't be changed by the user •While identifiers are user defined words •They are used to give names to entities like: •variables, arrays, functions, structures, etc
  • 16.
    Rules for NamingIdentifiers •The name should consist of • only alphabets (both upper and lower case) • digits and underscore sign( _ ). •First character should be an alphabet or underscore. •The name should not be a keyword.
  • 17.
    Rules for NamingIdentifiers • Since C is case sensitive, the uppercase and lowercase letters are considered different. • For example: code, Code and CODE are three different identifiers. • An identifier name may be arbitrarily long. • Some implementations of C recognize only the first eight characters • Although most implementations recognize 31 characters. • ANSI standard compilers recognIzes 31 characters.
  • 18.
    Rules for NamingIdentifiers •The identifiers are generally given meaningful names. •Some examples of valid identifier names: Value a Gross_pay sum data MARKS Username Roll_No
  • 19.
    Rules for NamingIdentifiers •Some examples of invalid identifier names are:
  • 20.
    Data Types •C supportsdifferent types of data. •Storage representation of these data types is different in memory. •There are four fundamental datatypes in C • char • int • float • double
  • 21.
    Data Types •'char' isused to store any single character •'int' is used to store integer value •'float‘ is used for storing single precision floating point number •'double' is used for storing double precision floating point number
  • 22.
  • 23.
    Data Types •We canuse type qualifiers with these basic types to get some more types. •There are two types of type qualifiers- 1. Size qualifiers - short, long 2. Sign qualifiers - signed, unsigned
  • 24.
    Data Types • Whenthe qualifier unsigned is used the number is always positive • When signed is used number may be positive or negative. • Note: If the sign qualifier is not mentioned, then by default signed qualifier is assumed. • The range of values for signed data types is less than that of unsigned type. • In signed type, the leftmost bit is used to represent the sign • while in unsigned type this bit is also used to represent the value.
  • 25.
    Data Types The sizeand range of different data types on a 16-bit machine is given in the following table. * The size and range may vary on machines with different word sizes.
  • 26.
    Constants •Constant is avalue that cannot be changed during execution of the program. •There are three types of constants:
  • 27.
    Numeric Constants •Numeric constantsconsist of numeric digits, they may or may not have decimal points (.) •These are the rules for defining numeric constants: 1. Numeric constant should have at least one digit. 2. No comma or space is allowed within the numeric constant. 3. Numeric constants can either be positive or negative but default sign is always positive.
  • 28.
    Numeric Constants •There aretwo types of numeric constants: • Integer constant • Real constant
  • 29.
    Integer Constant •Integer constantsare whole numbers which have no decimal point (.) •There are three types of integer constants based on different number systems. • The permissible characters that can be used in these constants are:
  • 30.
    Decimal constants •Some validdecimal integer constants are 0 123 3705 23759 •Some invalid decimal integer constants are:
  • 31.
    Octal constants •In octalinteger constants; first digit must be 0. •For example: 0 05 077 0324
  • 32.
    Hexdecimal constants •In hexadecimalinteger constants, •first two characters should be 0x or 0X. •Some examples are as: 0x 0X23 0x515 0XA15B 0xFFF 0xac
  • 33.
    Integer Constant • Bydefault the type of an integer constant is int. • But if the value of integer constant exceeds the range of values represented by ‘int’ type •Then the type is taken to be unsigned int or long int. • We can also explicitly mention the type of the constant by suffixing it with l or L (for long) u or U (for unsigned) ul or UL (for unsigned long)
  • 34.
  • 35.
    Real (floating point)Constants •Floating point constants are numeric constants that contain decimal point •Some valid floating point constants are: 0.5 5.3 4000.0 0.0073 5597.0 39.0807
  • 36.
    Real (floating point)Constants •The number is written in the mantissa and exponent form
  • 37.
    Real (floating point)Constants •By default the type of a floating point constant is double. •We can explicitly mention the type of constant by suffixing it with: f or F (for float type) l or L ( for long double)
  • 39.
    Character Constants •A characterconstant is a single character that is enclosed within single quotes ‘ ’. •Some valid character constants are: • '9' 'D‘ '$' ‘ ' '#' •Some invalid character constants are:
  • 40.
    Character Constants • Everycharacter constant has a unique integer value associated with it. • This integer is the numeric value the character in the machine's character code. • If the machine is using ASCII (American Standard Code for Information Interchange), Some ASCII values are:
  • 41.
    String Constants • AString constant has zero, one or more than one character. • A string constant is enclosed within Double Quotes (" ") • At the end of string, 0 is automatically placed by the compiler. • Some examples of string constants are: "Kumar" "593" "8" "A" • Note that "A" and 'A' are different • First one is a string constant which consists of character A and 0. • Second one is a character constant which represents integer value 65
  • 42.
    Symbolic Constants • Ifwe want to use a constant several times then we can provide it a name. For example: • if we have to use the constant·3.14159265 at many places in our program • then we can give it a name PI and use this name instead of writing the constant value everywhere. • These types of constants are called symbolic constants or named constants
  • 43.
    Symbolic Constants •These constantsare generally defined at the beginning of the program as: #define name value •'name' is the symbolic name for the constant, •'name' is generally written in uppercase letters •'value' can be numeric, character or string constant •These symbolic constants improve the readability and modifiability of the program.
  • 44.
  • 45.
    Variables •Variable is aname that can be used to store values. •Variables can take different values but one at a time. •These values can be changed during program execution. •A data type is associated with each variable.
  • 46.
    Variables •The data typeof the variable decides what values it can take. •The rules for naming variables are same as that for naming identifiers.
  • 47.
    Declaration of Variables •Wemust to declare a variable before it is used in the program. •The syntax of declaration of a variable is: datatype variablename;
  • 48.
    Initialization of Variables •When a variable is declared it contains undefined value commonly known as garbage value. • We can assign some initial value to the variable during the declaration itself. • This is called initialization of the variable.
  • 49.
    Expressions •An expression isa combination of: • Operators • Constants • Variables • Function calls •The expression be: • arithmetic • logical • relational
  • 50.
    Expressions • More onthis in future classes 
  • 51.
    Statements •A statement isan executable part of a program •This causes the computer to carry out some action
  • 52.
    Expression Statement •Expression statementconsists of an expression followed by a semicolon. ; /* null statement */
  • 53.
    Compound Statement •It consistsof several statements enclosed within a pair of curly braces { }. • Compound statement is also known as block of statements. { int 1=4, b=2, h=3; int area, volume; area=2*(1*b+b*h+h*1); volume=l*b*h; } Note: • If any variable is to be declared inside the block then it can be declared only at the beginning.
  • 54.
    Comments •Comments are usedfor increasing readability of the program. •Comments are written inside /* and */.