0% found this document useful (0 votes)
41 views18 pages

BCA_Sem1_C_Unit-I

This document provides an introduction to C programming, covering its history, the differences between compilers and interpreters, algorithms, flowcharts, and the structure of C programs. It details the components of C programming including tokens, keywords, identifiers, constants, variables, data types, and operators. Additionally, it explains the characteristics of algorithms and provides examples of algorithms and flowcharts.
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)
41 views18 pages

BCA_Sem1_C_Unit-I

This document provides an introduction to C programming, covering its history, the differences between compilers and interpreters, algorithms, flowcharts, and the structure of C programs. It details the components of C programming including tokens, keywords, identifiers, constants, variables, data types, and operators. Additionally, it explains the characteristics of algorithms and provides examples of algorithms and flowcharts.
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
You are on page 1/ 18

C PROGRAMMING UNIT I

Unit I
Introduction to Programming in C

1.1 History of C Programming Language


1. C is one of the high level language.
2. C programming language developed in 1972 by Dennis Ritchie at Bell Laboratories of
AT & T (American Telephone & Telegram) in USA.
3. C was originally developed for UNIX operating system.
4. It was developed to overcome problem of previous language such as B, BPCL, etc.
5. UNIX operating system development was started in 1969 and its code was rewritten in
c in the year 1972.

1.2Compiler and Interpreter

A. Compiler
1. Compiler used to translate high level language code to machine code.
2. Compiler scans complete program as once and translate it as a whole into machine code.
3. Compiler has slow speed because it scans complete program as once and translate it as
a whole into machine code.
4. Compiler generates all errors at a time, hence debugging is difficult than interpreter.
5. Compiler generate intermediate code which required linking hence compiler required
more memory.
6. Compiler checks all kind of limits, ranges, errors etc.
7. Program language like C, C++ uses compiler.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 1


www.nandigramiit.org
C PROGRAMMING UNIT I

B. Interpreter
1. Interpreter used to translate high level language code to machine code.
2. Interpreter reads line by line program and translate it into machine code.
3. Therefor Interpreter is faster than Compiler.
4. Interpreter generates single error at a time, hence debugging is easy than compiler.
5. Interpreter will not generate intermediate code hence Interpreter required less memory.
6. Program language like Python, Ruby uses Interpreter.

 Difference between compiler and interpreter

Compiler Interpreter

A compiler translates the entire source code in An interpreter translates the entire source code
a single run. line by line.

It consumes less time to translate source code It consumes more time to translate source code
to machine code to machine code

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 2


www.nandigramiit.org
C PROGRAMMING UNIT I

Compiler Interpreter

It consumes more time to execute machine It consumes less time to execute machine code.
code.

It is more efficient. It is less efficient.

CPU utilization is more. CPU utilization is less.

Both syntactic and semantic errors can be Only syntactic errors are checked.
checked.

The compiler is larger. Interpreters are often smaller than compilers.

It is not flexible. It is flexible.

The localization of errors is difficult. The localization of error is easier than the
compiler.

The compiler is used by the language such as An interpreter is used by languages such as
C, C++. Java.

1.3 Algorithms
1. An algorithm is set of well-defined instructions to solve problem.
2. It takes set of inputs and produce desired output.
3. It is not a complete program or code.
4. The instruction in algorithm can be implemented in any language with the same output.

 Following are the characteristics of algorithm


1. Input: Algorithm has some input values.
2. Output: Algorithm produce one or more output.
3. Un-ambiguity: Instruction in algorithm should be simple and un-ambiguous.
4. Finiteness: Algorithm should contain limited number of instructions and should be
countable.
5. Effectiveness: Each instruction in algorithm affect overall process.
6. Language independent: Instruction in an algorithm can be implemented in any language
with same output.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 3


www.nandigramiit.org
C PROGRAMMING UNIT I

Write an algorithm to add two numbers entered by user

Step 1: Start
Step 2: Declare variable num1, num2 and sum.
Step 3: Read value num1 and num2
Step 4: Add num 1 and num2 and assign the result to sum
Sum  num1 + num2
Step 5: Display sum
Step 6: Stop

Write an algorithm to find largest number among three numbers.

Step 1: Start
Step 2: Declare variables a, b and c
Step 3: Read value of a, b and c
Step 4: if a > b and a > c
Display a is largest number
else if b > c
Display b is largest number
else
Display c is largest number
Step 5: Stop

1.4 Flowchart
1. Flowchart is a sequential, logical and stepwise diagrammatic representation of a
program.
2. Flowchart uses simple geometric shapes and arrows to show the flow of program.
3. Flowchart can have only one start and stop symbol.
4. General flow of process is from top to bottom or from left to right.
5. Arrows should not cross each other.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 4


