0% found this document useful (0 votes)
2 views12 pages

C Language Notes

The document outlines the basic structure of a C program, including components such as header files, the main() function, delimiters, and the program body. It explains key concepts like IDE, keywords, escape sequences, format specifiers, and the importance of comments. Additionally, it covers data types, operators, and identifiers in C programming, providing examples and definitions for each topic.

Uploaded by

homehub017
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

C Language Notes

The document outlines the basic structure of a C program, including components such as header files, the main() function, delimiters, and the program body. It explains key concepts like IDE, keywords, escape sequences, format specifiers, and the importance of comments. Additionally, it covers data types, operators, and identifiers in C programming, providing examples and definitions for each topic.

Uploaded by

homehub017
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Structure of C Program

Every C program consists of the following main parts, which are;

 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

The main() Function:


The main() function indicated the beginning of a C program. The main() must be included in everry C
program. When a program is executed, the control directly goes to the main() function.

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

main() main() function

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.

Q: What are keywords? Give some examples.


Keywords:
Keywords are certain reserve words that have standard predefined meaning in C language. These
keywords can be used only for their intended purpose. They cannot be used as variable names.

Some examples of commonly used keywords are:


continue, break, signed, unsigned, float, int, char, long, short, void, do, while, for, switch, if, else, return
etc.

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)
{

int radius, area;


printf(“Enter the radius of the circle.. “);
scanf(“%d”, radius);
area = PI * (radius*radius);
printff(“Area of the circle is: %d”, area);
getch();
}

Q. What is statement terminator?

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.

Q. Define Source code and Object code. (2013)


Source Code:
Source code is the version of a computer program as it is originally written (i.e., typed into a
computer) by a human in a programming language. It is the fundamental component of a
computer program that is created by a programmer. It can be read and easily understood by a
human being.

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.

Q. What are comments? How do we use them in a C program?


Comment:
A comment is a programmer-readable explanation or annotation in the source code of
a C program. Comments are added with the purpose of making the source code easier for
humans to understand, and are generally ignored by compilers.

There are two ways to give comments:


1) Single-line comments
Single line comments starts with double slashes //.
for example:
// THIS IS A SINGLE LINE COMMENT
2) Multiple line comments
A multiple line comment starts with a slash asterisk /* and ends with a asterisk slash */
and can be used anywhere in a program. It can cover several lines within a C program.
for example:
/* THIS IS
MULTIPLE
LINE
COMMENT */

Q. Define Field width specifier?


Field-width Specifier
Field width specifiers are format specifiers used in C language for printing formatted output on the
screen.
Field width as the name suggests is the width or space which needs to be given for that particular
display line and it is specified using numbers.

For example, in the following C code,observe the outputs on the screen.


main()
{
int a,b;
float c,d;
Output of the source code :
a = 15; 7
b = a / 2; 7
printf("%d\n",b); 007
printf("%3d\n",b); 5.10
printf("%03d\n",b); 0005.10

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

2- float Data Type:


The float data type represents floating-point values, meaning any signed or unsigned
number having a decimal point. Examples of valid float values are; 2., -58.3442, +57.0, -394.

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

3- char Data Type:


The ‘char’ stands for characters. It is used to declare character type variables. A char
represents individual characters. In this data type alphabet, number, and special character/symbol
can be stored. Examples of char are; ‘e’, ‘P’, ‘$’, ‘7’.

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.

1. Unary operator: Those that require only one operand.


2. Binary operator: Those that require two operands.
3. Ternary operator: Those that require three operands.

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.

Operator Opearation Example


+ Addition a+b
- Subtraction a–b
* Multiplication a*b
/ Division a/b
% Modulus or remainder a%b

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..

Operator Opearation Example


< Less than a<b
> Greater than a>b
== Equal to a == b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b
!= Not equal to a != b

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.

There are number of assignment operators in C, some of them are;

Operator Opearation Example Explanation


+= Add assign a += b Same as a = a + b
–= Subtract assign a –= b Same as a = a – b
*= Multiply assign A *= b Same as a = a * b
/= Divide assign a /= b Same as a = a / b
%= Remainder assign a %= b Same as a = a % b

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:

Operator Opearation Example


&& Logical AND Return 1 if a=b && c=d; else 0
|| Logical OR Return 1 if a=b || c>d; else 0
! Logical NOT Return 1 if !a; else 0

5- Increment and Decrement Operators:


The increment and decrement operators are used to increase (add) or decrease (subtract)
the value of a variable by 1. They are unary operators since they operate on only one variable.

There are two types of increment / decrement operators, which 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).

Increment / Decrement operators can be summarized as:

Operator Example Operation Explanation


