c notes 1.1
c notes 1.1
C is a middle level language (C is a high level language but also incorporates features
of low-level languages like manipulation of bits, bytes, words and addresses.)
1|Page
Applications of C: Because of its portability and efficiency, C is used to develop the
system as well as application software. Some important system programs include operating
systems, compilers, interpreters, assemblers, editors, linkers and loaders.
Being a structured language, it is useful in developing application programs like database
systems, word processors, spreadsheets, graphics packages, CAD/CAM applications, office
automation tools, scientific and engineering applications, animation and games.
Pre-processor directives
Global declarations;
Main ()
Declarations;
Statements;
Pre-processor directives: these statements begin with # symbol. They direct the C pre-
processor to include header files and symbolic constants into a C program.
2|Page
Global declarations: Variables or functions whose existence is known in the main ()
function and other user defined functions, are called the global variables or functions. Their
declarations should be made before main ().
Main (): This is the main function of every C program. Execution of a C program starts with
Main (). No C program is executed without the main () function. It should be written in
lowercase letters and should not be terminated with semicolon. It calls other library functions
and user defined functions. There must be one and only one main () function in any C
program.
A pair of flower brackets: The flower brackets indicate body of the program, which
includes instructions to perform the required task. { indicates beginning of the main ()
function and } indicates end of the main() function.
Declarations: It is the part where all the variables, arrays, functions etc., used in the C
program are declared and may be initialized with their basic data types.
Statements: These are instructions to perform certain operations like input, output,
assignment, arithmetic statements, control statements and other statements. They also include
comments. Comments are explanatory notes on some instructions. The statements to be
commented must be enclosed within /* and */. The comment lines are not compiled and
executed.
User created sub-programs or functions: These are sub-programs created by the user to
perform certain common functions. They may be written before or after main ().
Example:
#include<stdio.h>
#include<conio.h>
void main()
printf(“\n HELLO
WORLD”); getch();
Output:
HELLO WORLD
3|Page
C PROGRAMMING PRELIMINARIES
CHARACTER SET:
Lowercase letters a to z
Digits 0 to 9
IDENTIFIERS: These are names given to program elements like variables, arrays, functions
and structures. Basically identifiers are the sequence of alphabets and digits.
VARIABLES:
An identifier/ quantity that change its value during program execution are called
„variable‟. A variable is an object or element that may take on any value of a specified type.
Variables represent named storage locations, whose value can be manipulated during
program execution.
Rules for naming a variable – same as rules for forming an identifier name
4|Page
CONSTANTS: It refers to fixed values that do not change during the execution of the
program. C supports several types of constants.
1. Numeric Constants
a. Integer constant
b. Float (real) constant
2. Character constants
a. Single character constants
b. String constants
3. Symbolic constants
4. Backslash constants
1. Numeric Constants
a. Integer constants: All whole numbers are integer constants. They are
sequence of digits without decimal point
E.g.: Decimal constants : 134, 9000, 3567, -987
Octal constant : 06, 034, 0677, -07867
Hexadecimal constant : 0x20, 0xA, 0x1AB
b. Float (Real) constant: Numbers having decimal point are floating point
constants. They can contain both an integer part and a fractional part in the
number. They are stored differently from integers. They may be represented
either in decimal form or in the exponent form.
E.g.: Valid decimal constants: - 18.5, .18, 5. , -25.67
Valid exponent constants: - 5.3E-5, -6.79E3, 34e-8
Here the number before „E‟ / „e‟ is called mantissa and
mantissa can be either real or integer, the number after „E‟/‟e‟ is called
exponent and exponent should be an integer.
2. Character constants
a. Single character: It is a letter or any symbol enclosed in single quotes.
Character constants can be used in assignment statements or in symbolic
constant definitions.
E.g.: ch=‟h‟ ;
#define PERIOD „.‟
b. String constant: A sequence of characters enclosed between double
quotes is called string constant.
E.g.: “C programming is fun”
“Enter two numbers”
3. Symbolic constants
It is a name that substitutes a numeric, character or a string constant.
E.g.: #define PI 3.1415 (Here we substitute PI for numeric constant)
#define PLACE “MYSORE”
#define FOUND 1
#define NUM 023 /* indicates octal number */
#define VALUE 0x20 /*0x indicates hexadecimal */
5|Page
4. Backslash constant
They are also called „escape sequences‟ and are used in the output statements. A
backslash constant is a combination of two characters in which the first character is
always the backslash (\) and the second character can be any one of a, b, f, n, r, t, v.
Backslash constant Meaning
„\a‟ System alarm
„\b‟ Back space
„\f‟ Form feed
„\n‟ New line
„\r‟ Carriage return
„\t‟ Horizontal tab
„\v‟ Vertical tab
„\”‟ Double quote
„\‟‟ Single quote
„\0‟ Null character
„\\‟ Back slash character
KEYWORDS (Reserved words): These are predefined words that have special meaning
and these meanings cannot be changed. They can be used only for the purpose for which they
have been defined. All keywords must be written in lowercase letters.
Example:
do if while volatile
DATA TYPES:
Data type indicates the type of data a variable can have. Data type defines set of
values, which can be stored in the variable along with certain operations that may be
performed on those values.
6|Page
There are three classes of data types
int: Means that the variable is an integer. An integer needs two bytes of space. Range of
values that can be stored in two bytes is -32768 to +32767. The operations that can be
performed on an integer value are addition, subtraction, multiplication, division and
remainder.
char: Means that the variable is a character. A character needs one byte of storage space. If
unsigned, the range of values is 0 to 255. The possible operations are input and output.
float: Means that the variable is a real number. Four bytes or 32 bits are allocated in memory,
this includes 7 significant digits after the decimal point. The range of values is 3.4E-38 to
3.4E+38.
double: Means that that variable is a double precision float type. Eight bytes or 64 bits of
storage space and range of values is 1.7E-308 to 1.7E+308.
Void: the void type has no values. This is usually used to specify the type of functions. The
type of a function is said to be void when it does not return any value to the calling function.
7|Page
Assigning values to variables:
Method 1: values can be assigned to variables at the time of
declaration E.g.: int sum=0;
float val=3.14159;
char ans=‟y‟;
Method 2: values can be assigned using an assignment statement
E.g.: sum=0;
Val=3.14159;
Ans=‟y‟;
Example: C program to compute area and circumference of circle
#include<stdio.h>
#include<conio.h>
#define PI 3.1415
void main()
{
int r;
float a, c;
clrscr();
8|Page
INPUT-OUTPUT STATEMENTS
1. scanf() 5. gets()
2. printf() 6. puts()
3. getchar() 7. getch()
4. putchar() 8. getche()
They allow the input read from the keyboard or the output displayed on VDU to be
formatted as per our requirements i.e., where this output will appear on screen, how many
spaces between two values, number of decimal places etc. Can be controlled using formatted
I/O statements.
FORMATTED INPUT:
scanf() statement is used to input the numeric, character and string type of data.
Example:- scanf(“%d%d”,&a,&b);
scanf(“%c%d%f”,&ch,&a,&r);
9|Page
Control String Meaning
%c Read a single character
%d Read a decimal integer
%e or %f or %g Read a floating-point number
%h Read a short int
%I Read a decimal or hexadecimal or octal number
%o Read on octal number
%p Read a pointer
%s Read a string
%u Read an unsigned integer
%x Read a hexadecimal number
FORMATTED OUTPUT:
C provides the printf() function to display the data on the monitor.
10 | P a g e
11 | P a g e
Example program 1:
#include<stdio.h>
#include<conio.h>
void main()
{
int val=63;
clrscr();
printf(“\n val is %d”,val);
printf(“\n val is %2d”,val);
printf(“\n val is %4d”,val);
printf(“\n val is %6d”,val);
printf(“\n val is %-6d”,val);
getch();
}
Output:
Example Program 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int num=126;
clrscr();
printf(“ number is=%d\n”,num);
printf(“ octal equivalent of %d=%o\n”,num,num);
printf(“ hexadecimal equivalent of %d=%x\n”,num,num);
printf(“hexadecimal equivalent of %d=%X\n”,num,num);
getch();
}
Output:
UNFORMATTED INPUT:
These statements are primarily concerned with reading the character type data from
keyboard.
getchar() function : This function reads a single character from the standard input
device.
Syntax: ch_var=getchar();
12 | P a g e
gets() function: This function reads in everything you enter from the keyboard until the
ENTER key is pressed i.e., it reads a string of all printable ASCII characters.
Syntax: gets(string);
UNFORMATTED OUTPUT:
These statements are mainly concerned with displaying or printing the character type
data on the monitor.
Syntax: putchar(ch_var);
Syntax: puts(string);
Example 1:
#include<stdio.h>
#include<conio.h>
void main()
{
char letter;
clrscr();
letter=getchar();
putchar(letter);
getch();
}
Output:
Example 2:
#include<stdio.h>
#include<conio.h>
void main()
{
char mesg[20];
clrscr();
printf(“enter the message to be displayed\n”);
gets(mesg);
puts(mesg);
getch();
}
13 | P a g e
Output:
All data transfer operations in C are carried out using functions available in the
standard library. These functions are known as the standard I/O library. These operations can
be classified into two types.
i. Buffered Input/output
ii. Unbuffered Input/output
Buffered Input:
Buffer is a temporary storage in the memory. The characters entered at the keyboard
are stored in the buffer until the ENTER key is pressed and then made available to the
program as a block of characters.
Input-output operations take less time on a buffered system.
If any mistake is done while entering the data before ENTER key is pressed, can be corrected.
E.g.: scanf() and getchar() are buffered input function.
Unbuffered Input:
Here, the character which is entered at the keyboard is made available to the program
immediately. It is useful in some interactive programs and to read and write binary data.
Function Meaning
acos(x) Arc cosine of x
asin(x) Arc sine of x
atan(x) Arc tangent of x
atan2(x,y) Arc tangent of x/y
cos(x) Cosine of x
sin(x) Sine of x
tan(x) Tangent of x
cosh(x) Hyperbolic cosine of x
sinh(x) Hyperbolic sine of x
tanh(x) Hyperbolic tangent of x
ceil(x) X rounded up to the nearest integer
x
exp(x) E to the x power (e )
fabs() Absolute value of x
14 | P a g e
floor(x) X rounded down to the nearest integer
fmod() Remainder of x/y
log(x) Natural log of x, x>0
log 10(x) Base 10 log of x, x>0
y
pow(x,y) X to the power y (x )
sqrt(x) Square root of x, x>0
Example:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y,p,z;
clrscr();
printf("enter x value\n");
scanf("%d",&x);
printf(“enter power\n”);
scanf(“%d”,&p)
y=sqrt(x);
z=pow(x,p);
printf("\n %d square root is=%d",x,y);
printf("\n %d power of %d=%d",x,p,z);
getch();
}
Output:
15 | P a g e