Fundamentals of C-Programming
C is a general-purpose, structured programming language. C resembles other high-level structured programming
languages such as Pascal and Fortran. C also contains certain additional features, however, that allow it to be used at a
lower level, thus bridging the gap between machine language and the more conventional high-level languages. This
flexibility allows C to be used for systems programming (e.g., for writing operating systems) as well as for
applications programming (e.g., for writing a program to solve a complicated system of mathematical equations, or for
writing a program to bill customers).
C was originally developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories, Inc. (now a part of AT&T).
It is an outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell Laboratories. C was
largely confined to use within Bell Laboratories until 1978, when Brian Kernighan and Ritchie published a definitive
description of the language.* The Kernighan and Ritchie description is commonly referred to as “K&R C.”
Structure of a C Program
Every C program consists of one or more modules calledfunctions. One of the functions must be called main. The
program will always begin by executing the main function, which may access other functions. Any other function
definitions must be defined separately, either ahead of or after main.
Importance/Features of C language
• C is robust language whose rich set of built in functions and operators can be used to write any complex program.
• It is well suited for writing both system software and application software.
• Programs written in C are efficient and faster, this is due to variety of data-types and powerful operators.
• C is highly portable.
• C is a structured programming language.
• C has ability to extend itself.
Character set
A character denotes any alphabet, digit or special symbol used to represent
information. Valid alphabets, numbers and special symbols allowed in C are
The alphabets, numbers and special symbols when properly combined form
constants, variables and keywords.
Keyword & Identifiers
Keyword / Reservd word
There are certain words reserved for doing specific task, these words are known as
reserved word or keywords. These words are predefined and always written in
lower case or small letter. These keywords cannot be used as a variable name as it
has predefined meaning and can be used only for their intended purpose.
Some examples are int, short, signed, unsigned, default, volatile, float, long,
double, break, continue, typedef, static, do, for, union, return, if, else, while,
do, extern, register, enum, case, goto, struct, char, auto, const etc.
Identifiers
Identifiers are user defined word used to name of entities like variables, arrays, functions,
structures etc. Rules for naming identifiers are:
1) Identifier name should only consists of alphabets (both upper and lower
case), digits and underscore (_) sign.
2) First characters should be alphabet or underscore
3) It should not be a keyword
4) Both upper case and lower case characters are allowed but they are
not interchangeable.
5) The maximum length is 32 characters long.
Data Types
Basic Data Types
i) int
ii) float
iii) double
iv) char
v)
Basic data type Data type with type Size Range
qualifier (byte)
char char or signed char 1 -128 to 127
Unsigned char 1 0 to 255
int int or signed int 2 -32768 to 32767
unsigned int 2 0 to 65535
short int or signed short int 1 -128 to 127
unsigned short int 1 0 to 255
long int or signed long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float float 4 -3.4E-38 to 3.4E+38
double double 8 1.7E-308 to 1.7E+308
Long double 10 3.4E-4932 to 1.1E+4932
Constants
Constant is a any value that cannot be changed during program execution. In C,
any number, single character, or character string is known as a constant. A constant
is an entity that doesn’t change whereas a variable is an entity that may change.
For example, the number 50 represents a constant integer value. The character
string "Programming in C is fun.\n" is an example of a constant character string. C
constants can be divided into two major categories:
Primary Constants Secondary Constants
Primary Constant
Numeric constant
Character constant
String constant
Numeric Constant
It is categorized a integer constant and real constant. An integer constants are
whole number which have no decimal point. Types of integer constants are:
Decimal constant: 0 ------ 9(base 10)
Octal constant: 0 ------ 7(base 8)
Hexa decimal constant: 0----9, A ----- F(base 16)
Real constant is also called floating point constant. To construct real constant we
must follow the rule of ,
-real constant must have at least one digit.
-It must have a decimal point.
-It could be either positive or negative.
-Default sign is positive.
-No commas or blanks are allowed within a real constant. Ex.: +325.34
426.0
-32.76
To express small/large real constant exponent(scientific) form is used where
number is written in mantissa and exponent form separated by e/E. Exponent can
be positive or negative integer but mantissa can be real/integer type, for example
3.6*105=3.6e+5. By default type of floating point constant is double, it can also be
explicitly defined it by suffix of f/F.
Character constant
Character constant represented as a single character enclosed within a single quote. These can
be single digit, single special symbol or white spaces such as ‘9’,’c’,’$’, ‘ ’ etc. Every character
constant has a unique integer like value in machine’s character code as if machine using ASCII
(American standard code for information interchange). Some numeric value associated with
each upper and lower case alphabets and decimal integers are as:
A ------------ Z ASCII value (65-90)
a ------------ z ASCII value (97-122)
0-------------9 ASCII value (48-59)
; ASCII value (59)
String constant
Set of characters are called string and when sequence of characters are
enclosed within a double quote (it may be combination of all kind of symbols) is a
string constant. String constant has zero, one or more than one character and at the
end of the string null character(\0) is automatically placed by compiler. Some
examples are “,sarathina” , “908”, “3”,” ”, “A” etc. In C although same characters
are enclosed within single and double quotes it represents different meaning such
as “A” and ‘A’ are different because first one is string attached with null character
at the end but second one is character constant with its corresponding ASCII value
is 65.
Symbolic constant
Symbolic constant is a name that substitute for a sequence of characters and,
characters may be numeric, character or string constant. These constant are
generally defined at the beginning of the program. The Syntax is
#define NAME value
here NAME is generally written in upper case for example
#define PI 3.142
#define MAX 10
#define CH ‘b’
#define NAME “sony”
Variables
Variable is a data name which is used to store some data value or symbolic
names for storing program computations and results. The value of the variable
can be change during the execution. The rule for naming the variables is same as
the naming identifier. Before used in the program it must be declared.
Declaration of variables specify its name, data types and range of the value that
variables can store depends upon its data types.
Syntax:
data-type var-1, var-2, ………var-n;
Example int a, number, max_no, max-value;
float average;
char flag;
Expressions/Assignment statement:
An expression or assignment statement is a combination of variables,
constants, operators and function call. It can be arithmetic, logical and
relational for example:-
int z= x+y // arithmatic expression
Operator :
1. Arithmetic Operator
+ addition, - Subtraction, * Multiplication, / Division, % (modulus operator)
2. Relational Operator
< less than, > Greater than, <= less than or equal to
>= less than or equal to, = = equal to, != Not equal to
3. Logical Operator
&& and, | | or, ! not
4. Assignment operator & additional assignment operators
= Equal to
+=, -=, *=, /=, %=
Eg : x=x+10; can be written as x+=10;
5. Unary Operators
++ Increment operator will increase the value of its operand always by 1
- - Decrement Operator will decrease the value of its operand always by 1
For eg : int x=10, y=0;
y=x++; y=10 and x=11
y=++x; y=11 and x=11
Sizeof : This unary operator gives the size of its operand.
For example : sizeof(int); will give output 2
Sizeof(float) will give output 4
Precedence and associativity of operators / Hierarchy of operation
The sequence of operation to be perform in an expression is known as
Hierarchy of operation or Precedence and associativity of operators
Operators Description Precedence level Associativity
() function call 1 left to right
[] array subscript
arrow operator
. dot operator
+ unary plus 2 right to left
- unary minus
++ increment
-- decrement
! logical not
~ 1’s complement
* indirection
& address
(data type) type cast
sizeof size in byte
* multiplication 3 left to right
/ division
% modulus
+ addition 4 left to right
- subtraction
<< left shift 5 left to right
>> right shift
<= less than equal to 6 left to right
>= greater than equal to
< less than
> greater than
== equal to 7 left to right
!= not equal to
& bitwise AND 8 left to right
^ bitwise XOR 9 left to right
| bitwise OR 10 left to right
&& logical AND 11
|| logical OR 12
?: conditional operator 13
=, *=, /=, %= assignment operator 14 right to left
&=, ^=, <<=
>>=
, comma operator 15
Library Functions
Trigonometric Functions
asin(x)
acos(x)
atan(x)
atan2(x,y) Arc tangent of x/y
sin(x)
cos(x)
tan(x)
Hyperbolic Functions
sinh(x)
cosh(x)
tanh(x)
Other Functions
ceil(x) x rounded up to next integer
floor(x) x rounded down to nearest integer
exp(x) e to the power of x
fabs(x) absolute value of x
fmod(x) reminder of x/y
log(x) log of x where x>0
log10(x) log of x with base 10 where x>0
pow(x,y) x to the power of y (xy)
sqrt(x) square root of x (x>=0)
Note : 1. x and y should be float/double
3. In trigonometric & hyperbolic function x and y are in radians
Input statement
The data can be entered in to the computer through standard input device by
means of scanf function.
Syntax :
Scanf(“ control string”, &arg1, &arg2, ……….. &argn);
Where control string specifies the field format in which data is to be entered and
&arg1, &arg2, …… &argn specify the address of the location where data is
stored.
Control String
%c read a single character
%d read decimal integer
%e read floating point (real) value in exponent
%f read floating point (real) value
%g read floating point (real) value
%i read decimal, octal or hexadecimal integer
%o read octal integer
%s read a string
%u read unsigned decimal integer
%x read hexadecimal integer
Formatted output statement
The data can be display on to the standard output device by means of printf
function.
printf(“ control string”, arg1, arg2, ……….. argn);
Where control string specifies the field format in which data is to be displayed
and arg1, arg2, …… argn specify the the value to be displayed on output
device.
In printf function the data can be displayed in formatted form with
following flag along with control string.
Example of printf statement with integer data
Example of printf statement with real/float/double data
Commonly uses flag in printf statement
SINGLE CHARACTER INPUT - The getchar FUNCTION
Single characters can be entered into the computer using the C library function getchar.
The getchar function is a part of the standard C I/O library. It returns a single character
from a standard input device (typically a keyboard). The function does not require any
arguments, though a pair of empty parentheses must follow the word getchar.
Syntax
getchar( );
EXAMPLE : A C program contains the following statements.
char c;
.....
c = getchar();
The first statement declares that c is a character-type variable. The second statement causes a single
character to be entered from the standard input device (usually a keyboard) and then assigned to c.
SINGLE CHARACTER OUTPUT -THE putchar FUNCTION
Single characters can be displayed (Le, written out of the computer) using the C library function putchar.
This function is complementary to the character input function getchar.
Syntax
putchar ( character variable);
EXAMPLE : A C program contains the following statements.
char flag;
.....
putchar(flag);
Example of using getch and putch function
#include<ctype.h>
main()
{
char flag;
c=getch();
putch(flag);
}
This program will read single character from keyboard through the function getch() ,
stored the single character in character variable flag and print the value of flag on
output device (Monitor) using putch(flag) function.
Type Casting OR Casting a value
The type of operand can be converted to any other data type by using type casting.
Syntax :
(type)expression;
Where type is any standard data type and expression may be constant or variable or an expression
Example :
float x=7.5,y; int a=15,b=6; float z;
y=(int)x; z=float(a)/float(b);
Then the value of y is 7.0 Then the value of z is 2.5
Notice that there are eight octal digits and 16 hexadecimal digits.
The octal digits range from 0 to 7;
The hexadecimal digits range from 0 to 9, and then A to F.
Each octal digit is equivalent to three binary digits (3 bits), and each
hexadecimal digit is equivalent to four binary digits (4 bits).
For example, the bit pattern 10110111 can be represented in hexadecimal as 87
(1011 0111)
Similarly, this same bit pattern (10110111) can be represented in octal as 267
(10 110 111)