++ ++a Prefix increment Increment by 1 and then use the new
value for processing.
++ a ++ Postfix increment Use the current value for processing
and then increment by 1.
-- --a Prefix decrement Decrement by 1 and then use the new
value for processing.
-- a-- Postfix decrement Use the current value for processing
and then decrement by 1.

Q. What is the difference between arithmetic and arithmetic assignment operators?


Arithmetic Operators Arithmetic Assignment Operators

1- An arithmetic operators performs only 1- An arithmetic assignment operator


one operation. performs two operations.

2- Arithmetic assignment operators


2- Arithmetic operators have higher
have lower order of precedence than
order of precedence.
arithmetic operators.
Q. What is an identifier?
Identifiers:
Identifier is a name given to variables, functions, constants and arrays. The
following names are examples of valid identifiers.
a tot_num no1 PI pi _rate
Rules for Constructing Identifiers
The rules for constructing identifiers are as fowllows;
 An identifoer must start with an alphabet or (rarely) an underscore.
 An identifier can contain letters, digits or underscores.
 It cannot contain blanks, commas or any other special character.
 It cannot contain more than 31 characters.
 It must confirm to case sensitivity .

Q. What is the difference between source code and object code?

Source Code Object Code

1- Source code is in a high-level 1- Object code is in a low-level


language. language.

2- It is generated by a programmer. 2- It is generated by a computer.

3- It does not require a language


3- It requires a language translator.
translator
4- A programmer can easily create, 4- A programmer cannot create,
modify and debug a source code. modify and debug a source code.

SELECTION CONTROL STRUCTURES


Q1. What is control structure in C language?

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.

 Serial or sequential control structure


 Selection or conditional control structure
 Repetitive or iteration control structure

Selection control structure


The selection control structure is used to select a statement out of several to
execute on the basis of condition. Statement is executed when the condition is true and
ignored when it is false
C language has three selection control structures, which are:
i. if Selection Structure
ii. if –else Selection Structure
iii. switch Statement

i- if Selection Control Structure


This is the simplest form of the selection control statements. It
takes an expression/condition in parenthesis and a statement or block of statements.
If the expression/condition is true then the statement or block of statements gets
executed otherwise these statements are skipped and control transfers to the next
step of the program.
The syntax of if selection structure is:
if (condition)
statement;
The syntax indicates that “if the condition is true, then execute the following
statement”.

ii- if-else Selection Control Structure:


The second form of ‘if’ statement adds the keyword ‘else’ followed
by an alternative statement or a block of statements. The if-else selection structure
executes a statement when the condition is true and some different statement when
the condition is false.
The syntax of if-else selection structure is:
if (condition)
statement 1;
else
statement 2;
The syntax indicates that “if the condition is true, execute the statement 1; otherwise
execute the statement 2”. The statement 2 will only be executed if the condition is
false.

iii- switch Selection Control Structure:


The switch statement is much like a nested if .. else statement. It is
used to select one of the several alternatives. The switch statement is especially useful
when the selection is based on the value of a single variable (called controlling
variable) or a simple expression (called the controlling expression). The value of the
controlling variable may be int or char.
The syntax of the switch statement is:
switch (variable)
{
case constant 1:
statement(s);
break;
case constant 2:
statement(s);
Break;
case constant 3:
statement(s);
Break;
.
.
.
default:
statement(s);
}

Sample program for switch statement:


Void main (void)
{
clrscr();
int n;
printf(“Enter 1, 2 or 3.. );
scanf(“%d”, &n);
switch(n)
{
case 1:
printf(“You entered 1 \n”);
break;
case 2:
printf(“You enterd 2 \n”);
break;
case 3:
printf(“You enterd 3 \n”);
break;
default:
printf(“You did not enter 1,2 or 3 \n”);
}
Q2. Explain the use of nested if-else statement.
Nested if-else Statement:
When an if-else statement is present inside the body of another “if” or “else”
then this is called nested if-else. The nested if-else statements test for multiple
conditions by placing if-else statement inside if-else statement. When a condition is
evaluated as true, the corresponding statements are executed and the rest of the
structure is skipped. This structure is referred to as if-else-if ladder.
Example:
void main (void)
{
clrscr();
float per;
printf(“Enter percentage of Marks… “);
scanf(“%f”, &per);
if(per >= 80)
printf(“Grade A+ \n”);
else if(per >= 70)
printf(“Grade A \n”);
else if(per >= 60)
printf(“Grade B \n”);
else if(per >= 50)
printf(“Grade C \n”);
else if(per >= 40)
printf(“Grade D \n”);
else if(per >= 33)
printf(“PASS \n”);
else
printf(“FAIL \n”);
getch();
}

You might also like