Unit1 C Sreedhar
Unit1 C Sreedhar
Introduction:
What is Problem Solving in Programming?
Computers are used to solve various problems in day-to-day life. Problem Solving is an essential
skill that helps to solve problems in programming. There are specific steps to be carried out to solve
problems in computer programming, and the success depends on how correctly and precisely we
define a problem.
This involves designing, identifying and implementing problems using certain steps to develop a
computer.
Types of Languages:
High-level languages are easy to learn. Because these are similar to human languages so we can easily
modify the language. But the disadvantage of this high-level language is it takes more time for
execution.Because High-level languages are translated into machine language by a compiler and then
execution will start. These languages are normally used to write application programs.
Algorithm is a computer procedure that is a lot like a recipe (called a procedure) and tells your computer
precisely what steps to take to solve a problem or reach a goal. The ingredients are called inputs, while
the results are called the outputs.
Flowchart
A flowchart is a type of diagram that represents an algorithm, workflow or process, showing the
steps as boxes of various kinds, and their order by connecting them with arrows.
This diagrammatic representation illustrates a solution model to a given problem.
Flowcharts are used in analyzing, designing, documenting or managing a process or program in
various fields.
Flowchart Symbols
History of C
Introduction to C: -
C is a high level language. It is both a general purpose(to develop applications programs) and a
specific purpose(to develop System programs ) programming language.
It was developed at the Bell Telephone Laboratory, USA (Now AT & T), in 1972. It was developed
by Dennis Ritchie .
C is derived from two early programming languages such as BCPL (Basic Combined Programming
Language) and B language.
He selected the name C for his new language because C comes after B in alphabetical order which
indicates advancement to B.
C is a General Purpose Programming Language This means C can be used to write a variety of
applications. It is often referred to as a “system programming language.”
1. C is renowned for its simplicity and is easy to use because of its structured approach.
2. C is portable, which means a C program runs in different environments.
3. C compilers are available for all operating systems and hardware platforms.
4. C is modular, which means C programs can be divided into small modules, which are much easier
to understand.
5. C is easy to debug. The C compiler detects syntax errors quickly and easily and displays the errors
along with the line numbers of the code and the error message.
Applications of C:-
Because of its portability and efficiency, C is used to develop the system as well as application software.
Some of the system and application software listed below are:
1. Operating Systems(System software)
2. Interpreters and Compilers(System software)
3. Editors(System software)
4. DBMS(Application software)
5. Spread sheets(Application software)
6. Graphics packages(Application software)
7. Word Processors(Application software)
C Tokens:-
A token in C can be defined as the smallest individual element of the C programming language that is
meaningful to the compiler.
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Operators
Identifiers:-
Identifiers are the names given to the program elements such as variables, arrays and functions.
Basically, identifiers are the sequences of alphabets and digits. The rules that govern the formation of
identifier names are:
1. The first character must be an alphabet or an underscore.
2. All succeeding characters must be either letters or digits
3. Uppercase and lowercase identifiers are different
4. No special character or punctuation symbols are allowed except the underscore.
5. No two successive underscores are allowed.
6. Keywords should not be used as identifiers.
Constants:
The quantity which does not change during the execution of a program is known as constant.
Constants are created by using a keyword called “const”.
Ex: const int a=10;
Note: after this statement if we are trying to modify the a value it gives an error as it is a constant.
Variables:-
1. The quantity that changes during the execution of a program is called a variable.
2. The variables are the name given to identify the specific program elements.
3. Variables are nothing but reserved memory locations to store values. This means when you create a
variable, you reserve some space in memory.
Rules for forming variable names:-
1. The first character must be an alphabet or an underscore.
2. All succeeding characters must be either letters or digits
3. Uppercase and lowercase variables are different
4. No special character or punctuation symbols are allowed except the underscore.
5. No two successive underscores are allowed.
6. Keywords should not be used as variables.
Predefined Data types:- They are basic data types and designate a single value. There are four
fundamental built in data types in C language. They are:
1. Integer
2. character
3. Floating point
4. Double
In C, there is one and only one non-specific data type called void. It does not specify anything.
Prepared By: Sreedhar Aakula, RGUKT
Int:-
Int is a keyword used to indicated integer number. Any integer number is a sequence of digits
without decimal point.
Float:-
Float is a keyword used to indicate a floating point number. The floating point numbers are the
same as real numbers.
Char:-
Char is a keyword used to indicate the character type data. The data may be a character constant
or a string constant. A character constant can be defined as any single character enclosed in a pair of
single quotes.
Double:-
Double is a keyword used to indicate a double precision floating point number. The precision
associated with the accuracy of the data. A double is normal float value, but the number of significant
digits that are stored after the decimal point is double that of the float.
Prepared By: Sreedhar Aakula, RGUKT
The float usually stores a maximum of 6 digits after the decimal point but double stores 16 significant
digits after the decimal point.
Modifiers in C language:
The amount of memory space to be allocated for a variable is derived by modifiers.
Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount
of storage space allocated to a variable.
There are 4 modifiers available in C language. They are,
1. short
2. long
3. signed
4. unsigned
Short
In general int data type occupies different memory spaces for a different operating system; to allocate
fixed memory space short keyword can be used. short int occupies 2 bytes of memory space in every
operating system.
Long
This can be used to increased size of the current data type to 2 more bytes, which can be applied on int
or double data types. For example int occupy 2 byte of memory if we use long with integer variable then
it occupy 4 byte of memory.
Signed
This keyword accepts both negative or positive value and this is default properties or data type modifiers
for every data type. in real time no need to write signed keyword explicitly for any data type.
Example
int a=10; // right
int a=-10; // right
signed int a=10; // right
signed int a=-10; // right
Unsigned
This keyword can be used to make the accepting values of a data type is positive data type.
Example
unsigned int a =100; // right
unsigned int a=-100; // wrong
%% to print %
Preprocessor statements:-
These statements begin with # symbol and also called as preprocessor directives. These
statements direct the C preprocessor to include header files and also symbolic constants into a C
program.
Examples:-
#include<stdio.h> -For the standard input / output functions
The main() function:-
As the name itself indicates, this is the main function of every C program. Execution of C program
starts with main(). No C program is executed without the main() function. The function main() should be
written in lowercase letters and should not be terminated by a semicolon. There must be one and only
one main() function in every C program.
Braces:-
Every C program uses a pair of curly braces ({, }). The left brace indicates the beginning of the
main() function. The right brace indicates the end of the main() function. The braces can also be used to
indicate the beginning and end of use-defined functions.
Declarations:-
The declaration is a part of the C program where all the variables, arrays, functions etc.. used in
the C program are declared and may be initialized with their basic data types.
Statements:-
These are instructions to the computer to perform some specific operations. They may be I/O
statements, arithmetic statements, control statements and other statements. They also include
comments. The comments are explanatory notes on some instructions. The comment statements are not
compiled and executed.
User defined functions:-
These are subprograms. A subprogram is a function. The user-defined functions contain a set of
statements to perform a specific task. These are written by the user. They may be written before or after
the main() function.
Prepared By: Sreedhar Aakula, RGUKT
Example:
write a c program to display “hello computer” on the screen
#include<stdio.h>
void main()
{
printf("hello computer\n");
}
Compiling and Executing a C program:-
Compiling a C program means translating it into machine language. C compilers are used for this
purpose. The C program to be compiled must be typed in using an editor. An editor is a program which
allows the programmer to write the program and edit it. C compilers are available with and without
editors.
There are basically five steps in the successful execution of a program.
1. Creating a source file(program file)
2. Saving the program(with .c as extension)
3. Compilation
4. Linking system library functions
Example1:
#include <stdio.h>
Prepared By: Sreedhar Aakula, RGUKT
int main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
Output
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Operators in C Language
Operators are that symbols which work on operands. Operator in C language is used to perform specific
mathematical or logical computations on the operands and it reduces a single value.
Operators in C language, are classified into several categories.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Shift Operators
6. Ternary or Conditional Operators
Arithmetic Operators
An arithmetic operator is used to perform arithmetic/mathematical operations on operands. Some of
the arithmetic operators are (+, -, *, /, %, ++, --)
Operator Name of Operator What it does How it is used
+ Plus Add two Operands a+b
- Minus Subtracts the second operand from the first. a-b
* Multiplication Multiplies both operands. a*b
/ Division Divides numerator by de-numerator. a/b
% Modulus return remainder, after an integer division. a%b
++ Increment Operator increases the integer value by one. a++
-- Decrement Operator decreases the integer value by one. a- -
Relational Operators
Relational operators help in making a relationship or comparison between two operands with which they
are used. Hence, relational operators help us make decisions in the program and their end result is either
true or false. Some of the relation operators are (==, !=, <, >, <=, >=)
Example: a==b
The expression given above, we used an equality operator that means it checks the value of a and b if
both values are the same then it will return true otherwise it will return false.
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.
Shift Operators:
Right Shift Operator
Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.
212 = 11010100 (In binary)
212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000
212>>0 = 11010100 (No Shift)
Left Shift Operator
Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.
212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
Prepared By: Sreedhar Aakula, RGUKT
212<<0 =11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)
C Ternary Operator (?:)
A conditional operator is a ternary operator, that is, it works on 3 operands.
Conditional Operator Syntax:
(condition) ? True part : False part
Example #6: C conditional Operator
#include<stdio.h>
int main()
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Even\n"):printf("Odd\n");
}
Output
Enter the Number : 4
Even
Assignment Operators
An assignment operator is used to assign values to the operands. Some of the assignment operators
are (=, +=, -=, *=, /=, %=)
Eample: a = b
In the above example, we are assigning b's value to variable a.
Operator Name of What it does How it is used
Operator
= assignment assign value of variable b to variable a a=b
+= plus assign a = a+b (adds values of a to b and assign this value to a += b
a)
-= minus assign a = a-b (subtracts values of b from a and assign this a -= b
value to a)
*= times assign a = a*b (Multiplies a with b and assign the value to a) a *= b
/= div assign a = a/b (divides a by b and assigns the value to a) a /= b
Comments:
A comment is an explanation or description of the source code of the program. It helps a developer
explain logic of the code and improves program readability. At run-time, a comment is ignored by the
compiler.
There are 2 types of comments in the C language.
1. Single Line Comments
2. Multi-Line Comments
MultiLine Comments
Multi-Line comments are represented by slash asterisk \* ... *\. It can occupy many lines of code, but it
can't be nested.
Syntax:
/*
code
to be commented
*/
Errors:
Errors are the problems or the faults that occur in the program (or)
Error is a abnormal condition whenever it occurs execution of the program is stopped.
There are commonly five types of errors exist in C programming:
1. Syntax error
2. Run-time error
3. Linker error
4. Logical error
5. Semantic error
Logical error:
The logical error is an error that leads to an undesired output. These errors produce the incorrect
output, but they are error-free, known as logical errors. The occurrence of these errors mainly depends
upon the logical thinking of the developer.
Semantic Error:
Errors that occur because the compiler is unable to understand the written code are called Semantic
Errors. A semantic error will be generated if the code makes no sense to the compiler, even though it is
syntactically correct. It is like using the wrong word in the wrong place in the English language.
Linker Error:
Linker errors are mainly generated when the executable file of the program is not created. This can be
happened either due to the wrong function prototyping or usage of the wrong header file.