Professional Documents
Culture Documents
INTRODUCTION
KEY-WORDS
1
By
Hare Krishna P.Vemana
Flowcharts
SYMBOL EXAMPLE
START
Terminal symbol
Calculate new
value
Sort table
of data
Subprocess/subprogram
Check temperature
Decision symbol
C-HISTORY:
2
By
Hare Krishna P.Vemana
C is a programming language which born at “AT & T’s Bell Laboratory” of USA in 1972.
C was written by Dennis Ritchie, thats why he is also called as father of c programming
language.
C language was created for the UNIX operating system (which is currently base of many UNIX
based OS).
Its use quickly spread beyond Bell Labs in the late 70’s because of its long list of strong
features.
Many of C’s principles and ideas were derived from the earlier language B. (Ken Thompson
was the developer of B Language.)
BCPL and CPL are the earlier ancestors of B Language
CPL is “common Programming Language” .In 1967, BCPL Language ( Basic CPL ) was
created as a scaled down version of CPL
As many of the features were derived from “B” Language thats why it was named as “C”.
After 7-8 years C++ came into existence which was first example of object oriented
programming.
Summary of C Programming Language History
Summary
5 Year 1972
C Programming Language Timeline:
Programming Development
Developed by
Language Year
3
By
Hare Krishna P.Vemana
4
By
Hare Krishna P.Vemana
Link Section
Definition Section
main ()
{
Declaration Section
Executable part
……
}
Subprogram section
Function 1
Function 2
.
.
Function n
Explanation:
Sections Description
Documentation section We can give comments about the program, creation or
modified date, author name etc in this section.
The characters or words or anything which are given
between “/*” and “*/”, won’t be considered by C compiler
for compilation process.
These will be ignored by C compiler during compilation.
Example : /* comment line1 comment line2 comment 3 */
Link Section Header files that are required to execute a C program are
written in this section.
Example #include<stdio.h>
Definition Section In this section, variables, symbolic constants are defined
and values are set to these variables. Ex #define X 123
#define is a pre-processor directive not a statement so it
not end with ;
Global declaration section Global variables are defined in this section.
and Global variable is a variable which is to be used
throughout the program, can be defined in this section.
Function prototype Function prototype gives much information about a
declaration section function like return type, parameter names used inside
the function.
All user defined functions are declared inside in this
section
main( ) function Every C program is started from main function and this
5
By
Hare Krishna P.Vemana
A SIMPLE C PROGRAM:
#include <stdio.h>
int main()
{
/* Our first simple C basic program */
printf("Hello World! ");
getch();
return 0;
}
Program Explanation:
Program code/line Explanation
#include <stdio.h> This is a preprocessor command that includes,links standard
input output header file(stdio.h) from the C library before
compiling a C program
int main() This is the main function from where execution of any C
program begins.
{ This indicates the beginning of the main function.
getch(); This command waits for any character input from keyboard.
6
By
Hare Krishna P.Vemana
#include<stdio.h>
void main()
{
printf("Hello World");
}
Output
Hello World
1. #include<stdio.h>:
First statement started with # it is called pre-processor directive.
#include<stdio.h> is used to include the stdio.h header file in our program.
Header files contains the functions that we use in our program.
Here we have used printf() function which is present in stdio.h header file.
2. void main():
Here main() is the function.
A program always starts with the main() function and every program must have main().
Here void is the return type of this function.void means main() function will not return
anything.
The opening curly braces { and closing curly braces } shows the body of the function.
o main() can also be called as a collection of statements.
3. printf():
Here printf() is output function.
This is used to print the values on the screen.
Its general form is
printf(“Statement you want to print on screen”);
4. Semicolon (;)
is used for denoting the termination of statement.
It is also called statement terminator in C Language.
Here we are using TURBO C compiler. If you have not downloaded it yet than download it from here.
7
By
Hare Krishna P.Vemana
Well this is the easiest C program. Now lets move on and try slightly complicated program of
multiplication of two numbers.
#include<stdio.h>
void main()
{
int a, b, c;
a=3;
b=4;
c=a*b;
printf("Answer is %d",c);
}
Output
Answer is 12
1. First two statements are same as above program. In the third statement I have written
int a, b, c;
Here int is the keyword for integer and a, b and c are the integer variables. So they can only store
integer values.
a=3;
b=4;
In these statements we are storing the values 3 and 4 in a and b variables respectively.
c=a*b;
We are multiplying the values in a and b, and storing them in the variable c.
8
By
Hare Krishna P.Vemana
%d for integers
%c for characters
%f for floating point numbers or real numbers
printf(“%d”,b);
scanf() in C
scanf() is used to take data(input) from the user.
Till now we have wrote programs in which we declared variables with some values. But in practice we
need to take/give/read input from users by using scanf().
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter two values to do multiplication");
scanf("%d%d",&a,&b);
c=a*b;
printf("Your answer is %d",c);
}
Now let’s try to understand this program.
1. First two instructions are same like our previous programs.
2. In the third instruction we are declaring three variables of integer(int) type.
3. In the fourth instruction we are printing the statement using printf() function.
4. In the fifth instruction we are taking input from the user through scanf() function.
In this scanf() function we have done two things.
a. We have given the format specifier %d instructed the compiler that we want to input integer value.
b. We have used ampersand (&) which is also called “address of operator”. By using this we
instruct the compiler we want to store that input in that variable (a and b).
Why do we use ampersand operator (&)?
As I have said already it is a “address of operator”. By using this operator we specify the address of
variable to the compiler.
Try making these programs yourself (take values from user)
9
By
Hare Krishna P.Vemana
DATA-TYPES
Data-type represents the type, size and range of data values in a program inside a memory.
C-supports a variety of data-types to handle different type of data values.
char To store character data values. such as ‘1’ ‘a’ ‘A’ ‘@’
1 byte ( 8 bits ) with range -128 to 127
int To Store whole number values such as 327,786,777,123..
16-bit OS : 2 bytes with range -32768 to 32767
32-bit OS : 4 bytes with range -2,147,483,648 to
2,147,483,647
float To store Real number values(decimals).such as
123.45,99.99
4 bytes with range 10-38 to 1038 with 7 digits of precision
double 8 bytes with range 10-308 to 10308 with 15 digits of
precision
10
By
Hare Krishna P.Vemana
The signed and unsigned modifiers may be applied to types char and int and will simply
change the range of possible values.
For example an unsigned char has a range of 0 to 255, all positive, as opposed to a signed
char which has a range of -128 to 127. An unsigned integer on a 16-bit system has a range of
0 to 65535 as opposed to a signed int which has a range of -32768 to 32767.
Note however that the default for type int or char is signed so that the type signed char is always
equivalent to type char and the type signed int is always equivalent to int.
The long modifier may be applied to type int and double only.
A long int will require 4 bytes of storage no matter what operating system is in use and has a
range of -2,147,483,648 to 2,147,483,647.
A long double will require 10 bytes of storage and will be able to maintain up to 19 digits of
precision.
The short modifier may be applied only to type int and will give a 2 byte integer independent of
the operating system in use.
Note: Note that the keyword int may be omitted without error so that the type unsigned is the same as
type unsigned int, the type long is equivalent to the type long int, and the type short is equivalent to
the type short int.
11
By
Hare Krishna P.Vemana
Pointer: A pointer is a variable which holds the address of another variable. Pointers are memory
related variables.
Ex int *p,a;
P=&a;
Structure: A structure is a collection of heterogeneous elements. i.e we can store different types of
elements inside a structure.
Ex struct student
{
int no,
char sname,
float fees
};
Size of a structure is sum of its data elements size, the above structure size is(2+1+4)=7 bytes
Union: the union is similar to structures but in which union elements are stored in same memory
location.
The size of union is it largest data-element size.
union emp
{
int eno;
char name;
float sal;
};
The size of union is it largest data-element size. Here, float sal has 4 bytes size which is largest among
union. so it will be the size of the union emp
Review Questions:
o Define data-type?
o Define type-qualifiers in C.
o What is long and short?
o Define signed and unsigned.
o What is enum?
12
By
Hare Krishna P.Vemana
o Write a c-program to input different input values from keyboard and print them.
13
By
Hare Krishna P.Vemana
Supported data
Format specifier Description
types
char
%c Character
unsigned char
short
Signed unsigned short
%d
Integer int
long
Scientific
float
%e or %E notation of
double
float values
Similar as %e float
%g or %G
or %E double
Signed
%hi short
Integer(Short)
Unsigned
%hu unsigned short
Integer(Short)
short
Signed unsigned short
%i
Integer int
long
Signed
%l or %ld or %li long
Integer
Signed
%lli, %lld long long
Integer
Unsigned
%llu unsigned long long
Integer
short
Octal unsigned short
%o representatio int
n of Integer. unsigned int
long
%p Address of void *
pointer to
14
By
Hare Krishna P.Vemana
void void *
%s String char *
short
Hexadecimal
unsigned short
representatio
%x or %X int
n of Unsigned
unsigned int
Integer
long
%n Prints nothing
Prints %
%%
character
15
By
Hare Krishna P.Vemana
C TOKENS
C tokens, Identifiers and Keywords are the basics in a C program. All are explained in this
page with definition.
C TOKEN:
C tokens are the basic buildings blocks in C language which are constructed together to write
a C program.
Each and every smallest individual units of a program are known as tokens.
C tokens are of six types.
They are,
1. Keywords (eg: int, while,auto),
2. Identifiers (eg: main, total),
3. Constants (eg: 10, 20),
4. Strings (eg: “total”, “hello”),
5. Special symbols (eg: (), {}),
6. Operators (eg: +, /,-,*)
IDENTIFIERS IN C LANGUAGE:
•Each program elements in a C program are given a name called identifiers.
•Names given to identify Variables, functions and arrays are examples for identifiers.
eg. x is a name given to integer variable in above program.
RULES FOR CONSTRUCTING IDENTIFIER NAME IN C:
1. First character should be an alphabet or underscore.
2. Succeeding characters might be digits or letter.
3. Punctuation and special characters aren’t allowed except underscore.
4. Identifiers should not be keywords.
KEYWORDS IN C LANGUAGE:
Keywords are pre-defined, reserved words in a C .
Each keyword is meant to perform a specific function in a C program.
Since keywords are referred names for compiler, they can’t be used as variable name.
C language supports 32 keywords which are given below.
Keywords in C Language
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
Variables
In programming, a variable is a container (storage area) to hold data.
A variable is a name for the memory location.
To indicate the storage area, each variable should be given a unique name (identifier).
Variable names are just the symbolic representation of a memory location. For example:
int playerScore = 95;
Here, playerScore is a variable of integer type. The variable is assigned value: 95.
The value of a variable can be changed, hence the name 'variable'.
In C programming, you have to declare a variable before you can use it.
16
By
Hare Krishna P.Vemana
Constants
17
By
Hare Krishna P.Vemana
2. 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
Note: E-5 = 10-5
3. Character constants
A character constant is a constant which uses single quotation around characters. For example: 'a', 'l',
'm', 'F'
4. Escape Sequences
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
Escape
Character
Sequences
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
18
By
Hare Krishna P.Vemana
Operators in C Language
C language supports a rich set of built-in operators. An operator is a symbol that tells the
compiler to perform certain mathematical or logical manipulations. Operators are used in
program to manipulate data and variables.
C operators can be classified into following types,
Arithmetic operators
Relation operators
Logical operators
Bitwise operators
Assignment operators
Conditional operators
Special operators
Arithmetic operators
C supports all the basic arithmetic operators. The following table shows all the basic arithmetic
operators.
Operator Description
+ adds two operands
- subtract second operands from first
* multiply two operand
/ divide numerator by denumerator
% remainder of division
++ Unary-Increment operator increases integer value by one
-- Unary-Decrement operator decreases integer value by one
Relation operators
The following table shows all relation operators supported by C.
Operator Description
== Check if two operand are equal
!= Check if two operand are not equal.
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand
Logical operators
C language supports following 3 logical operators. Suppose a=1 and b=0,
Bitwise operators
Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of
bitsfrom right to left. Bitwise operators are not applied to float or double.
Operator Description
19
By
Hare Krishna P.Vemana
Assignment Operators
Assignment operators supported by C language are as follows.
Conditional operator
It is also known as ternary operator and used to evaluate conditional expression.
epr1 ? expr2 : expr3
If epr1 Condition is true ? Then value expr2 : Otherwise value expr3
Special operator
Operator Description Example
sizeof Returns the size of an variable sizeof(x) return size of the variable x
& Returns the address of an variable &x ; return address of the variable x
* Pointer to a variable *x ; will be pointer to a variable x
20
By
Hare Krishna P.Vemana
Pre-increment operator is used to increment the value of variable before using in the
expression.
In the Pre-Increment value is first incremented and then used inside the expression.
b = ++y;
In this example suppose the value of variable ‘y’ is 5 then value of variable ‘b’ will be 6
because the value of ‘y’ gets modified before using it in a expression.
Post-increment operator is used to increment the value of variable as soon as after executing
expression completely in which post increment is used.
In the Post-Increment value is first used in a expression and then incremented.
b = x++;
In this example suppose the value of variable ‘x’ is 5 then value of variable ‘b’ will be 5
because old value of ‘x’ is used.
21
By
Hare Krishna P.Vemana
Storage classes
Storage class represents variables life-time and scope
Storage class specifies in C language tells the compiler
where to store a variable,
how to store the variable,
what is the initial value of the variable and
Life time of the variable.
Syntax:
storage_specifier data_type variable _name;
here,Storage_specifier is auto,extern,static or register and
data-type is any valid data type such as int ,float ,char..
and variable name is valid name of the variable
22
By
Hare Krishna P.Vemana
NOTE:
For faster access of a variable, it is better to go for register specifiers rather than auto
specifiers.
Because, register variables are stored in register memory whereas auto variables are stored in
main CPU memory.
Only few variables can be stored in register memory. So, we can use variables as register that
are used very often in a C program.
void increment(void)
{
auto int i = 0 ;
printf ( "%d ", i ) ;
i++;
}
0000
23
By
Hare Krishna P.Vemana
0123
The scope of this extern variable is throughout the main program. It is equivalent to global variable.
Definition for extern variable might be anywhere in the C program.
#include<stdio.h>
int x = 10 ;
int main( )
{
extern int y;
printf("The value of x is %d \n",x);
printf("The value of y is %d",y);
return 0;
}
int y=50;
OUTPUT:
The value of x is 10
The value of y is 50
24
By
Hare Krishna P.Vemana
}
return 0;
}
OUTPUT:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
25