www.nandigramiit.org
C PROGRAMMING UNIT I

1.5 Structure of C Programming

Structure of C programming language has six sections.

I. Documentation Section

1. In this section, information of program is written such as title of program, purpose of


program, author name, date etc.
2. Documentation section is never execute, it also called as comment section.
3. C provides two types of comments i.e. single line comment and multiline comment.
4. Single line comment starts with // (double slash symbol)
5. Multiline comment starts with /* and ends with */.

II. Header (Link) Section

1. In this section, we use header files such as stdio.h, conio.h, string.h etc.
2. Header files are used to link function used in c program.
3. These file are used using # include statement.
4. E.g #include <conio.h>

III. Definition Section

1. In definition section we define constant values using # define


2. E.f. #define PI 3.142

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 5


www.nandigramiit.org
C PROGRAMMING UNIT I

IV. Global Declaration section

1. The variable, which used in more than one function are called as global variable.
2. Global variable are declared in global declaration section i.e. outside of all functions.

V. Main Function Section

1. Every C has only one main function which starts with { and ends with }.
2. Main function has two sub sections.
A. Declaration part
i. In this part, variables are declared before used in program.
ii. Eg. int a,b,c;
B. Execution part
i. In this section, execution statements and function call statements are used.
ii. e.g. if(), printf(), scanf(). Clrscr(), etc.

VI. Sub Program Section

1. In this section, user can define function which is called as user defined function.
 The Character set
1. Every language has it’s own character set ex;- In English language there are 26
alphabets are used to create word, sentence, paragraph.
2. Similarly ‘C’ language has it’s own character sets i.e. ASCII character set.
3. In C language character set, 256 characters are available.
4. Using these characters we write the C program.
5. C language uses four types of characters.

i. Alphabets: Upper case 26 (A to Z) and lowercase 26 (a to z).


