Type of Languages:
High Level Language: High-level language is any programming language that
enables development of a program in much simpler programming context and is
generally independent of the computer’s hardware architecture.
In the late seventies C began to replace the more familiar languages of that time
like PL/I, ALGOL, etc.
1. Major parts of popular operating systems like Windows, UNIX, Linux is still
written in C.
2. Device driver programs are exclusively written in C.
3. Mobile devices, tablets, common consumer devices like microwave oven,
washing machines and digital cameras, these smart devices comes from a
microprocessor, an operating system and a program embedded in this
devices.
4. Many popular gaming frameworks (like DirectX) have been built using C
language.
5. To interact with the hardware devices C only the choice.
Fundamentals of C:
C is language consist of characters, keywords, Instructions and Programs.
C Character Set:
The alphabets, numbers and special symbols when properly combined form
constants, variables and keywords.
C Identifiers:
double accountBalance;
C Keywords:
1. Keywords are predefined, reserved words used in programming that have
special meanings to the compiler.
2. Keywords are part of the syntax and they cannot be used as an identifier.
3. As C is a case sensitive language, all the keywords are written in lower case
letters.
auto,double, int, struct, break, else, long, switch, case, enum, register,
typedef, char, extern, return, union, continue, for, signed, void, do, if,
static, while, default, goto, sizeof, volatile, const, float, short, unsigned
C Variables:
1. Variables are named memory locations used to hold some data of particular
type.
2. The value of a variable may change during the program execution at
different time, but it can contain a single value at a time.
3. Naming a variable will follow the rules of identifies
4. Declaration of a variable must be done before they are used in the C
Program. If you declare a variable in C, that means you are asking to the
OS for reserve a piece of memory with that variable name.
Declaration of a Variable:
1. It gives the information about the variable to the compiler.
2. What type of data will be kept in variable is specified in the variable
declaration.
3. When we declare a variable some space is allocation in in the main
memory(RAM) depending upon the type of datatypes
Syntax: datatype variable_name;
Example: int num;
Initialization of Variable:
1. Assignment of some value to the variable is known as initialization.
2. Before the assignment the variable contains one default value
Syntax: data_type variable_name;
variable_name = value;
Example: int x;
x = 50;
Types of C Variable:
1. Local variable
2. Global variable
3. Environment variable
Local variable in c:
Global variable in c:
Types of C Constants:
Note: Truly speaking the range of an Integer constant depends upon the
compiler.
A. The mantissa part and the exponential part should be separated by a letter
e.
B. The mantissa part may have a positive or negative sign.
C. Default sign of mantissa part is positive.
D. The exponent must have at least one digit, which must be a positive or
negative integer. Default sign is positive.
E. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex.: +3.2e-5
Ex.: 'A'
Escape Sequences:
Escape Sequences are necessary to use characters which cannot be typed or has
special meaning in C programming. For example: newline (enter), tab, question
mark etc. In order to use these characters, escape sequence is used.
Escape Sequences:
Escape Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
Structure of C program:
Ex: /* */, //
2. Link Section: This Section is the core part of the programme in which
compiler links the inbuilt function from the system library.
Declaration part: In which all the variables and user defined functions (udf)
are declared.
6. Subprogram Section: It describes all the user defined functions (UDF) that
are called in main section.
Let us now write down our first C program for simply calculate simple interest.
main( )
int p, n ;
float r, si ;
p = 1000, n = 3, r = 8.5 ;
printf ( "%f" , si ) ;
C operators:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Conditional operators
7. Special operators
Arithmetic operators:
Relational operators:
Operator Description
== Check if two operand are equal
!= Check if two operand are not equal.
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand
Logical operators:
C provides three logical operators when we test more than one condition to make
decisions.
Bitwise operators:
Bitwise operators perform manipulations of data at bit level. These operators also
perform shifting of bits from right to left. Bitwise operators are not applied to float
or double.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift
Now let’s see truth table for bitwise &, | and ^
The bitwise shift operator, shifts the bit value. The left operand specifies the
value to be shifted and the right operand specifies the number of positions that
the bits in the value have to be shifted. Both operands have the same precedence.
Example: a = 0001000
b=2
a << b = 0100000
a >> b = 0000010
Assignment Operators:
? : Operator
It is actually the if condition that we use in C language decision making, but using
conditional operator, we turn the if condition statement into a short and simple
operator.
Explanation:
Special operator:
Data Types:
Data types simply refers to the type and size of data associated with variables and
functions.
Primary (Built-in) Data Types: void, int, char, double and float.
Floating point type: Floating types are used to store real numbers.
void type:
1. The data type void actually refers to an object that does not have a value of
any type.
2. Void is an empty data type associated with functions and pointers
3. It is also known as generic data type
4. Its size is unknown, that’s why it is not used with variables
5. When void is used before a function it means that, the function will not
return anything to the calling function.
6. void main() : It says that main function will not return anything to the
operating system
7. If void is specified in the function argument, it explicitly says that the
function will not take any arguments.
Control Statements:
1. if statement
2. switch statement
3. conditional operator statement (? : operator)
4. goto statement
if statement:
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Using else if statement
switch statement:
switch statement is used when you have multiple possibilities for the if statement.
Syntax: switch(variable) {
case 1:
break;
case n:
break;
default:
break;
Points to Remember:
1. We don't use those expressions to evaluate switch case, which may return
floating point values or strings or characters.
2. break statements are used to exit the switch block. It isn't necessary to use
break after each block, but if you do not use it, then all the consecutive
blocks of code will get executed after the matching block.
Ex: int i = 1;
switch(i)
{
case 1:
printf("A"); // No break
case 2:
printf("B"); // No break
case 3:
printf("C");
break;
o/p: A B C
here the output was supposed to be only A because only the first case matches,
but as there is no break statement after that block, the next blocks are executed
too, until it a break statement in encountered or the execution reaches the end of
the switch block.
3. default case is executed when none of the mentioned case matches the
switch expression. The default case can be placed anywhere in the switch
case. Even if we don't include the default case, switch statement works.
4. Nesting of switch statements are allowed, which means you can have switch
statements inside another switch block. However, nested switch statements
should be avoided as it makes the program more complex and less
readable.
Note:
Goto:
... .. ...
... .. ...
... .. ...
label:
statement;
Here the label is an identifier. When goto statement is encountered, control of the
program jumps to label: and starts executing the code.
Arrays:
The data type of the elements may be any valid data type like char, int or float.
The elements of array have same variable name but each element has a different
index number (subscript).
A one-dimensional array has one subscript, two dimensional array has two
subscripts and so on.
The one-dimensional arrays are known as vectors and two-dimensional arrays are
known as matrices.
Arrays should also be declared before they are used in the program.
The size and type of arrays cannot be changed after its declaration.