Basics of c Programming
Basics of c Programming
Character set :
These are the fundamental units of a language. They are alphabets ( A- Z, a- z ), digits (0 – 9 ), Special Symbols
( + - * , < = ? etc) , White spaces (blank space, tab, new line enter etc. ).
TOKEN :
They are the fundamental building blocks of the program(Lexical units). There are 5 types of tokens in C++.
(A)KEYWORDS :
They reserved words in C++ which have a predefined meaning and cannot be used for any other purpose. Eg,
auto, break, double, int, for, if, this etc.
(B) IDENTIFIERS : They are user defined words that are used to name different program elements such as
memory locations, statements, functions, classes etc.. Identifiers used for naming memory location is known
as variables, assigned for statements are known as labels and used for set of statements are known as functions.
There are certain rules that must be followed when naming identifiers.
( i) Cannot be a key word.
(ii) The first character must be a n underscore or an alphabet.
(iii) White space and special characters are not to be used.
(iv) Upper case and lower case letters are treated differently( C++ is case sensitive).
Eg. For valid identifiers are avg, first_rank, Main, FOR etc.
(C)LITERALS( Constants) : They are data items that never change their values during the program running.
There are 4 types of literals.
( i ) Integer Literals: They are whole numbers without fractional part.
Eg. 12, -7, 012(Octal consists- starting digit is zero), 0X12a (Hexadecimal consists – starting with 0X ). (ii
)Floating point literals (Real constant): They have fractional part. They can be written in Fractional and
exponential form. Eg. 10.25, 5.01E8 =5.01 X 108. In exponential form there are two parts – Mantissa [ Part
before E, here 5.01 ] and Exponent [ part after E , here 8 – exponential part should be an integer ]. Some
valid real constants are 12.5, 12.3eo4, 15E+5, -3.234E-5, etc.
(iii) Character literals : Single character enclosed with single quotes and that never changes its value during
program run. Value of character constant is ASCII value of the character. Eg. The value of 'a' is 97, value of
'b' is 98, value of 'A' is 65 and so on. There are certain non-graphic character constants like enter key, Tab
key, backspace etc, which cannot be typed directly from the keyboard. These symbols can represented by using
ESCAPE SEQUENCES , which consists a back slash(\) followed by one or more characters. Even though
escape sequence contain more than one character enclosed in single quotes, it uses only one corresponding
ASCII code to represent it. [ \t is for horizontal tab, \0 for null character, \n for new line or line feed , \a for
alert bell, \b for backspace, etc. .] Eg valid Character Literal: ‘s’, ‘$’,’\n’ Invalid Character Literal: ‘82’, “k”,
‘\8’ .
( iv) String literals: It is a Sequence of one or more characters enclosed within a pair of double quotes. Eg.
“computer” , “123”, “C++” etc.
(D)PUNCTUATORS: Special symbols that have syntactic or semantic meaning to the compiler. A punctuator
is a token that is used in the syntax of the preprocessor.
Eg: #, : , ’ , ” ,( ) ,[ ], { , }
(E)OPERATORS: An operator is a symbol that tells the compiler about a specific operation. Operators are
the tokens that trigger some kind of operations. The operations applied on a set of data called operands Eg.
a+b, here the symbol + is an operator and a, b are the operands.
Integrated Development Environment (IDE) : The compilers such as GCC (GNU Compiler Collections ),
Turbo C++, Borland C++ provide an IDE for developing C++ programs. They provide facilities for typing,
editing, searching compiling linking and executing a C++ program. It have menu with File, Edit, Search, Run,
Compile, debug etc.
Data types: They are the means to identify the nature of the data and the set of operations that can be performed
on the data. Data types are necessary to declare variables. There are three types of data types. (A)
Fundamental data types (Built-in data types ): They are defined in the C++ compiler. They are ( i ). Int
data type. Whole numbers without fractional part. In GCC, it uses 4 bytes of memory and can store whole
numbers from -2147483648 to 2147483647 (232 = 2147483647 ).
(ii) char data type: They are symbols covered by the character set of C++ language. It uses only one byte of
memory. (storing in memory with its ASCII value).
( iii) float data type: They are numbers with fractional part. It uses 4 bytes of memory.
(iv) double data type : It is used for handling large floating point numbers with high precision. It uses 8 bytes
of memory. ( v) void data type: it uses zero bytes (null) of memory. If a function does not return a value then
the type should be void.
(B) Derived data types: They are derived from fundamental data types by some grouping or alteration in
size. Eg. Array, pointer etc.
(C) user defined data types. The programmer can define their own data types. Eg. struct, enum, union
, class etc.
Type modifiers : These are used along with the basic data types to alter the size, range and precision of the
variable. Important type modifiers are signed, unsigned, long and short. Variables: The name given
to a memory location is known as variable. It is use to store and retrieve data.
101 102 103 104
It have three part: a) variable name b). Memory address(starting address of the allocated
15
memory – also called L-value [ Location value] )and Content (the value stored in that
location – also called R – value [ read value ] ). In the figure 'n' is a variable name with int int n
data type. 101, 102 etc are the memory address. The content of the variable is 15. R-
value is 15 and L-value is 101.
Operators: They are symbols that trigger some kind of operation.Based on number of operands, operators are
classified into
(a) Unary operators - one operands( positive, negative, increment [ ++], decrement [ - - ] ).
(b). Binary operators: ( two operands- arithmetic, relational etc.) (c). Ternary operator (
conditional operators – three operands ? : ).
According to types of operation, there are
(a) Arithmetic operators : + , - , * , /, % ( modulus operator – getting remainder after integer division
. Eg. 5%2=1)
(b)Relational Operator : Here we can compare numeric data and the result will be either TRUE or FALSE.
< (less than), <= (less than or equal to ), > ( greater than ), >= (greater than or equal to ),== ( equal to ), !=
( not equal to )
(c)Logical operators : the operators are && (logical and), || (logical or ), ! (logical not). Here we can
relational expressions. The result will be either TRUE (1) or FALSE (0).
Eg. (5>2) && (3 < 4) evaluates to 1. (7<9) || (5> 8) evaluates to 1 . ! ( 5<6) evaluates to 0. (d)
Assignment operator: The assignment operator is ' = ' . It is used to store a value in a memory location.
Eg. X=5 ( the constant 5 is stored in variable X ).
(e) Input/ Output operator : The standard input device is the keyboard and the out put device is monitor. In
the input operation, the data from the keyboard is stored in a memory location. For this C++ provides the
operator ' >> ' ( get from or extraction operator. ). In output operation, the data is transferred from RAM to
monitor. The operator ' << ' (put to or insertion operator ) is used.
(f)Arithmetic assignment operator(short hand): The operators are +=, – = , * = , /=, %= . a+=10 means
a=a+10, b - =15 means b= b – 15, c =5 means c=c * 5 and so on.
(g) Increment (++ ) /decrement ( - - ) operator: a++ (post increment ) means a=a+1. ++a (pre increment
) means a=a+1; b- - (post decrement ) means b= b – 1 and - -b ( pre decrement ) means b = b – 1.
There are two versions of these operators.
Prefix form : The rule is change, then use. Eg. a= b = 10, c=++a, then the value of the variable c will be
11and a =11 ( Here the value of a is incremented by one at first and then the changed value of a is assigned to
c . ) . Let d = - -b, we get d =9 and b = 9 .
Post fix form: The rule is use, then change.
Eg. a= b = 10, c=a++, then the value of the variable c will be 10 and a =11. ( Here the value of a is
assigned to c first and then the value of a is incremented by one. That is a = b++ means a= b, b= b++) .
Let d = - -b, we get d = 10 and b = 9.
( h) Conditional Operator ( ? : ) : Here there are three operands.
Eg. Result = (mark > 30 ) ? “Passed” : “Failed” ; if the mark is greater than 30, then the result is passed
else result is failed.
( i ) sizeof operator: This operator returns the amount of memory space in bytes allocated for the operand.
The operand can be a constant, a variable or a data type.
Eg. Sizeof(int); (which is 4), sizeof (4.5); sizeof(a); etc. ( j ).
Precedence of operators: It is the order of priority given to the operators. It begins from parenthesis ( ) ,
unary operators, increment , decrement, multiplication, division, modulus, addition, subtraction, relational
operators, assignment, logical and lastly comma operator.
Eg. 7 +3 - ( 4 – 2 ) + 3 *2 - 8 = 10 – 2 + 6 - 8 = 6.
Expressions: They are composed of operators and operands. All expressions can be evaluated to get a value.
This result is known as the value returned by the expression.
They are divided into
(a) arithmetic expressions: here arithmetic operators are used. They are again classified as (i)
integer expressions: The values given will be integer only.
Eg. Let a=8, b = 3, c= a%b; gives c = 2.
(ii) Floating point expression) : Here modulus operator cannot be used.
Eg. a=5.0, b=2.0, c=a/5, then c= 2.5.
( b) Relational expression: here relational operators are used and produces boolean type results like true (1) or
false ( 0 ).
Eg a = 5, b= 2, (2*b > a ) will be 0(false ) .
(c) Logical expression: one or more relational expressions are combine with logical operator and produces
either true or false.
Eg a=5, b = 3 . ( a = b ) || ( a > b ) will be 1(True) .
Type conversion : The data type of one operand can be converted into another and it is called type conversion.
There are two ways
( a)implicit type conversion(Type Promotion) : In expression where different types of operands involved,
C++ compiler convert the lower sized operand to higher sized operand internally. . So it is known as type
promotion.
Eg. (5/2)*3+2.5 = 8.5, (5.0/2)*3+2.5=10.
(b)Explicit type conversion: Here the programmer may decide the data type of the result. Type casting is
applied on variables. Eg. Float a=5.0, b=2.0, c ; c=(int)(a/b); the output will be of he type int.
Statement : They are the smallest executable unit of a programming language. It uses a semi column ( ; ) as
delimiter. Executable statements are the instruction to the computer. There are mainly four types of statements.
(a) Declaration Statement: It is used to declare the type of the variable or constant used in the program. Each
variable should be declared before its use. Eg, int a, b; float avg;
Giving value to a variable at the time of its declaration is called variable initialization.
Eg. int a=5; float p=1000, r=6.75; If the variable initialized during the execution of the program is
known as dynamic initialization.
Eg. Int i=p*n*r/100; To make value of a variable remains unaltered through out a program, we should the key
word const . const is known as the access modifier. It is used to create constants whose value can never be
changed during execution. Eg const float pi=3.14; ( b)Assignment statement: It is used to assign a value
to a variable Eg. A=10; b= c+d;
Comments : It is used to document the program internally. They are added to describe the program. They
are ignored by the compiler.
They are two types.
(a)Single line comment: The two slashes ( // ) is used to write single line comment. (b)Multi
line comments: It write within / * and */.
Garbage value of a variable is the unpredictable value assigned to a variable at the time of declaration. When
we give names to the variables, such names can help us to remember the quantity they possess. Eg. roll_num,
emp_code, mark1, etc. These kind of identifiers are called mnemonic names.