Mr. R.D.SIVAKUMAR, M.Sc.,M.Phil.,M.Tech.,
Assistant Professor of Computer Science &
Assistant Professor and Head, Department of M.Com.(CA),
Ayya Nadar Janaki Ammal College,
Sivakasi – 626 124.
Mobile: 099440-42243
e-mail : sivamsccsit@gmail.com
website: www.rdsivakumar.blogspot.in
Introduction to C Programming
Introduction to C Programming
The C programming language is a popular and widely used programming language for
creating computer programs. Dennis Ritchie at AT & T Bell Laboratories developed the C
language
Basic types of elements: constants, identifiers, operators, punctuation, and keywords
These elements are collectively known as tokens. A token is a source program text that
the compiler does not break down into component elements
The tokens are,
number - identifier (variable)
= - operator
+ - operator
1 - constant
; - punctuation
The above statement has a collection of tokens such as identifier, constant, operator etc.
Constants
A constant is of numeric or non-numeric type. It can be a number, a character or a
character string that can be used as a value in a program.
Numeric data is primarily made up of numbers and can include decimal points. Non-
numeric data may be composed of numbers, letters, blanks and any special characters
supported by the system. In other words, non-numeric data consists of alphanumeric
characters.
A non-numeric data can be called as a literal. Constants are characterized by having a
value and type.
Numeric constants are of three types:
• integer constant
• floating-point constant
• character constant
Integer Constants
An integer constant is a decimal number (base 10) that represents an integral value
(the whole number).
It comprises of the digits 0 to 9. If an integer constant begins with the letters 0x or 0X,
it is a hexadecimal (base 16) constant.
 If it begins with 0 then it is an octal (base 8) constant. Otherwise it is assumed to be
decimal.
23, 36 and 948 are decimal constants
0x1C, 0XAB, and 0x23 are hexadecimal constants
071, 023, and 035 are octal constants
Integer constants are positive unless they are preceded by a minus sign and hence –18
is also a valid integer constant.
 Special characters are not allowed in an integer constant. The constant 2,345 is an
invalid integer constant because it contains the special character
Floating Point Constant
A floating-point constant is a signed real number. It includes integer portion, a
decimal point, fractional portion and an exponent.
 While representing a floating point constant, either the digits before the
decimal point (the integer portion) or the digits after the decimal point (the
fractional portion) can be omitted but not both.
The decimal point can be omitted if an exponent is included. An exponent is
represented in powers of 10 in decimal system.
Example:
58.64 is a valid floating-point (real) constant. It can be represented in
exponent form as follows:
5.864E1 => 5.864 X 101 => 58.64
5864E-2 => 5864 X 10-2 => 58.64
0.5864e2 => 0.5864 X 102 => 58.64
The letter E or e is used to represent the floating-point constant in exponent
form.
Character Constant
A character is a letter, numeral or special symbol, which can be handled by
the computer system.
These available symbols define the system’s character set.
Enclosing a single character from the system’s character set within single
quotation marks forms a character constant.
The characters used in C language are grouped into three classes.
• Alphabetic characters a, b, c, …., z, A, B, C, ….., Z
• Numeric characters 0 through 9
• Special characters + - * / % # = , . ‘ “ ( ) [ ] :
An escape sequence may be used to represent a single quote as a character
constant.
Character combinations consisting of a backslash  followed by a letter are
called escape sequences. ‘’’ is a valid single quote character constant.
Character Constant
Similarly some nonprintable characters are represented using escape sequence
characters.
Examples:
‘a’ Bell (beep)
‘b’ Backspace
‘f’ Form feed
‘r’ Carriage return
‘n’ New line
‘0’ null character
To represent the backslash itself as a character, two backslashes are used (‘’).
String Literal
A string literal or a string constant is a sequence of characters from the system’s
character set, enclosed in double quotes.
By default, the null character ‘0’ is assumed as the last character in a string literal.
To have a double quote itself as a character in the string constant, an escape sequence
‘”’ is used. “hello” is a valid string literal.
The actual number of characters in this string literal is 6 including the null character
at the last.
 The null character is invisible here.
