Unit - I To C Programming, Operators & Expressions To C Programming
Unit - I To C Programming, Operators & Expressions To C Programming
C Language features:
Simple
Portability
Powerful
Platform dependent
Structure oriented
Case sensitive
Modularity
Middle level language
Efficient use of pointers
The C Character Set:
A character denotes any alphabet, digit or special symbol used to represent PROGRAM.
EX:
Alphabets: A-Z & a-z.
Digits: 0,1,2,………… 9
structure of a c program:
Documentation Section:
The documentation section contains a set of comment including the name of the program
other necessary details.
Comments are ignored by compiler and are used to provide documentation to people who
read that code.
Comments are be giving in C programming in two different ways
Definition Section
The definition section defines all symbolic constants
A symbolic constant is a constant value given to a name which can't be changed in program
Global Declaration
In global declaration section, global variables and user defined functions are declared
Declaration Part
Executable Part
Declaration Part : All the variables that are later used in the executable part are declared in this
part.
Executable Part: This part contains the statements that are to be executed by the compiler.
Subprogram Section:
The subprogram section contains all the user defined functions that are used to perform a
specific task
These user defined functions are called in the main() function
C TOKENS
An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e. letters & digits)
and underscore( _ ) symbol.
Keywords:
Keywords are special words in C programming which have their own predefined meaning
Data types specify how we enter data into our programs and what type of data we enter.
C language has some predefined set of data types to handle various kinds of data that we
use in our program.
These data types have different storage capacities.
float 4 %f
double 8 %lf
Constants:
Constant in C means the content whose value does not change at the time of execution of a
program
. constants are also called as literals
There are four basic types of constants in C
Integer constants,
Floating-point constants,
Character constants
String constants
A decimal integer constant can consist of any combination of digits taken from the set 0
through 9
Ex: 0 1 743 5280 32767 9999
An octal integer constant:
An octal integer constant can consist of any combination of digits taken from the set 0
through 7.
Ex: 0 01 0743 077777
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
Floating-point Constants:
A floating-point constant is a base- 10 number that contains either a decimal point or an
exponent (or both).
While representing decimal form, you must include the decimal point, the exponent, or both;
and while representing exponential form, you must include the integer part, the fractional
part, or both
The signed exponent is introduced by e or E.the part before e called as mantissa and the
part after called as exponent.
String Constants: String literals or constants are enclosed in double quotes "".
Variables:
When we want to store any information, we store it in an address of the computer.
Instead of remembering the complex address where we have stored our information, we
name that address.
The naming of an address is known as variable.
Variable is the name of memory location.
we can change value of a variable during execution of a program
A programmer can choose a meaningful variable name. Example : average, height, age,
total etc.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum; //variable declaration
a=10; //variable definition
b=20; //variable definition
sum=a+b;
printf("Sum is %d",sum);
getch();
} output: 30
Statements:
In c language every statements ends with semicolon
A statement causes the computer to carry out some action
There are three different classes of statements in C
Expression statements,
Compound statements
Control statements
Arrays:
An array is an identifier that refers to a collection of data items that all have the same name.
The data items must all be of the same type.
Syntax: : Datatype arrayname[size];
Ex:
Int a[10]; // array holding the 10 integer type of elements.
Initialization:
type array-name[size] = { list of values };
int marks[4]={ 67, 87, 56, 77 }; //integer array initialization
float area[5]={ 23.4, 6.8, 5.5 }; //float array initialization
int marks[4]={ 67, 87, 56, 77, 59 }; //Compile time error
Expressions:
An expression is a combination of operators and operands
An expression represents a single data item, such as a number or a character
Ex:
Algebraic Expression C Expression
axb–c a*b–c
(m + n) (x + y) (m + n) * (x + y)
(ab / c) a*b/c
(x / y) + c x/y+c
Evaluation of Expressions :
Expressions are evaluated using an assignment statement of the form
Syntax: Variable = expression;
Variable is any valid C variable name
When the statement is encountered, the expression is evaluated first and then replaces the
previous value of the variable on the left hand side
All variables used in the expression must be assigned values before evaluation is attempted.
Output:
The value of a is = 16
The value of a is = 14
The value of a is = 6
OPERATORS
Arithmetic Operators :
C programming has more unary operators some of them are increment ++ and decrement --
to change the value of an operand (constant or variable) by 1.
Increment operators(++)
Decrement operators(--)
Increment operators(++)
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Operator Description Example
== Check if two operands are equal 5 = = 3 returns 0
!= Check if two operands are not equal. 5 > 3 returns 1
> Check if operand on the left is greater than operand on the 5 < 3 returns 0
right
< Check operand on the left is smaller than right operand 5 != 3 returns 1
>= check left operand is greater than or equal to right operand 5 >= 3 returns 1
<= Check if operand on left is smaller than or equal to right 5 <= 3 return 0
operand
if (a > b)
printf("a is greater than b\n");
else printf("a is less than or equal to b\n");
if (a >= b)
printf("a is greater than or equal to b\n");
else printf("a is lesser than b\n");
if (a < b)
printf("a is less than b\n");
else printf("a is greater than or equal to b\n");
if (a <= b)
printf("a is lesser than or equal to b\n");
else printf("a is greater than b\n");
if (a == b)
printf("a is equal to b\n");
else printf("a and b are not equal\n");
if (a != b)
printf("a is not equal to b\n");
else printf("a is equal b\n");
return 0;
}
Output: a is greater than b
a is greater than b
a is not equal to b
Logical operators:
:An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false
Logical operators are commonly used in decision making in C programming.
int main()
{
int a=10, b=4, c = 10, d = 20;
if (a>b || c= =d)
printf("a is greater than b OR c is equal to d\n");
else printf("Neither a is greater than b nor c is equal "
" to d\n");
if (!a)
printf("a is zero\n");
else printf("a is not zero");
return 0;
}
a is not zero
Bitwise Operators:
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift
Now lets see truth table for bitwise &, | and ^
a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4
Assignment operator:
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Syntax: L=R;
Ex: int a=20
Float f=33.38
L = ; // prints error i.e R value required
= R ; // prints error i.e L value required
Special Opearators:
Comma Operator
Comma operators are used to link related expressions together. For example:
Example: int i,j,k;
The sizeof operator
The sizeof is an unary operator which returns the size of data (constant, variables, array,
structure etc).
sizeof() operator is used in different way according to the operand type.
1 When operand is a Data Type.
When sizeof() is used with the data types such as int, float, char… etc it simply return amount
of memory is allocated to that data types.
Ex:
#include<stdio.h>
int main()
{
printf("%d\n",sizeof(char));
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(float));
printf("%d", sizeof(double));
return 0;
}
Output
1
2. When operand is an expression When sizeof() is used with the expression, it returns size of the
expression
Ex:
#include<stdio.h>
int main()
{
int a = 0;
double d = 10.21;
printf("%d", sizeof(a+d));
return 0;
}
Output:
8
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
This process is also called type casting and it is user defined. Here the user can type cast the result
to make it of a particular data type.
int main()
{
double x = 1.2;
return 0;
}
Output:
sum = 2
This is done to take advantage of certain features of type hierarchies or type representations.
It helps us to compute expressions containing variables of different data types.