2. C was originally developed in the 1970s, by Dennis
Ritchie at Bell Telephone Laboratories, Inc.
C is a High level , general –purpose structured
programming language.
Instructions of C consists of terms that are very closely
same to algebraic expressions, consisting of certain
English keywords such as if, else, for ,do and while
C contains certain additional features that allows it to be
used at a lower level , acting as bridge between machine
language and the high level languages.
This allows C to be used for system programming as
well as for applications programming
2
3. • What is C?
• C is a structured, relatively low-level, portable
programming language.
• Why study C?
• Many popular software tools are written in C.
• Has strongly influenced many other languages.
• C-shell, java, C++, Perl, etc.
• Forces the user to understand fundamental
aspects of programming.
• Very concise language.
3
4. C language consist of some characters set, numbers
and some special symbols. The character set of C
consist of all the alphabets of English language.
C consist of
• Alphabets a to z, A to Z
• Numeric 0,1 to 9
• Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
The words formed from the character set are building
blocks of C and are sometimes known as tokens.
These tokens represent the individual entity of
language. The following different types of token are
used in C
1) Identifiers 2) Keywords 3)
Constants 4
5. • A 'C' program consist of two types of elements , user
defined and system defined.
• Identifiers is nothing but a name given to these
elements.
• An identifier is a word used by a programmer to name
a variable , function, or label.
• identifiers consist of letters and digits, in any order,
except that the first character.
• Identifiers consist of letters and digits if any order,
except that the first character must be letter.
• Both Upper and lowercase letters can be used
5
6. • Keywords are nothing but
system defined identifiers.
• Keywords are reserved
words of the language.
• They have specific
meaning in the language
and cannot be used by
the programmer as
variable or constant
names
• C is case sensitive, it
means these must be
used as it is
• 32 Keywords in C
Programming
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short
unsigne
d
continue for signed void
default goto sizeof volatile
do if static while
6
7. • A variable is nothing but a name given to a storage area
that our programs can manipulate.
• Each variable in C has a specific type, which determines
the size and layout of the variable's memory
• The name of a variable can be composed of letters,
digits, and the underscore character.
• Upper and lowercase letters are distinct because C is
case-sensitive. There are following basic variable types
−
Type Description
• char Typically a single octet(one byte). This is an
integer type.
• int The most natural size of integer for the machine.
• float A single-precision floating point value.
• Double A double-precision floating point value.
7
8. • A constant is a value or an identifier whose value cannot
be altered in a program. For example: 1, 2.5,
• As mentioned, an identifier also can be defined as a
constant. eg. const double PI = 3.14
• Here, PI is a constant. Basically what it means is that, PI
and 3.14 is same for this program.
Integer constants
• A integer constant is a numeric constant (associated with
number) without any fractional or exponential part.
• There are three types of integer constants in C
programming:
• decimal constant (base 10)
• octal constant (base 8)
• hexadecimal constant (base 16)
8
9. Floating-point constants
• A floating point constant is a numeric constant that
has either a fractional form or an exponent form.
For example: 2.0,0.0000234,-0.22E-5
Character constants
• A character constant is a constant which uses
single quotation around characters.
For example: 'a', 'l', 'm', 'F'
String constants
• String constants are the constants which are
enclosed in a pair of double-quote marks.
For example: "good" ,"x", "Earth is roundn"
9
10. Sometimes, it is necessary to use characters which
cannot be typed or has special meaning in C
programming.
For example: newline(enter), tab, question mark etc. In
order to use these characters, escape sequence is used.
• For example: n is used for newline.
• The backslash ( ) causes "escape" from the normal way
the characters are interpreted by the compiler.
Escape Sequences Character
• b Backspace f Form feed
n Newline r Return
• t Horizontal tab v Vertical tab
• Backslash ' Single quotation mark
• " Double quotation mark ? Question mark
• 0 Null character 10
11. • An operator is a symbol which operates on a
value or a variable. For example: + is an
operator to perform addition.
C programming has wide range of operators to
perform various operations. For better
understanding of operators, these operators can
be classified as:
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators 11
12. Operator Meaning of Operator
• + addition or unary plus
• - subtraction or unary minus
• * multiplication
• / division
• % remainder after division (
modulo division)
12
13. 1. C programming has two operators
increment ++ and decrement -- to change
the value of an operand (constant or
variable) by 1.
2. Increment ++ increases the value by 1
whereas decrement -- decreases the value
by 1.
3. These two operators are unary operators,
meaning they only operate on a single
operand.
eg. int a=10, b=100 ++a = 11 13
14. • An assignment operator is used for assigning a
value to a variable. The most common
assignment operator is =
Operator Example Same as
• = a = b a = b
• += a += b a = a+b
• -= a -= b a = a-b
• *= a *= b a = a*b
• /= a /= b a = a/b
• %= a %= b a = a%b
14
15. • A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation
is false, it returns value 0.
• Relational operators are used in decision making and
loops.
Operator Meaning of Operator Example
• == Equal to 5 == 3 returns 0
• > Greater than 5 > 3 returns 1
• < Less than 5 < 3 returns 0
• != Not equal to 5 != 3 returns 1
• >= Greater than or equal to 5 >= 3 returns 1
• <= Less than or equal to 5 <= 3
return 0
15
16. • Logical operators, like relational operators, are
typically used in conditional expressions
1. if ( (a == 1) && (b < 3) || (c == 1) ) etc.
• However, these can also be used in regular
expressions
int a = 1, b = 2, c = 3, d;
d = ( a > b ) || ( c == (b – 1) );
What is the value of d here?
16
17. 17
• Comparision Operators: <, > , <=, >= , !=, ==,
!, &&, || .
•example:
• int a=4, b=5;
• a<b returns a true(non zero number) value.
• Bitwise Operators: <<, >>, ~, &, | ,^ .
•example
• int a=8;
• a= a>>1; // value of a becomes 4
18. 18
• Meaning of a + b * c ?
is it a+(b*c) or (a+b)*c
• All operators have precedence over each
other
• *, / have more precedence over +, - .
• If both *, / are used, associativity comes into
picture. (more on this later)
• example :
• 5+4*3 = 5+12= 17.
20. 20
• Input operation
• an instruction that copies data from an input
device into memory
• Output operation
• an instruction that displays information stored
in memory to the output devices (such as the
monitor screen)
21. 21
• A C function that performs an input or output
operation
• A few functions that are pre-defined in the
header file stdio.h such as :
•printf()
•scanf()
•getchar() & putchar()
22. 22
• Used to send data to the standard output
(usually the monitor) to be printed according to
specific format.
• General format:
• printf(“string literal”);
• A sequence of any number of characters surrounded by
double quotation marks.
• printf(“format string”, variables);
• Format string is a combination of text, conversion
specifier and escape sequence.
23. 23
• Example:
• printf(“Thank you”);
• printf (“Total sum is: %dn”, sum);
• %d is a placeholder (conversion specifier)
• marks the display position for a type integer
variable
• n is an escape sequence
• moves the cursor to the new line
24. Principles of Programming -
NI 2005
24
• Read data from the standard input device
(usually keyboard) and store it in a variable.
• General format:
• scanf(“Format string”, &variable);
• Notice ampersand (&) operator :
• C address of operator
• it passes the address of the variable instead of the
variable itself
• tells the scanf() where to find the variable to store the
new value
25. Principles of Programming -
NI 2005
25
• Example :
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
• Common Conversion Identifier used in printf
and scanf functions.
printf scanf
int %d %d
float %f %f
double %f %lf
char %c %c
string %s %s
26. Principles of Programming -
NI 2005
26
• If you want the user to enter more than one
value, you serialise the inputs.
• Example:
float height, weight;
printf(“Please enter your height and weight:”);
scanf(“%f%f”, &height, &weight);
27. Principles of Programming -
NI 2005
27
• getchar() - read a character from standard input
• putchar() - write a character to standard output
• Example: #include <stdio.h>
void main(void)
{
char my_char;
printf(“Please type a character: ”);
my_char = getchar();
printf(“nYou have typed this character: ”);
putchar(my_char);
}
28. Principles of Programming -
NI 2005
28
• Alternatively, you can write the previous code
using normal scanf and %c placeholder.
• Example
#include <stdio.h>
void main(void)
{
char my_char;
printf(“Please type a character: ”);
scanf(“%c”,&my_char);
printf(“nYou have typed this character: %c ”, my_char);
}
29. 29
• The operands of a binary operator must have a the
same type and the result is also of the same type.
• Integer division:
c = (9 / 5)*(f - 32)
The operands of the division are both int and hence the
result also would be int. For correct results, one may
write
c = (9.0 / 5.0)*(f - 32)
• In case the two operands of a binary operator are
different, but compatible, then they are converted to the
same type by the compiler. The mechanism (set of
rules) is called Automatic Type Casting.
c = (9.0 / 5)*(f - 32)
30. 30
1. char and short operands are converted
to int
2. Lower data types are converted to the
higher data types and result is of higher
type.
3. The conversions between unsigned and
signed types may not yield intuitive
results.
4. Example
float f; double d; long l;
int i; short s;
d + f f will be converted to double
i / s s will be converted to int
l / i i is converted to long; long
Hierarchy
Double
float
long
Int
Short and
char
31. 31
• Identify the start of the program
• Every C program has a main ( )
• 'main' is a C keyword. We must not use it for
any other variable.
• 4 common ways of main declaration
int
main(void)
{
return 0;
}
void
main(void)
{
}
main(void)
{
}
main( )
{
}
32. • Note: all statements end with a semicolon;
• Statements can (with a few exceptions) be
broken across lines or ganged on a single line
• Commas separate multiple declarations
• Blank lines have no effect
• Extra spaces between tokens has no effect.
• Comments are ignored by the compiler
32
33. 33
• A specification of an action to be taken by the
computer as the program executes.
• Each statement in C needs to be terminated with
semicolon (;)
• Example:
#include <stdio.h>
void main(void)
{
printf(“I love programmingn”);
printf(“You will love it too once ”);
printf(“you know the trickn”);
}
statement
statement
statement
34. • So far our C programs are as follows:
/* description of program */
#include <stdio.h>
/* any other includes go here */
int main(){
/* program body */
return 0;
}
• Let’s learn more about the structure of “program
body” 34