Six bytes are required to store this string in memory.
However, the physical length of this string is 5 characters.
Identifiers
Identifiers are the names that are to be given to the variables, functions, data types
and labels in a program. The name of a variable can consist of alphabets (letters) and
numbers The first character in a variable’s name should be an alphabet, ie., a number is
not allowed as a first character in the variable name.
The valid variable names are:
x
length
x_value
y_value
a123
Keywords (which have special meaning in C) cannot be used as identifiers. The
following variable names are invalid and the reasons are stated.
123 - The first character is a number
1abc - The first character is a number
x value - A blank character is used
x&y - A character other than alphabet and number is used & is not a valid character in
a variable name.
for - It is a keyword
Fundamental Data Types
Data is one of the most commonly used terms in programming. Data can be defined
as the raw information input to the computer. Data is differentiated into various types
in C. There are three numeric data types that comprise the fundamental or primary
data types. The fundamental data types are: int, float and char
The following are the declaration statements of variables of different types:
int x;
float f;
char ch;
We know already that an integer is a whole number, a float is a real number and a
character is represented within single quotes.
Example:
1 is an integer (whole number)
1.0 is a real number (floating point number)
‘1’ is a character constant
“1” is a string literal
The data type associated with a string literal is char * (character pointer) and the
explanation is provided in the Arrays section of this chapter. An integer requires 2
bytes of memory to store its value, a float requires 4 bytes of memory and a character
requires 1 byte of memory.
Derived Types
Long, double, unsigned, arrays and pointers are the derived types from the
fundamental primitive types. A long integer can be declared as follows:
long int i;
or
long i;
To store a long integer value, four bytes of memory are required.
To store the real (float) values more precisely, the type double is used.
It occupies 8 bytes in the memory.
Unsigned numbers are represented as follows:
unsigned int i;
Unsigned int occupies 2 bytes as normal integers but all the bits are used to
store the value. In a regular integer, the most significant bit (the left most bit) is
a sign bit.
Pointer Variables
The variables in C are classified into ordinary variables and pointer variables.
An ordinary variable can take values of its associated type.
Example:
int x;
Here, x is an ordinary variable of type integer and assumes a whole number as its
value.
x = 10;
A pointer variable is declared as follows:
int *y;
The above declaration is a pointer variable declaration. Here, y is a pointer variable
whose type is an integer pointer (int *).
x is an integer, it occupies two bytes in the memory
Pointer Variables
The address of the variable x is 948 as shown above. Every byte is addressable in main
memory. To obtain the address of the variable, we have to use the “address of” operator
(&). So in the statement y = &x; the address of x is stored into the pointer variable y.
Since y is a pointer variable, it can assume only an address of a variable. We can say that
y points to x. We can represent this as follows:
Assume, the value of x is 10. To retrieve the value of x through the pointer variable y (since y
already points to x), we can use the “indirection” operator (*). That is, *y will give the value
contained in the location pointed by y. If the value of x is 10 and the pointer variable y is
pointing to x then the value of the expression *y is 10.
Remember in the above example,
• y represents the address of the variable x (&x)
• *y represents the value of the variable x (x)
Both address of and indirection operators are used as unary operators here
Operators
C has rich set of operators. Operators are what make things happen.
An operator is defined as a symbol that specifies an operation to be performed.
Operators inform the computer what tasks it has to perform as well as the order
in which to perform them. The order in which operations are performed is called
the order of precedence.
 It is also called hierarchy. The direction in which operations are carried out is
