ECE - C Unit-I
ECE - C Unit-I
EC-405 PROGRAMMING IN C
UNIT- I
C-Programming Basics
Course Contents: Structure of a C program - Character Set –keywords – Data types -Constants,
Variables – Arithmetic operators- evaluation of expression– Assignment statement –Nested assignment
statement – Compound assignment operators- Increment, Decrement operators- printf() and scanf()
functions – Operator precedence – Relational, Logical, Bitwsie logical operators
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph) Laboratories.
Generally, we use to call C as a middle level language. Because, the ‘C’ compiler combines the
capabilities of an assembly language with the features of a high-level language.
C is highly portable, that is, ‘C’ programs written on one computer can be run on another with
little (or) no modification.
‘C’ language is best for structured programming, where the user can think of a problem in
terms of function modules (or) blocks.It has the ability to extend itself.
A C program is divided into different sections. There are six main sections to a basic cprogram.
1. Documentation Section
The documentation section is the part of the program where the programmer
givesthe details of the program.
Programmer usually gives the name of the program, the details of the author
and other detailslike the time of coding and description. It gives anyone to read the code
and its overview.
2. Link Section
This part of the code is used to declare all the header files that will be used in the
program.
This leads to the compiler being told to link the header files to the system
libraries.
Example #include<stdio.h>
3. Definition Section
In this section where the global variables are declared. All the global variable
used are declared in this part.
The user-defined functions are also declared in this part of the code.
int a=7;
5. Main Function Section: Every C-programs need to have the main function. Each main
function contains 2parts.
2. Execution part
The declaration part is the part where all the variables are declared.
The execution part begins with the curly brackets { and ends with the curly closebracket }.
Both the declaration and execution part are inside the curly braces.
Example
int main()
{
int a=10;
printf(" %d", a);
return 0;
}
All the user-defined functions are defined in this section of the program. We can also say this
section as a user defined program section.
int main() This is the main function from where execution of any C program
begins.
/*_some_comments_*/ whatever is given inside the command “/* */” in any C program,
won’t be considered for compilation and execution.
printf(“Hello_World! “); printf command prints the output onto the screen.
getch(); This command waits for any character input from keyboard.
b) Compiler checks for errors if source code contains errors then goto sourcecode to
modify.
c) If source code is errors free then compiler comments into object with an
“extension .obj”.
c) If object code is errors free then linkers comments “.obj” file into executetable
file with an extension “.exe”.
a) Executable file .exe is sum, if any errors at run time then go to source code
tomodify.
b) If no runtime error occurs then the output will be displayed to the user and
theprogram is successful.
A character set defines the valid characters that can be used in source programs or
interpreted when a program is running.
The source character set is the set of characters available for the source text/source
code.The character set are classified into:
1. Alphabets / Letters
2. Digits
3. Special Symbols
4. White Spaces
▪ Constants are the fixed values that do not change during the execution of a program.
– Numeric Constants
▪ A numeric constant is a sequence of numeric digits. It required minimum size of 2bytes and
max 4 bytes. It may be positive or negative but by default sign is always positive. These are
two types
1. Integer constants
2. Real constants / Floating constants
– Character Constants
2. String Constants
Variables
▪ Unlike constants, the value of a variable can be changed during the execution of aprogram.
▪ The rule for naming the variables is same as the naming identifier. Before used in the
program it must be declared.
▪ Declaration of variables specifies its name, data types and range of the value thatvariables
can store depends upon its data types.
4. C variables are case sensitive. Ex: name and Name are two different variables.
Declaring a Variable:
▪ Syntax:variable_name = value;
Keywords
▪ Keywords are predefined, reserved words in C language and each of which isassociated
with specific features.
▪ C language has reserved 32 words as auto double int struct continue for
keywords.
break else switch default do if
▪ All the Keywords are written in
case char goto return union float
lower-case letters.
▪ Since C is case-sensitive.
▪ Identifiers refer to the name of variables, functions and arrays. Each variable has a name by
which it is identified in the program. In C Language the identifier is allowedthe maximum
length of 31.
▪ After the first character, an identifier can have any combination of characters.
The kind of data variable holds in a programming language is called “Data type”.
Syntax of declaring variable of any primary data type is data-type var1, var 2, var n;
Integer : Integers are defined by using the keyword “int”. The format specifier
for interger is “%d”. It occupies 2 bytes of memory. Range of integer is -32768 to +32767.
There are some derived data types which are supported by C such as arrays, functions,
structures, and pointers.
These are data types defined by user. typedef and enumerated data types are two user defined
data types.
#include <stdio.h>
void main() {
// datatypes Output:
Integer datatype : 10
int a = 10; char b = 'S'; float c = 2.88; Character datatype : S
double d = 28.888; Float datatype : 2.880000
Double Float datatype : 28.888000
printf("Integer datatype : %d\n",a);
printf("Character datatype : %c\n",b);
printf("Float datatype : %f\n",c);
printf("Double Float datatype : %lf\n",d);
getch();
}
Explain different arithmetic operators, relational operators, and logical operators with
their precedence
Operators:
Operators are Symbols that tells the computer to perform certain mathematical or
logical manipulations.
These operators are used in programs to manipulate data and variables.
Arithmetic Operator:
The Arithmetic operators are some of the C Programming Operator, which are used to perform
arithmetic operations includes operators like Addition, Subtraction, Multiplication, Division
and Modulus.
* Multiplication a*b=8
/ Division a/b=2
% Modulus a%b=0
It returns the remainder after (Here remainder is zero). If it is
the division 10 % 3 then it will be 1).
Example: Write a C program to print Arithmetic operators using only two variables.
#include<stdio.h>
void main() Output:
{ addition of 2 numbers is 13
subtraction of 2 numbers is 7
int a=10,b=3;
multiplication of 2 numbers is 30
division of 2 numbers is 3
printf("\naddition of 2 numbers is %d",a+b);
modulo of 2 numbers is 1
printf("\nsubtraction of 2 numbers is %d",a-b);
printf("\nmultiplication of 2 numbers is %d",a*b);
printf("\ndivision of 2 numbers is %d",a/b);
printf("\nModulo of 2 numbers is %d",a%b);
getch();
}
Within the above arithmetic operators in C, The value of a is 10 and the value of b is 3 then
the adition of two operands is 13.
The value of a is 10 and the value of b is 3 then the subtraction of two operands is 7.
The value of a is 10 and the value of b is 3 then the multiplication of two operands is 13.
RELATIONAL OPERATOR
Relational Operators are used to compare two quantities and take certain decision depending on
their relation.
All the operators are called as binary operators because they take two expressions as operands.
If the specified relation is true it returns one. If the specified relation is false it returns zero.
== Checks if the values of two operands are equal or not. If yes, then the condition
becomes true.
!= Checks if the values of two operands are equal or not. If the values are not equal, then the
condition becomes true.
> Checks if the value of left operand is greater than the value of right operand. If yes, then the
condition becomes true.
< Checks if the value of left operand is less than the value of right operand. If yes, then the
condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of right operand.
If yes, then the condition becomes true.
<= Checks if the value of left operand is less than or equal to the value of right operand. If
yes, then the condition becomes true.
Example: Try the following example to understand all the relational operators available
in C
#include<stdio.h>
void main()
{
int a=9, b=4;
printf("\n The < value of a is %d", a<b);
These operators are used for testing more than one condition and making decisions. It
always produce single result in the form of true or false but at a time only one result is valid.
getch();
Output:
&& operator : Both conditions are true 0
}
|| operator : Only one condition is true 1
! reverses the state of the operand 0
((a!=b)||(b>c)&&(a<b)||(b>c))
((4!=4) || (4>5)) && ((3<4) || (4>5))
When there is more than one operator in an expression the operator with higher precedence is
evaluated first and the operator with the least precedence is evaluated last.
+,- Arithmetic 4
(binary)
< ,<=, >, >= Relational 5
==, != Relational 6
&& Logical AND 7
|| Logical OR 8
Since C language does not support chaining assignment like a=b=c; each assignment
operator (=) a=b
operates on two operands only. Then how expression a=b=c evaluates? According to
operators associativity assignment operator (=) operates from right to left, that means
associativity of assignmentoperator (=) is right to left.
Expression a=b=cis actually a=(b=c), see how expression a=(b=c)evaluates?
Both are unary operators and can be used as pre or post increment/decrement.
#include <stdio.h>
void main()
{
Department of Computer Science Engineering
Aditya Group of polytechnic Colleges(255,249,404),Surampalem Page 14
LECTURE NOTES
EC-405 PROGRAMMING IN C
int b=10;
printf("\nThe value of b is %d",b);
printf("\npost increment of b is %d",b++);
printf("\npost increment of b is %d",++b);
printf("\npre decrement of b is %d",--b);
printf("\npost decrement of b is %d",b--);
printf("\nThe value of b is %d", b);
getch();
}
Output:
The value of b is 10
post increment of b is 10
post increment of b is 12
pre decrement of b is 11
post decrement of b is 11
The value of b is 10
The printf() function is used to display aoutput and the scanf() function is used to take input
from users.
The printf() and scanf() functions are commonly used functions in C Language.These functions
are inbuilt library functions in header files of C Programming.
printf() function:-
In C Programming language, the printf() function is used for output.
printf() function can take any number of arguments.Frist argument must be enclosed
within the double quotes "hello" and every other argument should be separated by
comma(,) within the double quotes.
scanf() Function:
The scanf() function is used to read input data from the console.
The scanf() function is in built function available in the C library.
scanf() function can read character,string,numeric and other data from keyword in C
language.
Syntax:
scanf("format specifier", argument_list);
Example:
#include <stdio.h>
void main()
{
// using scanf()
int number;
printf("Please enter a number: ");
scanf("%d", &number);
printf("You entered: %d", number);
getch();
}
Output:
Please enter a number: 7
You entered: 7
Type Casting or Type Conversion is a process of converting one type of data intoanother type
of data.
The conversion is done only between those datatypes where in the conversion ispossible i.e..
char to int and vice versa. Type conversion are two types:
The compiler usually performs this type of conversion when a particular expression
contains more than one data type. In such cases either type promotion or demotion takes
place.
bool->char->int->long->float->double
Explicit type Conversion : Explicit type conversion rules out the use of compiler for converting one
data type toanother instead the user explicitly defines within the program the datatype of the
▪ Example:
#include<stdio.h>
void main()
{
double x=1.2;
int sum=(int)x+1; // Explict conversion from double to int
printf("Sum=%d",sum);
getch();
} Output: Sum=2
BITWISE OPERATORS:
Bitwise operators do manipulation on bits stored in memory.
These operators work with char and int type data. They cannot be used with floating
point numbers.
Operators Meaning of operators
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
The & (bitwise AND) in C takes two numbers as operands and does AND on every it of two
numbers. The result of AND is 1 only if both bits are 1.
The | (bitwise OR) in C takes two numbers as operands and does OR on every bit of two
numbers. The result of OR is 1 if any of the two bits is 1.
The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every bit of
two
numbers. The result of XOR is 1 if the two bits are different.
The ~ (bitwise NOT) in C takes one number and inverts all bits of it.
******