Basics of C
Basics of C
unit 2
C Language Topics-unit 1 and 2
UNIT 1 BITS AND BYTES IN COMPUTING 9 Hrs. Computers : Hardware – Software –
Processor – Memory – I/O devices – Interface – Programming Languages – Evolution from
COBOL, FORTRAN to C, Python – Need. Algorithms: Role in problem solving – Analysis –
Design – Flowcharts: Role in problem solving – Symbols – Design – Pseudocode: Role in
problem solving – Design - Program: Role in problem solving – Design.
Practice Problems: 1. Describe a simple real world problem in your domain/department
and describe it in the form of problem statement, input, output and provide its solution in
terms of algorithm, flowchart, pseudo code and program.
UNIT 2 C: MATH BEHIND CODING 9 Hrs. C: Structure of program – Character set – Tokens
– Keywords – Identifiers – Constants – Variables – Datatypes – Strings – Operators and its
types – Functions – Header Files. Algorithmic Strategies: Iteration and Recursion –
Efficiency – Role of Time and Space consumption while building an algorithm –
Complexities
Practice Problems: 1. Describe a simple real world problem in your domain/department
and provide a computing and non-computing solution for the same. Calculate the time
and space consumed in both solutions. Compare and contrast the pros and cons in both
solutions. 2. Write an algorithm, flowchart, pseudo code followed by a simple C code to do
find the Factorial and Fibonacci series using both iteration and recursion.
Algorithms & Flowcharts
Algorithms
The sequence of steps to be performed in order to solve
a problem by the computer is known as an Algorithm.
The Algorithm often refers to the logic of a program.
Algorithms can be expressed in many different
notations, including natural languages, pseudocode,
flowcharts and programming languages.
Flow Chart
Flowchart is a graphical or symbolic representation of
an algorithm.
It is the diagrammatic representation of the step-by-
step solution to a given problem.
Let us take a small problem and see how can we write
an algorithm using natural language and draw
flowchart for the same.
Illustration
Consider the problem of finding the largest number in
a given set of three numbers.
Algorithm
1. Get three numbers from the user
2. Compare the first two numbers
3. The larger of the first two numbers is compared
with the third number
4. The larger number obtained as a result of the
above execution is the largest number
5. Print the that number as output
Overview of C
C is a procedural programming
language as well as a general-purpose
programming language that was
developed by Dennis Ritchie at AT&T’s
Bell laboratories in 1972.
It is an amazing and simple
language that helps us to develop
complex software applications with
ease.
It is considered as the mother of all
languages.
C is a high-level programming
language that provides support to a
low-level programming language as
A brief history
C is a programming language developed at “AT & T’s Bell Laboratories” of USA in
1972.
It was written by Dennis Ritchie.
The programming language C was first given by Kernighan and Ritchie, in a classic
book called “The C Programming Language, 1st edition”.
For several years the book “The C Programming Language, 1st edition” was the
standard on the C programming.
Preprocessor Section:
It is used to link system library files, for defining the macros and
for defining the conditional inclusion
Eg: #include, #include, #define MAX 100, etc.,
int main()
int main(void)
Int main(int argc, char*argv[])
void main()
void main(void)
void main(int argc, char * argv[])
Example Program for structure of C
#include<stdio.h>
#include<conio.h>
void main()
{
int p,n;
float si,r;
clrscr();
printf(“Enter principle amount, year and rate of
interest”); scanf(“%d %d %f”,&p,&n,&r);
si=p*n*r/100;
printf(“The simple interest is : %f”,si);
getch();
}
Tokens
• TOKEN is the smallest unit in a 'C' program. It is each
and every word and punctuation that you come across in
your C program. The compiler breaks a program into the
smallest possible units (tokens) and proceeds to the
various stages of the compilation. A token is divided into
six different types,
• Keywords
• Identifiers
• Strings
• Constants
• Special Characters
• Operators
Token
Keywords
The constants refer to fixed values that the program may not change or
modify during its execution. Constants can be of any of the basic data
types like an integer constant, a floating constant and a character constant.
There is also a special type of constant called enumeration constant.
Eg:
Integer Constants- 45, 215u
Floating Constants- 3.14, 4513E-5L
Character Constants- \t, \n
Strings
A string in C is actually a one-dimensional array of characters which is
terminated by a null character '\0'.
Eg:
char str = {‘S’, ’A’, ’T’, ’H’, ’Y’, ’A’, ’B’, ’A’, ’M’, ’A’}
Special Symbols
The symbols other than alphabets, digits and white spaces for example - []
() {} , ; : * … = # are the special symbols.
1.Special characters
•Special characters in 'C' are shown in the given table,
Types of Operators
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Size of() Operators
Arithmetic Operators
Syntax:
Condition? Exp1:Exp2
Example:
X=(a>b)?a:b
The ‘?:’ operator acts as ternary operator. It first evaluate the
condition, if it is true then exp1 is evaluated, if condition is false
then exp2 is evaluated. The drawback of Assignment operator is
that after the ? or : only one statement can occur.
Bitwise Operators
Bitwise Operators are used for manipulation of data at bit level. It
operates on integer only.
Special operators:
sizeof () operator:
1. Sizeof operator is used to calculate the size of data type or variables.
2. Sizeof operator will return the size in integer format.
3. Sizeof operator syntax looks more like a function but it is considered as an operator
in c programming
printf("%d", sizeof(10));
printf("%d", sizeof('A'));
printf("%d", sizeof(10.10));
return 0;
}
Output :
214
In this example we have passed the constant value to a
sizeof operator. In this case sizeof will print the size required
by variable used to store the passed value.
Example of Sizeof Data Type
#include<stdio.h>
int main()
{
printf("%d", sizeof(int));
printf("%d", sizeof(char));
printf("%d", sizeof(float));
return 0;
}
Output :
214
In this case we have directly passed an data type to an sizeof.
Example of Nested sizeof operator
#include<stdio.h>
int main()
{
int num = 10;
printf("%d", sizeof(sizeof(num)));
return 0;
}
Output:
2
We can use nested size of in c programming. Inner size of
will be executed in normal fashion and the result of inner size
of will be passed as input to outer size of operator.
Innermost Size of operator will evaluate size of Variable
“num” i.e 2 bytes Outer Size of will evaluate Size of constant
“2” .i.e 2 bytes
In the C Programming language, data types refer to a broad system used
for declaring variables or functions of different types.
Integers are whole numbers with a range of values, range of values are
machine dependent. Generally an integer occupies 2 bytes memory
space and its value range limited to -32768 to +32767.
CHAR DATA TYPE
As there are singed and unsigned int (either short or long), in the
same way there are signed and unsigned chars; both occupy 1
byte each, but having different ranges.
The double is same as float but with longer precision and takes
double space (8 bytes) than float.
The void data type is usually used with function to specify its type.
C program we declared "main ()" as void type because it does not
return any value.
Array: An array in C language is a collection of similar data-
type, means an array can hold value of a particular data type
for which it has been declared. Arrays can be created from any
of the C data-types int.
integer roll_no;
STRUCTURE DATA TYPE