ii. Digits: 0 to 9 (10)
iii. Special characters: , . / ‘ “ ; : { } [ ] \ | ` ~ ! @ # $ % ^ & * ( ) _ - = + etc.
iv. White spaces: These are used in string constant. E.g. /n /t /b // /” /r etc.

1.6 Token
1. Token is smallest individual element of program.
2. For example, we cannot create sentence without word; similarly, we cannot create
statement without token.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 6


www.nandigramiit.org
C PROGRAMMING UNIT I

3. Therefore we can say that tokens are basic component or building blocks to create
program.

Classification of tokens in C
Tokens in C language can be divided into seven categories
1. Keywords 2. Identifiers 3. Strings 4. Operators 5. Constant
6. Special Characters

1.6.1 Keywords
1. Keywords are the words whose meaning has already been explained to the C compiler.
2. The keywords cannot be used as variable names.
3. There are 32 keywords used in C all keywords must be written in lower case.
4. List of keywords:

2. Identifiers
1. Identifiers are the names given to variables, functions, arrays constants, structures,
unions and labels of statements.
2. The rules for naming identifiers are as follows −
3. Identifier names are unique.
4. Cannot use a keyword as identifiers.
5. Identifier has to begin with a letter or underscore (_).
6. It should not contain white space.
7. Special characters are not allowed.
8. Identifiers can consist of only letters, digits, or underscore.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 7


www.nandigramiit.org
C PROGRAMMING UNIT I

9. Only 31 characters are significant.


10. They are case sensitive.

The following names are valid identifiers

X y12 sum_1 _tempera


Names area text_rate TABLE

3. String
Any group of characters defined between double quotation marks is a string constant.
Eg. “Nandigram Institute of Information Technology”.

4. Constants
1. Constants are fixed values that never change during execution of program.
2. The types of constant can be integer constant, floating point constant, character
constant, string constant, Boolean constant etc.

a) Integer constant
1. Any integer value using 0 to digits are integer constant.
2. ‘,’, ‘.’ Space ‘$’ or’ R’ sign are not allowed in integer constant e.g. 12,480 and $380
are illegal constants

b) Floating point constant


1. A floating-point literal has an integer part, a decimal point, a fractional part, and an
exponent part.
2. You can represent floating point literals either in decimal form or exponential form.
e.g. 3.14159 and 314159E-5 are legal constants.

c) Character Constants
1. Character Constants are enclosed in single quotes, e.g., 'x'
d) String constant
1. Any group of characters defined between double quotation marks is a string constant.
Eg. “Nandigram Institute of Information Technology”.
NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 8
www.nandigramiit.org
C PROGRAMMING UNIT I

e) #define preprocessor constant


1. We can define all basic type constants using #define preprocessor.
Eg. #define PI 3.142

5. Special characters
Some of the special characters that are used in C programming are as follows
1. Brackets[] −used for array element
2. Parentheses() − are used for function calls and function parameters.
3. Braces{} −indicates the start and end of a block of code
4. Comma (, ) −used to separate more than one variables or parameters in function.
5. Semicolon(;) − It is called as a statement terminator
6. Asterisk (*) − It is used to create a pointer variable.

1.6.2 Variables
1. Variables are containers for storing data values, like numbers and characters.
2. In C, we can define variables according to the data type such as int, char, float,
double, long etc.
3. Syntax:
type variableName = value; or type variableNaame;
4. Example:
int age=19;
float weight;

1.6.3 Data Types


1. Data types are used to define or declare the type of particular variable.
2. In C language there are three classes of data types.

1) Primary data type.


2) Derived data type.
3) User-defined data types.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 9


www.nandigramiit.org
C PROGRAMMING UNIT I

1. Primary data type


1. It is also known as basic or built in data type.
2. Primary data type is divided into 3 type.

a. Integer data type:


1. It stores only integer values and cannot store decimal point values.
2. Integer data type contains char, unsigned char, int, unsigned int, long int and unsigned
long int data types.
3. The range, size and format specifiers are shown in following table.

Size in Format
Data Type Range
Byte String
char -128 to 127 or -27 to 27-1 1 %c
unsigned char 0 to 255 or 0 to 28-1 1 %c
int -32768 to 32767 or -215 to 215-1 2 %i or %d
unsigned int 0 to 65535 or 0 to 216-1 2 %u
-2147483648 to 2147483647
long int 4 %ld
or -231 to 231-1
unsigned long int 0 to 4294967295 or 0 to 232-1 4 %lu

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 10


www.nandigramiit.org
C PROGRAMMING UNIT I

b. Floating – point data type


1. A floating-point data type stores the value which has an integer part, a decimal point, a
fractional part, and an exponent part.
2. Floating-point data type contains float, double and long double data types.
3. The range, size and format specifiers are shown in following table.

Size in Format
Data Type Range
Byte String
Float 3.4e-38 to 3.4e+38 4 %f
Double 1.7e-308 to 1.7e+308 8 %lf
Long double 3.4e-4932 to 1.1e+4932 10 %lf

c. Void Type
1. The Void type has no value.
2. This is usually used to specify the type of functions.
3. The type of a function is said to be void when it does not return any value to the calling
function.

2. Derived data type


1. Derived data type are derived from fundamental data type.
2. The derived data type are array, pointer and function.

3. User defined data types


1. The data types that are defined by the user are called as User defined data types.
2. User defined data types are structure and union.

1.6.4 Operators
1. An operator is a symbol that tells the compiler to perform mathematical or logical
operations.
2. C language providing eight types of operators.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 11


www.nandigramiit.org
C PROGRAMMING UNIT I

1. Logical Operators
Logical operator works on Boolean types of operands or conditions.
1. &&, || and ! are the logical operators.
2. Logical AND operator (&&) - If both the operands are true, then the condition becomes
true. E.g. if((a>b) && (a>c)), if a is greater than both b and c then it returns true
3. Logical OR Operator (||) - If any of the two operands is true, then the condition becomes
true.
E.g. if((a>b) || (a>c)), if a is greater than b or c then it returns true.
4. Logical NOT operator (!): Is unary operator. If the condition is true it returns false and
if the condition is false it returns true.
E.g. if(!(a>b)), if a is greater than b then it returns false else it returns true.
2. Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language.
Assume A=10 and B=20.

Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% It returns remainder after an integer division. B%A=0

3. Relational Operators
If we want to compare two values, then Relational operators are used.
C supports six Relational operators
Assume A=10 and B=20.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 12


www.nandigramiit.org
C PROGRAMMING UNIT I

Operator Example

== (A == B) is not true.

!= (A != B) is true.

> (A > B) is not true.

< (A < B) is true.

>= (A >= B) is not true.

<= (A <= B) is true

4. Assignment Operator
The assignment operators are used to assign values or result of the expression to
variables.
ex:-a=10;
5. Increment and Decrement operators
Prefix Increment and Decrement operators
In the prefix operators the values of variables is Increment and Decrement First and then
assigned to the expression

++a and - - a
a=3
y=++a
y=4
 Postfix Increment and Decrement operators
Postfix operators first assign the values to variables on left side and then
increment the operand
a=5;
y=a++;
y=6

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 13


www.nandigramiit.org
C PROGRAMMING UNIT I

6. Special Operators
Below are some of the special operators that the C programming language offers.

Operators Description

This is used to get the address of the variable.


& Example : &a will give address of a.

This is used as pointer to a variable.


Example : * a where, * is pointer to the variable
* a.

This gives the size of the variable.


Sizeof () Example : size of (char) will give us 1.

7. Bit-wise Operator
Bitwise operator works on bits and perform bit-by-bit operation.
Assume a=4 (0100) and b=3 (0011)

Operator Example

& (A & B) = 0, i.e., 0000

| (A | B) = 7, i.e., 0111

^ XOR (A ^ B) = 49, i.e., 0111

~ One;s compliments (~A ) = , i.e,. -0111101

<< A << 2 = 16 i.e., 1 0000

>> A >> 2 = 1 i.e., 0001

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 14


www.nandigramiit.org
C PROGRAMMING UNIT I

8. Conditional operators
This is only one ternary operator in C programming language.
Syntax:
Expirations ? expression2 : expression3
Example:
x=(a<b)? a:b;
it means that –

if (a>b)
x=a
else
x=b;

 Input and Output Functions


Input and Output Functions are used to read the data from the input devices and display the
results on the screen.
In C programming, the input/output functions are classified in to two types:
1. Formatted I/O Function and
2. Unformatted I/O Function

1.7 Formatted I/O Function:


1. Formatted Input and Output functions used to read and write data with specific
formatting.
2. These I/O functions supports all data types like int, float, char, and many more.
3. scanf() and printf() are commonly used formatted I/O functions

A. printf()
1. This function is used to display result on the screen.
2. It can be used to display any combination of numerical value or char or string value.
3. This function is defined in the stdio.h header file.
4. Syntax:
printf (“format string”, v1, v2, . . . , vn);
NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 15
www.nandigramiit.org
C PROGRAMMING UNIT I

5. For eg.
printf (“%f”, s);
printf (“\n sum=%6.2f”, s);
printf (“\n %d factorial is %d”, k, kfact);
B. scanf()
1. This function is used to receive input from keyboard.
2. It can be used to receive any combination of numerical value or char or string value.
3. This function is defined in the stdio.h header file.
4. Syntax:
scanf (“format string”, &v1, &v2, . . . , &vn);
5. For eg.
scanf (“%f”, &s);
scanf (“%c”,&ans);
scanf (“%d %d”,&k, &kfact);

//display the ascii value


#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
prinf(“Enter any integer value :: “);
scanf(“%d”,&y);
printf(“%c %d”,y,y);
getch();
}
1.7 Unformatted function: -
1. These functions are called unformatted I/O functions because it cannot use format
specifiers.
2. The unformatted input/output functions only work with the character data type.
3. In case values of other data types are passed to these functions, they are treated as the
character data.
NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 16
www.nandigramiit.org
C PROGRAMMING UNIT I

4. getch(), getche(), getchar(), putch(), putchar() and clrscr() are commonly used
unformatted functions provide by conio.h header file and gets() and puts() are the
unformatted functions provided by stdio.h header file.

a. getch()
1. getch() function reads a single character from the keyboard.
2. The character entered by user doesn’t display on the screen.
3. There is no need to press enter key from keyboard. As soon as any key pressed from
keyboard getch() read a single character.
4. getch() is also used for hold the screen.
5. Example:
ch=getch(); // where ch is a char variable.

b. getche()
1. getche() function reads a single character from the keyboard.
2. The character entered by user is displayed on the screen.
3. There is no need to press enter key from keyboard. As soon as any key pressed from
keyboard getch() read a single character.
4. Example:
ch=getche(); // where ch is a char variable.

c. getchar()
1. The getchar() function is used to read only a first single character from the keyboard
whether multiple characters is typed by the user and this function reads one character
at one time until and unless the enter key is pressed.
2. Example:
ch=getchar(); // where ch is a char variable.

d. gets()
1. gets() function reads a group of characters or strings from the keyboard.
2. These characters get stored in a character array.
3. This function allows us to write space-separated texts or strings.
4. Syntax:

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 17


www.nandigramiit.org
C PROGRAMMING UNIT I

gets(str); // where str is a character string variable.


5. Example :
char str[15];
gets(str);

e. puts()
1. puts() function is used to display a group of characters or strings which is already stored
in a character array.
2. Syntax:
puts(str); // where str is a string (array of characters)
3. Example :
char str[20]=" Hello World";
puts(str);

f. clrscr()
1. clrscr() function is used to clear the monitor screen.
2. It has the following syntax :
clrscr();

 Difference between scanf() and gets()


1. The main difference between these two functions is that scanf() stops reading characters
when it encounters a space, but gets() reads space as character too.
2. If you enter name as Shaikh Nisar using scanf() it will only read and store Shaikh and
will leave the part after space. But gets() function will read it completely.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 18


www.nandigramiit.org

You might also like