called Associativity. There are three types of operators in C.
• Unary operators
• Binary operators
• Ternary operator
Unary Operators
Order of precedence is high for the unary operators and they have only one operand.
The order of evaluation (associativity) is from right to left. Table 4.1 lists the unary
operators and their functions.
The increment or decrement operator is used to increase or to decrease the current
value of a variable by 1. Generally the unary operators appear before the operand. For
example, to find the address of integer variable x, we can use the expression & x. In case
of increment and decrement operators, they may appear before or after the operand
Hence there are two forms:
Postfix increment or decrement
Prefix increment or decrement
The unary operators ++ and — are called prefix increment or decrement operators
when they appear before the operand. The unary operators ++ and — are called postfix
increment or decrement operators when they appear after the operand. Their usages are
explained in the other sections of this chapter.
Binary Operators
Arithmetic Operators
The arithmetic operators like +, -, *, /, and % are binary operators as they have two
operands. Table 4.2 shows the list of arithmetic operators. All the arithmetic operators
observe left to right associativity.
The arithmetic operators can be used with integers and float values. The modulus operator
works with integers only and it gives the remainder value after division. The integer division
truncates the result.
Example:
5 / 2 = 2 (the fractional part is truncated)
5 % 2 = 1 (the remainder after division)
With respect to integers, the division operator returns the quotient of the division and the
modulus operator returns the remainder after division. The division, multiplication and
modulus operators have higher precedence than addition and subtraction operators.
Relational Operators
The relational or Boolean operators are binary operators as they always have
two operands Table shows the list of relational operators.
 All the relational operators observe left to right associativity, that is, they are
evaluated from left to right.
The relational operators are used to compare two values (items) and the
result will be either true or false.
Assignment Operators
The assignment operator (=) assigns the value of the right-hand operand to the
left-hand operand. C has arithmetic-assignment operators too.
They are +=, - =, *=, /= and %=.
Consider the following statement:
i = i + 1;
Here, the old value of the variable i is incremented by 1 and the incremented
value is stored as the new value of i. The above statement can be represented in
the following ways
Using the arithmetic - assignment operator,
i += 1;
In the above statement, the operator += is used to increment the old value of i
by one and the new value is stored in the variable I itself.
Similarly, the statement i *= 2; represents that the old value of i is multiplied by
2 and the new value is stored in i itself. Of all the operators, the assignment
operators have the least precedence
The order of evaluation
We have discussed the precedence of operators in the previous section.
Using the precedence of operators, we will see how to evaluate an expression.
The bracketed expression should be evaluated first.
Consider the following expression
5 * 2 + 8 + (3 – 2) * 5
It will be evaluated as follows:
5 * 2 + 8 + 1 * 5 (bracketed expression is evaluated first)
10 + 8 + 5 (multiplication has higher precedence over addition)
23 (the value of the expression is 23)
Ternary Operator
C has one ternary operator, which is a conditional operator. The symbol
used for this operator is ?: and it has three operands. The
syntax of the conditional operator is as follows:
conditional expression? expression 1 : expression 2;
If the conditional expression is true, expression 1 is evaluated.
If the conditional expression is false, expression 2 is evaluated.
Example :
The following example show the uses of conditional operator:
j = i < 0 ? - i : i;
This example assigns the absolute value of i to j. If i is less than
0, -i is assigned to j. If i is greater than or equal to 0, i is assigned to j.
Punctuation and Keywords
C’s punctuation symbols and their uses are listed as follows:
[ ] - used to represent array index (square brackets)
{ } - used to cover the body of the function (curly braces)
( ) - used to represent a function, to group items and to group expressions (simple
parentheses)
< > - used to enclose a header file in a preprocessor statement (angular brackets)
“ “ - used to represent string literals (double quotes)
‘ ‘ - used to represent a character constant (single quotes)
/* */ - used to represent a comment
; - used as a statement terminator
, - used to separate items
blank and white space – used to improve readability of the program
The following are the list of important keywords. They cannot be used as identifiers
for the variables in a program.
A keyword must be specified precisely as given in the list. For example, auto is a
keyword, whereas, Auto or AUTO are not keywords.
Thank you..!!

