C Programming Notes PDF
C Programming Notes PDF
C Programming Notes PDF
BASICS OF C LANGUAGE
Introduction
The C language was developed in early 1970s at AT&T labs by Brian Kernighan and
Dennis Ritchie. The language was originally developed to write the UNIX operating
system. Since that time, literally thousands of applications have been written in C
Character Set of C
Alphabets : a to z, A to Z
Numbers : 0,1 to 9
Special Symbols : +–*/\&|% !?[]{} ><= ._:;^~'"
White Space : space, horizontal tab, formfeed, newline, and carriage return
Escape Sequences
The output of a C program may also use a combination of characters such as \b, \n, \t,
etc. These combinations are known as escape sequences. The escape sequence
represents a single character, even though it is written as a two or more character. The
following table gives the escape sequence used in C
1
Sequence Meaning Sequence Meaning
\a alert sound \v vertical tab
\b backspace \’ single quote
\f formfeed \” double quote
\f newline \? question mark
\r carriage return \\ backslash
\t asterisk \0 null
Keywords of C
The C programming language keeps a small set of keywords for its own use. These
keywords cannot be used as identifiers in the program. The C language specifies 32
keywords. Here is the list of keywords used in Standard C; you will notice that none of
them use upper-case letters.
Keywords are case sensitive. For example, return is a keyword and it must be used as
is. So it cannot be used as Return, or RETURN.
The keywords have special meaning in the language and cannot be used for any other
purpose such as constant name or variable name.
Identifiers in C
An identifier is a word used in the program for any variable, function, data definition,
etc. In the programming language C, an identifier is a combination of alphanumeric
characters
The first character must be a letter of the alphabet or an underline, and the
remaining being any letter of the alphabet, any numeric digit, or the underline.
Both upper and lower case letters can be used. Identifiers are case sensitive. Thus
using "salary" for a variable is not the same as using "SALARY" and neither of them
is same as using "saLAry" for a variable. All three refer to different variables.
An identifier can use an underscore sign. For example the identifier " student_name
" contains the underscore.
2
In old C up to eight characters can be used for identifiers. If more than eight are
used, they may be ignored by the compiler. In Standard C this limit has changed to
31 characters. In practice the actual limit will depend on the C compiler.
Variable Names in C
The following are invalid variable names. The reason why each name is invalid is also
given.
Numbers: These are further divided into two types: integer and real. A number without a
decimal point is known as an integer (or a whole number). A number with decimal point
is known as a real number.
int: This requires 2 bytes of memory and can store 65536 different numbers. The
range of these numbers are from – 32768 to +32767
long: This requires 4 bytes of memory and can store 4294967295 different
numbers. Thus it can store larger numbers. The range of these numbers are from
–2147483648 to +2147483647
3
C defines two basic types of real numbers: float and double.
float: This requires 4 bytes of memory. It is used to store numbers. In the range
3.4 × 10–38 to +3.4 × 10+38
double: This requires 8 bytes of memory. It is used to store numbers in the range
1.7× 10–308 to 1.7× 10+308 . Thus it can store larger numbers.
To represent individual characters, C uses the char type. This requires only one byte of
memory. Each represented character will have a corresponding integer value in the
range 0 – 255. Thus a char is a special type of integer.
Most machines today use the ASCII character set, in which the letter A is represented by
the integer code 65, the digit 1 is represented by integer code 49, the space character is
represented by the integer code 32, etc.
The following table gives the memory requirement and range of various data types in C.
Basic Constants in C
There are four basic types of constant in C. They are: integer constant, floating-point
constant, character constant and string constant. The first two represent numbers and
are hence also known as the numeric constants.
A negative constant is preceded by a minus sign. For positive constant the plus sign
is optional.
blank space and comma is not permitted within a constant.
the value of constant must be within the specified range.
If the number has two or more digits, the first digit must not be 0.
4
Integer Constant: A constant number without a decimal point is an integer constant.
The following are examples of invalid numeric constant. The reason why each is invalid
is also given.
Decimal: This is the base 10 number system. This system uses the digits from 0 to 9.
Octal : This is the base 8 number system. This system uses the digits 0 to 7. The first
digit must be zero (0) in order to identify it as an octal number. Following are
examples of valid octal integer constants.
Hexadecimal: This is the base 16 number system. This system uses the digits 0 to 9
and A, B, C, D, E, F. The letters A through F represent the decimal quantities 10
through 15 respectively.
A hexadecimal integer constant must begin with a 0x or 0X. It can then be followed
by sequence of valid digits. Following are examples of valid hexadecimal integer
constants.
Floating Point Constant : A constant number with decimal is a floating point number.
Such numbers may also be expressed in exponential (scientific) form. For example the
value 356.3 can be written as 3.563e2 where e2 means multiply by 102.
The mantissa must be real or integer. The exponent must be an integer a minus or
optional plus sign. Following are examples of valid floating point constants.
A string constant can contain the newline character sequence \n. For example ‘I\n
am\n happy’ is a valid string constant.
5
Test Yourself:
Variable Declaration
In general a C program will make use of variables to store data. All variables used in a
program must be declared with specific data type.
datatype v1,v2,... , vn ;
where v1, v2,... vn are name of variables. The variables are separated by comma. A
declaration statement must end with a semicolon
The following lines declares four variables: a of type int , x of type float, m of type double
and r of type char.
int a;
float x;
double m;
char r;
Two or more variables of the same type can be declared in one line.
int a,b,c;
float x,y,weight;
Assignment
A variable can be assigned value after declaration. The example shows three variables
declared of type integer. The variables are assigned values later in the lines that follow.
int a,b,pi;
.. .. ..
.. .. ..
a = 10;
b = 20;
pi = 3.1412;
We can also assign values to variables at the same time when a variable is declared as
follows:
6
In these lines: age is an integer variable given initial value 35, and code is a char type
variable given the initial value ‘A’
C also supports multiple assignments. For multiple assignments the following syntax is
used:
a=b=c=55 ;
Arithmetic Expressions
The basic evaluation procedure is from the left to the right. An arithmetic expression
within parentheses is evaluated left-to-right using the rules of precedence of operators.
High priority : * / %
Low priority : + –
The evaluation is done in two left-to-right passes. During the first pass the high priority
operators are applied. During the second pass the low priority operators are applied.
Evaluation is as follows:
7
x = 1 + 3 (5-4 evaluated)
x = 4 (1+3 evaluated)
When parentheses are nested, the innermost parentheses have highest priority.
For example :
15-(16/(2+2)*2)+3
15-((16/2)+2)*2)+3
The first expression evaluates to 10, whereas the second expression evaluates to –2.
Parentheses can also be used to improve the readability of the program.
Expressions
a+b-c ;
x=a ;
x>6 ;
The first expression uses the arithmetic operators + and –, the second expression uses
the assignment operator =, the third expression uses the relational operator >. These
operators are discussed later.
Questions:
8
Some Simple C programs
Programs written in C
Program 1: A C program consists of one or more functions. In language have to be
the example given below main() is such a function. All C compiled and then
programs must have a main() function. executed. There is no
single compiler of C.
These compilers differ
main()
from one another.
{ Even on the same
printf("hello, world"); computer there may
} be several compilers.
Execution of the above program begins at the first statement of main. The main function
will usually invoke other functions to perform its job, some coming from the same
program, and others from libraries.
printf is a library function which will format and print output on the terminal (unless
some other destination is specified). In this case it prints the message ‘hello, world’. The
output is shown below.
hello, world
Program 2: Here's a bigger program that adds three integers and prints their sum.
main ()
{
int a, b, c, sum;
a = 1; b = 2; c = 3;
sum = a + b + c;
printf("sum is %d", sum);
}
sum is 6
Program 3: Here's a similar program that adds three floating point numbers and prints
their sum.
# include <stdio.h>
main ()
{
float a, b, c, sum ;
a = 1.0 ; b = 2.0 ; c = 3.0;
sum = a + b + c ;
printf ("sum is %f", sum) ;
}
9
The output is shown below.
sum is 6.000000
10