C PROGRAMMING Lesson 3 and 4
C PROGRAMMING Lesson 3 and 4
KEYWORDS,
IDENTIFIER, VARIABLES AND
CONSTANTS
C programming language provides many built-in functions to read any given input and to
display data on screen when there is a need to output the result.
Format specifier: Format specifiers defines the type of data to be printed on standard output.
Whether to print formatted output or to take formatted input we need format specifiers.
Format specifiers are also called as format string. Each format specifier begins with the
percentage character (%) followed by a conversion character which indicates the type of the
corresponding data item.
Here is a list of all format specifiers used in C programming language:
Format specifiers Uses Example
printf() function can be used in two ways. First one is, to print a text that is not a value of a
variable. Second one is, to print the value of variables.
Format of printf() function to print a text: The text should be in double quotation of the
function.
printf(” Output text should be here “);
Format of printf() function to print the value of a single variable:
printf(“format_specifier”, variable_name);
Example:
printf() function for printing ‘char’ type data of variable a: printf(“%c”, a);
printf() function for printing ‘int’ type data of variable a: printf(“%d”, a);
printf() function for printing ‘float’ type data of variable a: printf(“%f”, a);
printf() function for printing ‘double’ type data of variable a: printf(“%lf”, a);
Format of printf() function to print the value of multiple variables:
printf(“format_specifier1, format_specifier2….”, variable_name1, variable_name2…);
Format of printf() function for printing same type of multiple variables:
If variable a,b and c are int type then the format of printf():
printf(“%d %d %d”, a, b, c);
Format of printf() function for printing different type of multiple variables:
If variable a,b and c are int, float and double type respectively then the format of printf():
printf(“%d %f %lf”, a, b, c);
Backslash
Characters Uses Examples Output
Learning
Printing Horizontal
\t tab printf(“Learning\t ICT”); Learning ICT
Learning
Printing Single
\’ quotation mark printf(“Learning \’ICT\’ “); Learning ‘ICT’
'C' provides various data types to make it easy for a programmer to select a suitable data type
as per the requirements of an application. Following are the three data types:
Array, functions, pointers, structures are derived data types. 'C' language provides more
extended versions of the above mentioned primary data types. Each data type differs from one
another in size and range. Following table displays the size and range of each data type.
Integer is nothing but a whole number. The range for an integer data type varies from machine
to machine. The standard range for an integer data type is -32768 to 32767.
Each data type differs in range even though it belongs to the integer data type family. The size
may not change for each data type of integer family.
The short int is mostly used for storing small numbers, int is used for storing averagely sized
integer values, and long int is used for storing large integer values.
Whenever we want to use an integer data type, we have place int before the identifier such as,
int age;
Here, age is a variable of an integer data type which can be used to store integer values.
Like integers, in 'C' program we can also make use of floating point data types. The 'float'
keyword is used to represent the floating point data type. It can hold a floating point value
which means a number is having a fraction and a decimal part. A floating point value is a real
number that contains a decimal point. Integer data type doesn't store the decimal part hence
we can use floats to store decimal part of a value.
Generally, a float can hold up to 6 precision values. If the float is not sufficient, then we can
make use of other data types that can hold large floating point values. The data type double
and long double are used to store real numbers with precision up to 14 and 80 bits
respectively.
While using a floating point number a keyword float/double/long double must be placed
before an identifier. The valid examples are,
float division;
double BankBalance;
Character data types are used to store a single character value enclosed in single quotes.
Example,
Char letter;
A void data type doesn't contain or return any value. It is mostly used for defining functions in
'C'.
Example,
void displayData()
int main() {
int x, y;
float salary = 13.48;
char letter = 'K';
x = 25;
y = 34;
int z = x+y;
printf("%d \n", z);
printf("%f \n", salary);
printf("%c \n", letter);
return 0;}
Output:
59
13.480000
K
We can declare multiple variables with the same data type on a single line by separating them
with a comma. Also, notice the use of format specifiers in printf output function float (%f)
and char (%c) and int (%d).
3.4- Tokens
A token is the smallest unit in a 'C' program. A token is divided into six different types as
follows,
Tokens in C
Do If Static While
3.6- Identifier
3.7- Variable
A variable is an identifier which is used to store some value. Constants can never change at
the time of execution. Variables can change during the execution of a program and update the
value stored inside it.
A single variable can be used at multiple locations in a program. A variable name must be
meaningful. It should represent the purpose of the variable.
Example: Height, age, are the meaningful variables that represent the purpose it is being used
for. Height variable can be used to store a height value. Age variable can be used to store the a
ge of a person
A variable must be declared first before it is used somewhere inside the program. A variable
name is formed using characters, digits and an underscore.
Following are the rules that must be followed while creating a variable:
height or HEIGHT
_height
_height1
My_name
For example, we declare an integer variable my_variable and assign it the value 48:
int my_variable;
my_variable = 48;
By the way, we can both declare and initialize (assign an initial value) a variable in a single
statement:
Data_type variable_name;
For example: int number;
The Programming language C has two main variable types:
1. Local Variables
2. Global Variables
Local Variables: Local variables scope is confined within the block or function where it is
defined. Local variables must always be defined at the top of a block. When a local variable is
defined – it is not initialized by the system, you must initialize it yourself. When execution of
the block starts the variable is available, and when the block ends the variable ‘dies’.
Global Variables: Global variable is defined at the top of the program file and it can be
visible and modified by any function that may reference it. Global variables are initialized
automatically by the system when you define them!. If same variable name is being used for
global and local variable then local variable takes preference in its scope. But it is not a good
practice to use global variables and local variables with the same name.
3.8- Constants
Constants are the fixed values that never change during the execution of a program. Following
are the various types of constants:
Integer constants
An integer constant is nothing but a value consisting of digits or numbers. These values never
change during the execution of a program. Integer constants can be octal, decimal and
hexadecimal.
2. Octal constant contains digits from 0-7, and these types of constants are always
preceded by 0.
3. Hexadecimal constant contains a digit from 0-9 as well as characters from A-F.
Hexadecimal constants are always preceded by 0X.
The octal and hexadecimal integer constants are very rarely used in programming with 'C'.
Character constants
A character constant contains only a single character enclosed within a single quote (''). We
can also represent character constant by providing ASCII value of it.
String constants
A string constant contains a sequence of characters enclosed within double quotes ("").
Example, "Hello", "Programming"
Real Constants
Like integer constants that always contains an integer value. 'C' also provides real constants
that contain a decimal point or a fraction value. The real constants are also called as floating
point constants. The real constant contains a decimal point and a fractional value.
Mantissa e Exponent
For example, to declare a value that does not change like the classic circle constant PI, there
are two ways to declare this constant
1. By using the const keyword in a variable declaration which will reserve a storage
memory
#include <stdio.h>
int main() {
const double PI = 3.14;
printf("%f", PI);
//PI++; // This will generate an error as constants cannot be changed
return 0;}
2. By using the #define pre-processor directive which doesn't use memory for storage
and without putting a semicolon character at the end of that statement
#include <stdio.h>
#define PI 3.14
int main() {
printf("%f", PI);
return 0;}
3.9. Modifiers in C
The amount of memory space to be allocated for a variable is derived by modifiers. Modifiers
are prefixed with basic data types to modify (either increase or decrease) the amount of storage
space allocated to a variable.
Summary
EXERCISES:
13. What do you understand by float type? Write down with example.
Depending on the number of operands that an operator can act upon, operators can be
classified as follows:
1. Unary Operators
2. Binary Operators
3. Ternary Operators
1. Unary Operators:Those operators that require only single operand to act upon are
known as unary operators. For Example increment(++) and decrement(–) operators.
2. Binary Operators:Those operators that require two operands to act upon are called
binary operators.Binary operators are classified into :
1. Arithmetic operators (+, -, * etc.)
2. Relational Operators ( <, >, ==)
3. Logical Operators (&&, ||)
4. Assignment Operators (=, +=, -=)
5. Bitwise Operators (&, |)
3. Ternary Operators: These operators requires three operands to act upon. For
Example Conditional operator(?:).
Operators Description
Relational operators: These operators are used to compare the value of two operands. For
example: checking if one operand is equal to the other operand or not, an operand is greater
than the other operand or not etc.
Logical operators: Logical Operators are used to combine two or more conditions/constraints
or to complement the evaluation of the original condition in consideration. The result of the
operation of a logical operator is a boolean value either true or false.
Assignment operators: These operators are used to assign the values for the variables in C
programs. The left side operand of the assignment operator is a variable and right side
operand of the assignment operator is a value. The value on the right side must be of the same
data-type of variable on the left side otherwise the compiler will raise an error.
Simple assignment operator. Assigns values from right C = A + B will assign the
= side operands to left side operand value of A + B to C
Increment/decrement operators: Increment Operators are used to increase the value of the
variable by one and Decrement Operators are used to decrease the value of the variable by
one in C programs. Both increment and decrement operator are used on a single operand and
variable, so it is called as a unary operator. Unary operators are having higher priority than the
other operators it means unary operators are executed before other operators.
Example of pre-increment
4
main()
5
{
6
int x,i;
7
i=10;
8
x=++i;
9
printf("x: %d",x);
10
printf("i: %d",i);
11
getch();
12
}</span>
post-increment (variable ++): In Post-increment first value of variable is used in the
expression (initialize into another variable) and then increment the value of variable.
Example of post-increment
4
void main()
5
{
6
int x,i;
7
i=10;
8
x=i++;
9
printf("x: %d",x);
10
printf("i: %d",i);
11
getch();
12
}</span>
o pre-decrement
o post-decrement
Pre-decrement (– variable): In pre-decrement first decrement the value of variable and then
used inside the expression (initialize into another variable).
Example of pre-decrement
void main()
int x,i;
i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
getch();
#include<stdio.h>
#include<conio.h>
void main()
int x,i;
i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
getch();
Conditional operators: Conditional operators return one value if condition is true and returns
another value is condition is false. This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
In above example, if A is less than 0, Negative is returned else Positive is returned. This is
equal to if else conditional statements.
Bitwise operators: These operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then calculation is performed on
the operands. Bitwise operators can not be used on float and double type data.
Operators Description
4.3.Expression
Expression: Operators, functions, constants and variables are combined together to form
expressions. Consider the expression A + B * 5. where, +, * are operators, A, B are variables,
5 is constant and A + B * 5 is an expression.
Mathematical expressions are written in c program in the following way:
Operator precedence and associativity in expression:
Operator precedence determines which operator is performed first in an expression with more
than one operators with different precedence. For example 10 + 20 * 30 is calculated as 10 +
(20 * 30) and not as (10 + 20) * 30.
Associativity is used when two operators of same precedence appear in an expression.
Associativity can be either Left to Right or Right to Left. For example ‘*’ and ‘/’ have same
precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated
as “(100 / 10) * 10”.
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
Lesson Evaluation-