Basics of “C” Programming
History
• In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the
publication of The C Programming Language by Kernighan
& Ritchie caused a revolution in the computing world
Terminologies of ‘C’
1. Keywords
2. Identifiers
3. Variables
4. Constants
5. Special Symbols
6. Operators
7. Character & String
1. Keywords
• Keywords are the reserved words whose meaning has already
been explained to the C compiler.
• C has 32 keywords.
• These keywords combined with a formal syntax form a C
programming language.
• Rules to be followed for all programs written in C:
All keywords are lower-cased.
C is case sensitive.
Keywords cannot be used as a variable or
function name.
2. Identifiers
• Identifiers refer to the name of variables, functions and arrays.
• These are user-defined names and consist of sequence of letters
and digits, with a letter as a first character.
• Both uppercase and lowercase letters are permitted, although
lowercase letters are commonly used .
• The underscore character is also permitted in identifiers. It is
usually used as a link between two words in long identifiers.
Identifier Names
Some correct identifier names are -
arena, s_count
marks40
class_one
Some erroneous identifier names are -
X
1stsst
oh!god
start….end
An identifier cannot be the same as a C keyword
3. Variables
• Variables are named locations in memory that are used to hold
a value.
• A variable may take different values at different times during
execution.
• The syntax for declaring a variable is –
DataType IdentifierName ;
• Example- int num;
long int sum , a;
4. Constants
• Constants are the fixed values that do not change
during the execution of a program.
• C supports several types of constants.
– Numeric Constants
• Integer constants
• Real constants
– Character Constants
• Single character constant
• String Constants
5. Special Symbols
• ! , @, #, $ , & , * , ….. These all symbols that can be
find on Keyboard, are called Special Symbols.
• Every symbol has its special meaning different place
that’s why it is called Special Symbols.
6. Operators
• Operator is a symbol that operates on one or more
operands and produces output.
Example - c=a+b;
• In the above Example the symbols + and = are
operators that operate on operands a, b , and c .
Types of Operators
• Arithmetic Operators
– They are used to perform Mathematical operations between
2 or more operands.
– It includes:
• + [Addition]
• - [Subtraction]
• * [Multiplication]
• / [Division]
• % [Modulus]
• ++ [Increment Operator]
• -- [Decrement Operator]
Comparison Operators
• A comparison operator is a symbol used to compare two or
more operands.
• It includes:
– == [Checks if the value of two operands is equal or not, if yes then condition becomes
true.]
– != [Checks if the value of two operands is equal or not, if values are not equal then
condition becomes true.]
– > [Checks if the value of left operand is greater than the value of right operand, if yes
then condition becomes true.]
– < [Checks if the value of left operand is less than the value of right operand, if yes then
condition becomes true.]
– >= [Checks if the value of left operand is greater than or equal to the value of right
operand, if yes then condition becomes true.s]
– <= [Checks if the value of left operand is less than or equal to the value of right operand,
if yes then condition becomes true.]
Logical Operators
• A logical operator is a symbol used to connect two or more
operands.
• It includes:
– && [Called Logical AND operator. If both the operands are non zero then
only condition becomes true.]
– | | [Called Logical OR Operator. If any of the two operands is non zero then
then condition becomes true.]
– ! [Called Logical NOT Operator. Use to reverse the logical state of its
operand. If a condition is true then Logical NOT operator will make false.]
7. Character & String
• The Characters can be used to form words, numbers
and expressions.
• The characters in C are grouped into the following
categories :
– Letters
– Digits
– Special characters
– White Spaces
• Remember that a character ‘a’ is not equivalent to the
string “a”.
Why use C?
• Mainly because it produces code that runs nearly as fast as
code written in assembly language. Some examples of the use
of C might be:
– Operating Systems
– Language Compilers
– Assemblers
– Text Editors
– Video Games
– Network Drivers
– Modern Programs, etc.
Why C Still Useful?
• C provides:
Efficiency, high performance and high quality
flexibility and power
many high-level and low-level operations middle level
Stability and small size code
Provide functionality through rich set of function libraries
Gateway for other professional languages like C C++ Java
• C is used:
System software Compilers, Editors, embedded systems
Also used in application programs.
A simple Program of C
/* Write a program to print a message */
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“ C Programming”);
getch( );
}
Comment about the program should be enclosed within ‘/*’ &
‘*/’.
printf() is a function which is used to print messages on the
screen.
main() is a function from which execution of the program
starts.
Here stdio.h is a library file which contains standard
input/output functions , keywords etc.
#include< > is used to define the library file that is to be used
in the program for compiler information.
Escape Sequence
• \n new line
• \t tab
• \r carriage return
• \a alert
• \\ backslash
• \” double quote
Control FLOW Statements
• C language has decision making capabilities and
supports controlling of statements.
• C supports following decision making statements:
1. IF
2.IF-ELSE and its various form
3. Switch
4. Conditional Operator
If statement
• The if statement is a powerful decision making
statement.
• The if statement is used to control the flow of
execution of statements.
• It is a two way decision statement which is used in
conjunction with an expression.
Different forms of If statement
• Simple If statement.
• If…..else statement.
• Nested if…..else statement.
• Else if ladder.
Simple if statement
• If the if expression evaluates to true, the block
following the if statement or statements are executed.
• The general form of a simple if statement is :
if (test-expression)
{
statement-block;
}
If….Else statement
• The general form of if……else statements is :
if(expression){
statement;
}
else{
statement;
}
• The else statement is optional. It is used only if a
statement or a sequence of statements are to be
executed in case the if expression evaluates to false.
Else if ladder
• The general form of else if ladder is:
• The conditions are evaluated from the top downwards.
Nested if…else statement
• The general form of nested if…else statement is:
if (test-expression 1)
{
if (test-expression 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}
Statement x;
Switch – case statement
• The switch-case control statement is a multi-way decision maker that tests
the value of an expression against a list of integers or character constants.
• When a match is found, the statements associated with that constant are
executed.
• The syntax of switch-case is:-