Updated Section 2 (1)
Updated Section 2 (1)
Syllabus
Definition of Software, its classification, Problem solving steps, Introduction of C and its structure, history
and characteristics, Introduction to keywords, Constants and identifiers, Fundamental of C variable and data
types, Rules of constants, Introduction to arithmetic, relational and logical operators, Introduction to
expressions, Managing data input, Managing data output.
Page 1
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Page 2
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Translator program
A translator takes a program written in source language as input and converts it into a program in target
language as output. It also detects and reports the error during translation.
Example:
Page 3
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Compiler
A compiler is a translator used to convert high-level programming language to low-level programming
language. It converts the whole program in one session and reports errors detected after the conversion.
Interpreter
Just like a compiler, is a translator used to convert high-level programming language to low-level programming
language. Interpreter translates line by line and reports the error once it encountered during the translation
process.
Page 4
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Assembler
An assembler is a translator used to translate assembly language to machine language.
Page 5
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Program writing is a systematic process. It is not all about coding only. It is not a magical or random process.
It requires following a certain methodology. In order to write a program, following steps must be formally or
informally followed:
• Building mathematical model of the problem [DFD, decision tree, system flowchart etc.]
• Timeline
3. Program Design (Algorithm/Flowchart)
Identify various processing steps needed to solve the problem and represent them in a particular way
(algorithm, pseudo code, flow chart etc.).
• Input design
• Output design
• Relationship between input and output design
Page 6
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
6. Documentation
Program needs to be documented for future use. Following documents need to be kept:
• Explanation of program
• Flowchart
• Source code
• Steps to run the program
• Details of testing and result
7. Program Maintenance
• Upgrading the program
• Changing program
• Supporting users
In the above algorithm, steps 2 and 3 are not simple. Both of these require repetition of the work. The details
of that repetition can also be specified to make the algorithm clearer.
Page 7
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
The detailed algorithm is written below which will include the repetition details as well:
This algorithm has every step equivalent to some programming language instruction and thus, is a
complete solution of the problem.
Pseudo Code
Another useful representation of the detailed step is pseudo code. It is a more formal representation than the
algorithm.
All pseudo code will start with keyword START and complete with keyword STOP or END.
The above written detailed algorithm can be written in form of pseudo code as follows:
1. START
2. Read n
3. Sum 0
4. for i 1 to n do
5. read number i
6. sum sum + number i
7. end for
8. average sum/n
9. print average
10. STOP
Flow Chart
Flowchart is a diagrammatic representation of an algorithm, process or program. Flowchart is very helpful in
writing program and explaining program to others.
Page 8
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Symbols Used In Flowchart
Different symbols are used for different states in flowchart, for example: Input/output and decision making
has different symbols. The table below describes all the symbols that are used in making flowchart
Used to represent the operation in which there are two alternatives, true
Decision
and false.
Predefined
Used to represent a group of statements performing one processing task.
Process/Function
Example
The flow chart for the above mentioned problem of finding average of n numbers will be drawn s shown in
the following figure:
Page 9
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Page 10
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Page 11
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Define C programming.
C is a high-level and general-purpose programming language that is ideal for developing firmware or portable
applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis
Ritchie for the Unix Operating System in the early 1970s.
C programming is considered as the base for other programming languages, that is why it is known as
mother language.
1. Mother language
2. System programming language
3. Procedure-oriented programming language
4. Structured programming language
5. Mid-level programming language
1) C as a mother language
C language is considered as the mother language of all the modern programming languages because most of
the compilers, JVMs, Kernels, etc. are written in C language, and most of the programming languages
follow C syntax, for example, C++, Java, C#, etc.
It provides the core concepts like the array, strings, functions, file handling, etc. that are being used in
many languages like C++, Java, C#, etc.
2) C as a system programming language
A system programming language is used to create system software. C language is a system programming
language because it can be used to do low-level programming (for example driver and kernel). It is
generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in
C. It can't be used for internet programming like Java, .Net, PHP, etc.
3) C as a procedural language
A procedure is known as a function, method, routine, subroutine, etc. A procedural language specifies a series of
steps for the program to solve the problem.
A procedural language breaks the program into functions, data structures, etc.
C is a procedural language. In C, variables and function prototypes must be declared before being used.
4) C as a structured programming language
A structured programming language is a subset of the procedural language. Structure means to break a
program into parts or blocks so that it may be easy to understand.
In the C language, we break the program into parts using functions. It makes the program easier to
understand and modify.
5) C as a mid-level programming language
C is considered as a middle-level language because it supports the feature of both low-level and high-level
languages. C language program is converted into assembly code, it supports pointer arithmetic (low-level), but
it is machine independent (a feature of high-level).
A Low-level language is specific to one machine, i.e., machine dependent. It is machine dependent, fast to run.
But it is not easy to understand.
A High-Level language is not specific to one machine, i.e., machine independent. It is easy to understand.
Page 12
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Structure of a C Program
Every C program consists of one or more modules called functions. One of the functions must be called
main. The program will always begin by executing the main function, which may access other functions. Any
other function definitions must be defined separately, either ahead of or after main.
1. A function heading, which consists of the function name, followed by an optional list of arguments,
enclosed in parentheses.
The arguments are symbols that represent information being passed between the function and other parts of
the program. (Arguments are also referred to as parameters.)
Each compound statement is enclosed within a pair of braces, i.e., { }. The braces may contain one or more
elementary statements (called expression statements) and other compound statements. Thus compound
statements may be nested, one within another. Each expression statement must end with a semicolon (;).
Comments (remarks) may appear anywhere within a program, as long as they are placed within the delimiters
/* and */ (e.g., /* this is a comment */). Such comments are helpful in identifying the program's principal
features or in explaining the underlying logic of various program features.
Page 13
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
#include <stdio.h>
int main() {
printf("Hello C Programming\n");
return 0;
}
#include <stdio.h> includes the standard input output library functions. The printf() function is defined
in stdio.h .
int main() The main() function is the entry point of every program in c language.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for successful
execution and 1 for unsuccessful execution.
Page 14
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Features of C Language
C is the widely used language. It provides many features that are given below.
1. Simple
2. Machine Independent or Portable
3. Mid-level programming language
4. structured programming language
5. Rich Library
6. Memory Management
7. Fast Speed
8. Pointers
9. Recursion
10. Extensible
1) Simple
C is a simple language in the sense that it provides a structured approach (to break the problem into parts),
the rich set of library functions, data types, etc.
2) Machine Independent or Portable
Unlike assembly language, c programs can be executed on different machines with some machine specific
changes. Therefore, C is a machine independent language.
3) Mid-level programming language
Although, C is intended to do low-level programming. It is used to develop system applications such as
kernel, driver, etc. It also supports the features of a high-level language. That is why it is known as mid-level
language.
4) Structured programming language
C is a structured programming language in the sense that we can break the program into parts
using functions. So, it is easy to understand and modify. Functions also provide code reusability.
5) Rich Library
C provides a lot of inbuilt functions that make the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the allocated memory at
any time by calling the free() function.
7) Speed
The compilation and execution time of C language is fast since there are lesser inbuilt functions and hence
the lesser overhead.
8) Pointer
C provides the feature of pointers. We can directly interact with the memory by using the pointers. We can
use pointers for memory, structures, functions, array, etc.
9) Recursion
In C, we can call the function within the function. It provides code reusability for every function.
Recursion enables us to use the approach of backtracking.
10) Extensible
C language is extensible because it can easily adopt new features.
Page 15
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
File: simple.c
#include <stdio.h>
int main(){
printf("Hello C Language");
return 0;
}
Let's try to understand the flow of above program by the figure given below.
1) C program (source code) is sent to preprocessor first. The preprocessor is responsible to convert
preprocessor directives into their respective values. The preprocessor generates an expanded source code.
2) Expanded source code is sent to compiler which compiles the code and converts it into assembly code.
3) The assembly code is sent to assembler which assembles the code and converts it into object code. Now
a simple.obj file is generated.
4) The object code is sent to linker which links it to the library such as header files. Then it is converted
into executable code. A simple.exe file is generated.
5) The executable code is sent to loader which loads it into memory and then it is executed. After
execution, output is sent to console.
Page 16
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
scanf() function
The scanf() function is used for input. It reads the input data from the console.
scanf("format string", &argument_list);
Page 17
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
What is variable in C?
A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can
be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Let's see the syntax to declare a variable:
type variable_list;
The example of declaring the variable is given below:
int a;
float b;
char c;
Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:
int a=10, b=20; //declaring 2 variable of integer type
float f=20.8;
char c='A';
Types of Variables in C
There are many types of variables in c:
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Page 18
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
1. Local Variable
A variable that is declared inside the function or block is called a local variable.
void function1()
{
int x=10; //local variable
}
You must have to initialize the local variable before it is used.
2. Global Variable
A variable that is declared outside the function or block is called a global variable. Any function can change
the value of the global variable. It is available to all the functions.
3. Static Variable
A variable that is declared with the static keyword is called static variable.
void function1()
If you call this function many times, the local variable will print the same value for each function call, e.g,
11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11,
12, 13 and so on.
Page 19
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
4. Automatic Variable
All variables in C that are declared inside the block, are automatic variables by default. We can explicitly
declare an automatic variable using auto keyword.
void main()
{
int x=10; //local variable (also automatic)
auto int y=20; //automatic variable
}
5. External Variable
We can share a variable in multiple C source files by using an external variable. To declare an external
variable, you need to use extern keyword.
myfile.h
extern int x=10; //external variable (also global)
program1.c
#include "myfile.h"
#include <stdio.h>
void printValue()
{
printf("Global variable: %d", x);
}
Constants in C
Constants refer to fixed values that the program may not alter during its execution. These fixed values are
also called literals. Constants can be of any of the basic data types like an integer constant, a floating
constant, a character constant, or a string literal. There are enumeration constants as well. Constants are
treated just like regular variables except that their values cannot be modified after their definition.
List of Constants in C
Constant Example
Page 20
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Defining Constants
There are two simple ways in C to define constants −
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following result − value of area : 50
#include <stdio.h>
main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
}
When the above code is compiled and executed, it produces the following result − value of area : 50
Note that it is a good programming practice to define constants in CAPITALS.
Page 21
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
The big advantage of const over #define is type checking. We can also have pointers to const variables, we can pass
them around, typecast them and any other thing that can be done with a normal variable. One disadvantage that one
could think of is extra space for variable which is immaterial due to optimizations done by compilers.
In general const is a better option if we have a choice. There are situations when #define cannot be replaced by
const. For example, #define can take parameters. #define can also be used to replace some text in a program
with another text
Page 22
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Float 4 byte
Double 8 byte
Page 23
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Type Casting in C
Typecasting allows us to convert one data type into other. In C language, we use cast operator for
typecasting which is denoted by (type).
Syntax:
1. (type)value;
Note: It is always recommended to convert the lower value to higher for avoiding data loss.
int f= 9/4;
printf("f : %d\n", f ); //Output: 2
With Type Casting:
float f=(float) 9/4;
printf("f : %f\n", f ); //Output: 2.250000
Let's see a simple example to cast int value into the float.
#include<stdio.h>
int main(){
float f= (float)9/4;
printf("f : %f\n", f );
return 0;
}
Output:
f : 2.250000
Keywords in C
A keyword is a reserved word. You cannot use it as a variable name, constant name, etc. There are only
32 reserved words (keywords) in the C language.
Page 24
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
There are following types of operators to perform different types of operations in C language.
o Arithmetic Operators
o Relational Operators o
Shift Operators
o Logical Operators o
Bitwise Operators
o Ternary or Conditional Operators o
Assignment Operator
o Misc Operator
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume variable A holds
10 and variable B holds 20 then −
#include <stdio.h>
main()
{
int a = 21;
int b = 10;
int c ;
Page 25
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
c = a + b;
printf("Line 1 - Value of c is %d\n", c );
c = a - b;
printf("Line 2 - Value of c is %d\n", c );
c = a * b;
printf("Line 3 - Value of c is %d\n", c ); Output:
Line 1 - Value of c is 31
c = a / b; Line 2 - Value of c is 11
printf("Line 4 - Value of c is %d\n", c ); Line 3 - Value of c is 210
Line 4 - Value of c is 2
c = a % b; Line 5 - Value of c is 1
printf("Line 5 - Value of c is %d\n", c );
Line 6 - Value of c is 21
c = a++; Line 7 - Value of c is 22
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", c );
}
Relational Operators
The following table shows all the relational operators supported by C. Assume variable A holds 10 and
variable B holds 20 then −
== Checks if the values of two operands are equal or not. If yes, (A == B) is not true.
then the condition becomes true.
!= Checks if the values of two operands are equal or not. If the (A != B) is true.
values are not equal, then the condition becomes true.
> Checks if the value of left operand is greater than the value (A > B) is not true.
of right operand. If yes, then the condition becomes true.
< Checks if the value of left operand is less than the value of (A < B) is true.
right operand. If yes, then the condition becomes true.
>= Checks if the value of left operand is greater than or equal to (A >= B) is not true.
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 (A <= B) is true.
value of right operand. If yes, then the condition becomes
true.
Page 26
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Example of relational operators
#include <stdio.h>
main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
printf("Line 1 - a is equal to b\n" );
} else {
printf("Line 1 - a is not equal to b\n" );
}
if ( a < b ) {
printf("Line 2 - a is less than b\n" );
Output:
} else {
printf("Line 2 - a is not less than b\n" ); Line 1 - a is not equal to b
} Line 2 - a is not less than b
if ( a > b ) { Line 3 - a is greater than b
printf("Line 3 - a is greater than b\n" ); Line 4 - a is either less than or equal to b
} else { Line 5 - b is either greater than or equal to b
printf("Line 3 - a is not greater than b\n" );
}
/* Lets change value of a and b */
a = 5;
b = 20;
if ( a <= b ) {
printf("Line 4 - a is either less than or equal to b\n" );
}
if ( b >= a ) {
printf("Line 5 - b is either greater than or equal to b\n" );
}
}
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1
and variable B holds 0, then −
&& Called Logical AND operator. If both the operands are non-zero, (A && B) is false.
then the condition becomes true.
! Called Logical NOT Operator. It is used to reverse the logical !(A && B) is true.
state of its operand. If a condition is true, then Logical NOT
operator will make it false.
Page 27
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Example: Logical Operators
#include <stdio.h>
main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
printf("Line 1 - Condition is true\n" );
}
if ( a || b ) {
printf("Line 2 - Condition is true\n" );
}
/* lets change the value of a and b */
a = 0;
Output:
b = 10;
if ( a && b ) { Line 1 - Condition is true
printf("Line 3 - Condition is true\n" ); Line 2 - Condition is true
} else { Line 3 - Condition is not true
printf("Line 3 - Condition is not true\n" ); Line 4 - Condition is true
}
if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
} }
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and ^ is as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Page 28
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable
'B' holds 13, then −
& Binary AND Operator copies a bit to the result if it (A & B) = 12, i.e., 0000
exists in both operands. 1100
| Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.
^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49, i.e., 0011 0001
operand but not both.
~ Binary One's Complement Operator is unary and has (~A ) = ~(60), i.e,. -0111101
the effect of 'flipping' bits.
#include <stdio.h>
main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101
*/ int c = 0;
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
Output:
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c ); Line 1 - Value of c is 12
Line 2 - Value of c is 61
c = ~a; /*-61 = 1100 0011 */ printf("Line Line 3 - Value of c is 49
4 - Value of c is %d\n", c ); Line 4 - Value of c is -61
Line 5 - Value of c is 240
c = a << 2; /* 240 = 1111 0000 */ Line 6 - Value of c is 15
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}
Page 29
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Assignment Operators
The following table lists the assignment operators supported by the C language −
Page 30
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Example Assignment Operators
#include <stdio.h>
main() {
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200; Output:
c %= a; Line 1 - = Operator Example, Value of c = 21
printf("Line 6 - %= Operator Example, Value of c = %d\n", c ); Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
c <<= 2; Line 4 - *= Operator Example, Value of c = 441
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c ); Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
c >>= 2; Line 8 - >>= Operator Example, Value of c = 11
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c ); Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
c &= 2; Line 11 - |= Operator Example, Value of c = 2
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
}
Page 31
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Besides the operators discussed above, there are a few other important operators including sizeof and
? : supported by the C Language.
#include <stdio.h>
main() {
int a = 4;
short b;
double c;
int* ptr;
Page 32
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Precedence of Operators in C
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operator direction to be evaluated; it may be left to right or right to left.
Let's understand the precedence by the example given below:
int value=10+20*10;
The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).
The precedence and associativity of C operators is given below:
Page 33
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
#include<stdio.h>
int main(){
//printing information Output:
printf("Hello C"); Hello C
return 0;
}
Escape Sequence in C
An escape sequence in C language is a sequence of characters that doesn't represent itself when used
inside string literal or character.
It is composed of two or more characters starting with backslash \. For example: \n represents new line.
Page 34
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
List of Escape Sequences in C
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\? Question Mark
\0 Null
#include<stdio.h>
int main(){
int number=50;
printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\"");
return 0; Output:
} You
are
learning
'c' language
"Do you know C language"
Page 35
Course Title: Introduction to Computer & Computer Programming Course Code: CCE-1201
Page 36