Basics of C
Basics of C
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.
Documentation Section:
It consist of a set of comment lines
The comment lines begins with /* and ends with */ or a single
set of // in the beginning of the line
These lines are not executable
Comments are very helpful in identifying the program features.
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
To
ken
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