C Language Notes
C Language Notes
Header files
The main() function
Delimeters
Program body
Header Files:
A header file is added by using pre-processor directive “include” (, if the function/object defined in it is to
be used in the program). It contains definitions of library functions/objects. Some commonly used header
files are; stdio.h, conio.h, math.h etc
Delimeters:
The delimeters or curly braces ( { } ) indicates starting and ending limit of a program.
Body of Program:
The statements which are written between the delimeters under the main() function are called body of
program. It consists of library functions, structures, or statements to make a program.
Structure of C Program:
#include<conio.h>
#include<stdio.h> Header files
Statement ;
Statement;
:
:
}
Q: What is IDE of C language?
IDE:
The IDE (Integrated Development Environment) is a screen that contains windows, menubar, statusline,
and quick reference keys. The IDE is used to develop, run, test and debug a program. The program listing,
error messages and other information are displayed in separate windows in the IDE.
Escape Sequence
Escape sequence is a pair of characters to control the way the cursor moves on the screen. In C, all escape
sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape
character"); the remaining characters determine the interpretation of the escape sequence.
Escape
Meaning Description
Sequence
Moves the cursor at the beginning of the
\n New line
next line
\t Tab Moves the cursor to the next tab stop
\a Alert Produces a beep
Moves the active position to previous
\b Backspace
position on the current line
Moves the active position to the initial
\r Carriage return
position of the next paragraph
\0 End of string Null
\\ Backslash Presents with a backslash
\’ Single Quote Presents with a single quote
\” Double quote Presents with a double quote
\? Question Mark Presents with a question mark
Format Specifier
Format specifier is used tp print a variable in the form in which it is declared. It starts with a % sign which
is followed by a letter that represents the data type of the variable.
Specifier Description
%d Integer format specifier
%f Float format specifier
%c Character format specifier
%s String format specifier
%u Unsigned Integer format specifier
%ld Long Int format specifier
%lu Long Unsigned format specifier
%o Octal number format specifier
%x Hexadecimal number format specifier
Q. What is meant by case sensitive language?
Case Sensitive Language
Case sensitive means whether the letters are capital or not. C is a case sensitive
language. It means that predefined words(keywords) must be written in the same case as they were
defined. Eg:- for,while,int,float,if etc. If we define predefined words in another case. The compiler will not
execute them. It will show an error.
Q. What is preprocessor directive? Explain any two preprocessor directives in C with examples.
Preprocessor Directive
Preprocessor directives are lines included in a program that begin with the character #, which make them
different from a typical source code text. They are called-up by the compiler to process some programs
before compilation.
A preprocessor directive is usually placed in the top of the source code in a separate line beginning with
the character "#", followed by directive name and an optional white space before and after it. A
preprocessor directive statement must not end with a semicolon (;). Preprocessor directives can be
defined in source code.
The following are some of the preprocessor directives that you can be used in source code:
#include, #define, #undef, #if, #ifdef, #ifndef, #error etc.
1. #include
The #include preprocessor directive is used to paste code of given file into current file. It is used include
system-defined and user-defined header files. If included file is not found, compiler renders error.
Syntax:
#include< file_name >
where file_name is the name of file to be included. The ‘<‘ and ‘>’ brackets tells the compiler to look for
the file in standard directory.
Example :
#include<stdio.h>
Void main (void)
{
printf(“This is an example of #include directive”);
return 0;
}
2. #define
The #define is used to define a macro. A macro is a segment of code which is replaced by the value of
macro. These macro definitions allow constant values to be declared for use throughout your code.
Syntax:
#define NAME value
Where,
NAME – it is macro name and written in capital letters.
value – it is value of the constant.
Example :
#include<stdio.h>
#define PI 3.14
void main (void)
{
Statement Terinator
A statement in C program in C language is terminated with a semicolon ( ; ). Semicolon terminates the line
not the carriage return you type afterwards. C language pays no attention to carriage return in a program
listing.
What is C language?
C Language:
C is a general purpose programming language developed at AT&T’s Bell Laboratories of
USA in 1972. It was designed and written by Dennis Ritchie. In the late seventies C began to
replace the more familiar languages of that like PL/1, ALGOL etc.
It was originally developed for writing system software but is now considered a general purpose
language. Today C is widely used for writing applications including word processing,
spreadsheets, games, robotics and graphics programs.
Object Code:
Object code is produced when a compiler translates source code into recognizable and executable
machine code. It is a set of instruction codes that is understood by a computer. Object code is
usually produced by a compiler that reads some higher level computer language source
instructions and translates them into equivalent machine language instructions.
c = 15.3;
d = c / 3;
printf("%7.2f\n",d);
printf("%07.2f\n",d);
}
DATA TYPE
The variable types specifies the type of data that can be stored in it. Each variable is declared by
its data type.
C supports four basic data types;
1. int
2. char
3. float
4. double
These four basic data types can be modified using C’s type modifiers to more precisely fit the
specific need. The type modifier are;
i. short
ii. long
iii. signed
iv. unsigned
1- int Data Type:
The int is a data type used to represent integer values which include the whole
numbers and their negative values or zero. The set of integers can be represented as -∞ .... -
5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5.....∞
The number of bytes reserved by the compiler for the integer type modifiers are given below.
Data Type Range Bytes width Format
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
long signed int -2147483648 to +2147483647 4 %ld
long unsigned int 0 to 4294967295 4 %lu
The number of bytes reserved by the compiler for the float data types are given below.
Data Type Range Bytes width Format
-38 38
Float 3.4x10 to 3.4x10 4 %f
double 1.7x10-308 to 1.7x10308 8 %lf
long double 3.4x10-4932 to 1.1x104932 10 %Lf
The number of bytes reserved by the compiler for the float data types are given below.
Data Type Range Bytes width Format
signed char -128 to 127 1 %c
unsigned char 0 to 255 1 %c
OPERATORS
An operator is a symbol that tells the compiler to perform a certain mathematical or logical
operation . Operators are used in programs to manipulate a value or a variable.
Classification of Operators
Operators fall into three categories.
C programming has wide range of operators to perform various operations. It has as many as 45
different operators. Some commonly used operators are:
1. Arithematic operators
2. Relational operators
3. Assignment operators
4. Logical operators
5. Increment and Decrement operators
1- Arithmetic Operators:
The operators which are used to perform mathematical operations on operands (values or
variables) are known as ‘Arithmetic operator’. All the arithmetic operators are binary operator,
i.e: performs operations on two operands.
2- Relational Operators:
The relational operator are used to compare two numeric operands. The operands can be
variables, constants or expressions that ultimately get evaluated to a numeric value. All the
relational operatpors are binary operators hence require two operands..
The relational operator returns zero when the relation it compared is false and 1 if it is true.
3- Assignment Operators:
The basic assignment operator is ‘ =’. It is used to assign value of an expression to an
identifier (variable). It has the general form
identifier = expression
Where identifier generally represents a variable, and expression represents a constant, a
variable or a more more complex expression.
4- Logical Operators:
The logical operators are also called Boolean operators to honour George Boole, Irish
mathematician. Logical operators combine the result of one or more expressions. After testing the
conditions, they return either true (1) or false (0) as net result.
Logical operators are unary or binary operators. The operands may be constants,
variables, or expressions. The basic logical operators are:
i. First increment / decrement the value of the variable and then take the newer value for
processing (Prefix).
ii. First take the value of the variable for processing and then only increase / decrease the
variable (Postfix).
Control Structure:
A statement that is used to control the flow of execution in a program is called a
control structure. It alters the flow of execution of the program.
There are three types of control structures available in C.