BCA_Sem1_C_Unit-I
BCA_Sem1_C_Unit-I
Unit I
Introduction to Programming in C
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.
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.
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
Compiler Interpreter
It consumes more time to execute machine It consumes less time to execute machine code.
code.
Both syntactic and semantic errors can be Only syntactic errors are checked.
checked.
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.
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
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.
I. Documentation 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>
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.
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.
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.
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.
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.
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
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
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;
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
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.
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.
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.
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.
Operator Example
== (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
6. Special Operators
Below are some of the special operators that the C programming language offers.
Operators Description
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) = 7, i.e., 0111
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;
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);
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:
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();