Introduction to C Programming - R.D.Sivakumar

  • 1.
    Mr. R.D.SIVAKUMAR, M.Sc.,M.Phil.,M.Tech., AssistantProfessor of Computer Science & Assistant Professor and Head, Department of M.Com.(CA), Ayya Nadar Janaki Ammal College, Sivakasi – 626 124. Mobile: 099440-42243 e-mail : [email protected] website: www.rdsivakumar.blogspot.in Introduction to C Programming
  • 2.
    Introduction to CProgramming The C programming language is a popular and widely used programming language for creating computer programs. Dennis Ritchie at AT & T Bell Laboratories developed the C language Basic types of elements: constants, identifiers, operators, punctuation, and keywords These elements are collectively known as tokens. A token is a source program text that the compiler does not break down into component elements The tokens are, number - identifier (variable) = - operator + - operator 1 - constant ; - punctuation The above statement has a collection of tokens such as identifier, constant, operator etc.
  • 3.
    Constants A constant isof numeric or non-numeric type. It can be a number, a character or a character string that can be used as a value in a program. Numeric data is primarily made up of numbers and can include decimal points. Non- numeric data may be composed of numbers, letters, blanks and any special characters supported by the system. In other words, non-numeric data consists of alphanumeric characters. A non-numeric data can be called as a literal. Constants are characterized by having a value and type. Numeric constants are of three types: • integer constant • floating-point constant • character constant
  • 4.
    Integer Constants An integerconstant is a decimal number (base 10) that represents an integral value (the whole number). It comprises of the digits 0 to 9. If an integer constant begins with the letters 0x or 0X, it is a hexadecimal (base 16) constant.  If it begins with 0 then it is an octal (base 8) constant. Otherwise it is assumed to be decimal. 23, 36 and 948 are decimal constants 0x1C, 0XAB, and 0x23 are hexadecimal constants 071, 023, and 035 are octal constants Integer constants are positive unless they are preceded by a minus sign and hence –18 is also a valid integer constant.  Special characters are not allowed in an integer constant. The constant 2,345 is an invalid integer constant because it contains the special character
  • 5.
    Floating Point Constant Afloating-point constant is a signed real number. It includes integer portion, a decimal point, fractional portion and an exponent.  While representing a floating point constant, either the digits before the decimal point (the integer portion) or the digits after the decimal point (the fractional portion) can be omitted but not both. The decimal point can be omitted if an exponent is included. An exponent is represented in powers of 10 in decimal system. Example: 58.64 is a valid floating-point (real) constant. It can be represented in exponent form as follows: 5.864E1 => 5.864 X 101 => 58.64 5864E-2 => 5864 X 10-2 => 58.64 0.5864e2 => 0.5864 X 102 => 58.64 The letter E or e is used to represent the floating-point constant in exponent form.
  • 6.
    Character Constant A characteris a letter, numeral or special symbol, which can be handled by the computer system. These available symbols define the system’s character set. Enclosing a single character from the system’s character set within single quotation marks forms a character constant. The characters used in C language are grouped into three classes. • Alphabetic characters a, b, c, …., z, A, B, C, ….., Z • Numeric characters 0 through 9 • Special characters + - * / % # = , . ‘ “ ( ) [ ] : An escape sequence may be used to represent a single quote as a character constant. Character combinations consisting of a backslash followed by a letter are called escape sequences. ‘’’ is a valid single quote character constant.
  • 7.
    Character Constant Similarly somenonprintable characters are represented using escape sequence characters. Examples: ‘a’ Bell (beep) ‘b’ Backspace ‘f’ Form feed ‘r’ Carriage return ‘n’ New line ‘0’ null character To represent the backslash itself as a character, two backslashes are used (‘’).
  • 8.
    String Literal A stringliteral or a string constant is a sequence of characters from the system’s character set, enclosed in double quotes. By default, the null character ‘0’ is assumed as the last character in a string literal. To have a double quote itself as a character in the string constant, an escape sequence ‘”’ is used. “hello” is a valid string literal. The actual number of characters in this string literal is 6 including the null character at the last.  The null character is invisible here. Six bytes are required to store this string in memory. However, the physical length of this string is 5 characters.
  • 9.
    Identifiers Identifiers are thenames that are to be given to the variables, functions, data types and labels in a program. The name of a variable can consist of alphabets (letters) and numbers The first character in a variable’s name should be an alphabet, ie., a number is not allowed as a first character in the variable name. The valid variable names are: x length x_value y_value a123 Keywords (which have special meaning in C) cannot be used as identifiers. The following variable names are invalid and the reasons are stated. 123 - The first character is a number 1abc - The first character is a number x value - A blank character is used x&y - A character other than alphabet and number is used & is not a valid character in a variable name. for - It is a keyword
  • 10.
    Fundamental Data Types Datais one of the most commonly used terms in programming. Data can be defined as the raw information input to the computer. Data is differentiated into various types in C. There are three numeric data types that comprise the fundamental or primary data types. The fundamental data types are: int, float and char The following are the declaration statements of variables of different types: int x; float f; char ch; We know already that an integer is a whole number, a float is a real number and a character is represented within single quotes. Example: 1 is an integer (whole number) 1.0 is a real number (floating point number) ‘1’ is a character constant “1” is a string literal The data type associated with a string literal is char * (character pointer) and the explanation is provided in the Arrays section of this chapter. An integer requires 2 bytes of memory to store its value, a float requires 4 bytes of memory and a character requires 1 byte of memory.
  • 11.
    Derived Types Long, double,unsigned, arrays and pointers are the derived types from the fundamental primitive types. A long integer can be declared as follows: long int i; or long i; To store a long integer value, four bytes of memory are required. To store the real (float) values more precisely, the type double is used. It occupies 8 bytes in the memory. Unsigned numbers are represented as follows: unsigned int i; Unsigned int occupies 2 bytes as normal integers but all the bits are used to store the value. In a regular integer, the most significant bit (the left most bit) is a sign bit.
  • 12.
    Pointer Variables The variablesin C are classified into ordinary variables and pointer variables. An ordinary variable can take values of its associated type. Example: int x; Here, x is an ordinary variable of type integer and assumes a whole number as its value. x = 10; A pointer variable is declared as follows: int *y; The above declaration is a pointer variable declaration. Here, y is a pointer variable whose type is an integer pointer (int *). x is an integer, it occupies two bytes in the memory
  • 13.
    Pointer Variables The addressof the variable x is 948 as shown above. Every byte is addressable in main memory. To obtain the address of the variable, we have to use the “address of” operator (&). So in the statement y = &x; the address of x is stored into the pointer variable y. Since y is a pointer variable, it can assume only an address of a variable. We can say that y points to x. We can represent this as follows: Assume, the value of x is 10. To retrieve the value of x through the pointer variable y (since y already points to x), we can use the “indirection” operator (*). That is, *y will give the value contained in the location pointed by y. If the value of x is 10 and the pointer variable y is pointing to x then the value of the expression *y is 10. Remember in the above example, • y represents the address of the variable x (&x) • *y represents the value of the variable x (x) Both address of and indirection operators are used as unary operators here
  • 14.
    Operators C has richset of operators. Operators are what make things happen. An operator is defined as a symbol that specifies an operation to be performed. Operators inform the computer what tasks it has to perform as well as the order in which to perform them. The order in which operations are performed is called the order of precedence.  It is also called hierarchy. The direction in which operations are carried out is called Associativity. There are three types of operators in C. • Unary operators • Binary operators • Ternary operator
  • 15.
    Unary Operators Order ofprecedence is high for the unary operators and they have only one operand. The order of evaluation (associativity) is from right to left. Table 4.1 lists the unary operators and their functions. The increment or decrement operator is used to increase or to decrease the current value of a variable by 1. Generally the unary operators appear before the operand. For example, to find the address of integer variable x, we can use the expression & x. In case of increment and decrement operators, they may appear before or after the operand Hence there are two forms: Postfix increment or decrement Prefix increment or decrement The unary operators ++ and — are called prefix increment or decrement operators when they appear before the operand. The unary operators ++ and — are called postfix increment or decrement operators when they appear after the operand. Their usages are explained in the other sections of this chapter.
  • 16.
    Binary Operators Arithmetic Operators Thearithmetic operators like +, -, *, /, and % are binary operators as they have two operands. Table 4.2 shows the list of arithmetic operators. All the arithmetic operators observe left to right associativity. The arithmetic operators can be used with integers and float values. The modulus operator works with integers only and it gives the remainder value after division. The integer division truncates the result. Example: 5 / 2 = 2 (the fractional part is truncated) 5 % 2 = 1 (the remainder after division) With respect to integers, the division operator returns the quotient of the division and the modulus operator returns the remainder after division. The division, multiplication and modulus operators have higher precedence than addition and subtraction operators.
  • 17.
    Relational Operators The relationalor Boolean operators are binary operators as they always have two operands Table shows the list of relational operators.  All the relational operators observe left to right associativity, that is, they are evaluated from left to right. The relational operators are used to compare two values (items) and the result will be either true or false.
  • 18.
    Assignment Operators The assignmentoperator (=) assigns the value of the right-hand operand to the left-hand operand. C has arithmetic-assignment operators too. They are +=, - =, *=, /= and %=. Consider the following statement: i = i + 1; Here, the old value of the variable i is incremented by 1 and the incremented value is stored as the new value of i. The above statement can be represented in the following ways Using the arithmetic - assignment operator, i += 1; In the above statement, the operator += is used to increment the old value of i by one and the new value is stored in the variable I itself. Similarly, the statement i *= 2; represents that the old value of i is multiplied by 2 and the new value is stored in i itself. Of all the operators, the assignment operators have the least precedence
  • 19.
    The order ofevaluation We have discussed the precedence of operators in the previous section. Using the precedence of operators, we will see how to evaluate an expression. The bracketed expression should be evaluated first. Consider the following expression 5 * 2 + 8 + (3 – 2) * 5 It will be evaluated as follows: 5 * 2 + 8 + 1 * 5 (bracketed expression is evaluated first) 10 + 8 + 5 (multiplication has higher precedence over addition) 23 (the value of the expression is 23)
  • 20.
    Ternary Operator C hasone ternary operator, which is a conditional operator. The symbol used for this operator is ?: and it has three operands. The syntax of the conditional operator is as follows: conditional expression? expression 1 : expression 2; If the conditional expression is true, expression 1 is evaluated. If the conditional expression is false, expression 2 is evaluated. Example : The following example show the uses of conditional operator: j = i < 0 ? - i : i; This example assigns the absolute value of i to j. If i is less than 0, -i is assigned to j. If i is greater than or equal to 0, i is assigned to j.
  • 21.
    Punctuation and Keywords C’spunctuation symbols and their uses are listed as follows: [ ] - used to represent array index (square brackets) { } - used to cover the body of the function (curly braces) ( ) - used to represent a function, to group items and to group expressions (simple parentheses) < > - used to enclose a header file in a preprocessor statement (angular brackets) “ “ - used to represent string literals (double quotes) ‘ ‘ - used to represent a character constant (single quotes) /* */ - used to represent a comment ; - used as a statement terminator , - used to separate items blank and white space – used to improve readability of the program The following are the list of important keywords. They cannot be used as identifiers for the variables in a program. A keyword must be specified precisely as given in the list. For example, auto is a keyword, whereas, Auto or AUTO are not keywords.
  • 22.