CCP 3 Unit
CCP 3 Unit
3rd Unit
Programming languages allow us to translate the 1s and 0s into something that humans can understand
and write.
A programming language is made up of a series of symbols that serves as a bridge that allow humans to
translate our thoughts into instructions computers can understand.
High level languages are written in a form that is close to our human language, enabling
to programmer to just focus on the problem being solved.
These programmer friendly languages are called ‘high level’ as they are far removed
from the machine code instructions understood by the computer.
Advantages
They are closer to the native language of a computer (binary), making them harder for
programmers to understand.
Assembly Language
Machine Code
Assembly Language
Few programmers write programs in low level assembly language, but it is still used for
developing code for specialist hardware, such as device drivers.
Machine Code
C programming is considered as the base for other programming languages, that is why it is known as
mother language.
It is one of the most popular computer languages today because of its structure, high-level
abstraction, machine independent feature etc.
Facts about C
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around the early 1970s.
The language was formalized in 1988 by the American National Standard
Institute (ANSI).
The UNIX OS was totally written in C.
Today C is the most widely used and popular System Programming Language.
Most of the state-of-the-art software have been implemented using C.
1. Mother language
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
Initially, C language was developed to be used in UNIX operating system. It inherits many features of
previous languages such as B and BCPL.
History of C language
C language has evolved from three different structured language ALGOL, BCPL and B
Language.
It uses many concepts from these languages while introduced many new concepts such
as datatypes, struct, pointer etc.
In 1989, the language was formalised by American National Standard
Institute(ANSI).
Features of C language
It is a robust language with rich set of built-in functions and operators that can be used
high-level language.
Programs Written in C are efficient and fast. This is due to its variety of data type and
powerful operators.
C is highly portable this means that programs once written can be run on another
C language is the most widely used language in operating systems and embedded
The compilation is a method whereby the source code is converted into object code.
It is achieved with compiler assistance.
The compiler tests the source code for syntactic or structural errors and produces the
object code if the source code is error-free.
The method of compiling can be divided into four stages, i.e., pre-processing, compilation,
assembly, and linking.
Preprocessor
Compiler
Assembler
Linker
First, the input file is transferred to the preprocessor, i.e., hello.c, and then the preprocessor
transforms the source code to extended source code. The expanded source code extension
will be hello.i.
The expanded source code is transferred to the compiler, and this expanded source code is
converted to assembly code by the compiler. The assembly code extension will be a hello.s.
It then sends this assembly code to the assembler, which transforms the assembly code into
object code.
When an object code has been developed, the linker generates the executable file.
C Token
#include
int main()
printf("Hello,World")
return 0;
Comments
Comments are plain simple text in a C program that are not compiled by the compiler.
We write comments for better understanding of the program.
C is a case sensitive language so all C instructions must be written in lower case letter.
2) Pre-processor Directive
Link Section
Definition Section
Declaration Part
Executable Part
1. Download a full-fledged IDE like Turbo C or Microsoft Visual C++, which comes along
First C Program
Before starting the abcd of C language, you need to learn how to write, compile and run the first c
program.
To write the first c program, open the C console and write the following code:
#include <stdio.h>
int main(){
printf("Hello C Language");
return 0;
#include <stdio.h> includes the standard input output library functions. The printf() function is
defined in stdio.h .
To compile and run a C language program, you need a C compiler. To setup a C language
compiler in your Computer/laptop, there are two ways:
Like any other function, main is also a function but with a special
characteristic that the program execution always starts from the ‘main’.
‘int’ and ‘void’ are its return type. So, let’s discuss all of the three one by
one.
void main – The ANSI standard says "no" to the ‘void main’ and thus
using it can be considered wrong. One should stop using the ‘void
main’ if doing so.
int main – ‘int main’ means that our function needs to return some
integer at the end of the execution and we do so by returning 0 at the
end of the program. 0 is the standard for the “successful execution of
the program”.
main – In C89, the unspecified return type defaults to int. So, main is
equivalent to int main in C89. But in C99, this is not allowed and thus
one must use int main.
The meaning of C language keywords has already been described in the C compiler.
Thus, keywords cannot be used as Variables names because that would try to change the
existing meaning of the keyword, which is not allowed.
Example of keyword:
int marks;
List of Keywords in C:
do if static while
Note:
A keyword name cannot be used as a variable name.
Keywords must be written in lower case.
It specifies the type/kind of entity.
Data Types in C
A data type specifies the type of data that a variable can store such as integer, floating, character, etc.
Data types in c refer to an extensive system used for declaring variables or functions of different
types.
Following are the examples of some very common data types used in C:
char: The most basic data type in C.
It stores a single character and requires a single byte of memory in almost all compilers.
float: It is used to store decimal numbers (numbers with floating point value) with single
precision.
double: It is used to store decimal numbers (numbers with floating point value) with
double precision.
Different data types also have different ranges upto which they can store numbers. These
ranges may vary from compiler to compiler. Below is list of ranges along with the
memory requirement and format specifiers on 32 bit gcc compiler.
Memory Format
Data Type (bytes) Range Specifier
unsigned short
int 2 0 to 65,535 %hu
Memory Format
Data Type (bytes) Range Specifier
-2,147,483,648 to
int 4 2,147,483,647 %d
-2,147,483,648 to
long int 4 2,147,483,647 %ld
unsigned long
int 4 0 to 4,294,967,295 %lu
unsigned long 0 to
long int 8 18,446,744,073,709,551,615 %llu
float 4 %f
double 8 %lf
Variables in C
Variable in C
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.
In other words, variable is a name which is used to store a value of any type during program
execution.
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.
Variable Declaration:
type variable_name;
or for multiple variables:
type variable1_name, variable2_name, variable3_name;
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:
float f=20.8;
char c='A';
int a;
int _ab;
int a30;
int 2;
int a b;
int long;
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
Local Variable
A variable that is declared inside the function or block is called a local variable.
1. void function1(){
2. int x=10;//local variable
3. }
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.
void function1(){
Static Variable
A variable that is declared with the static keyword is called static variable.
void function1(){
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
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.
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(){
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
program1.c
#include "myfile.h"
#include <stdio.h>
void printValue(){
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.
Defining Constants:
In C/C++ program we can define constants in two ways as shown below:
1. Using #define preprocessor directive
2. Using a const keyword
Literals: The values assigned to each constant variables are referred to as the literals.
Generally, both terms, constants and literals are used interchangeably. For eg, “const int =
5;“, is a constant expression and the value 5 is refered to as constant integer literal.
This directive is used to declare an alias name for existing variable or any value. We can
use this to declare a constant as shown below:
Example
#include<stdio.h>
#define val 10
#define floatVal 4.5
#define charVal 'G'
int main()
{
printf("Integer Constant: %d\n",val);
printf("Floating point Constant: %.1f\n",floatVal);
printf("Character Constant: %c\n",charVal);
return 0;
}
using a const keyword: Using const keyword to define constants is as simple as defining
variables, the difference is you will have to precede the definition with a const keyword.
#include <stdio.h>
int main()
{
// int constant
const int intVal = 10;
// Real constant
const float floatVal = 4.14;
// char constant
const char charVal = 'A';
// string constant
const char stringVal[10] = "ABC";
return 0;
}
Identifier Variable
Identifier is the name given to a variable, function While, variable is used to name a memory location which
etc. stores data.
An identifier can be a variable, but not all All variable names are identifiers.
indentifiers are variables.
Example:
Example:
// int variable
// a variable
int a;
int studytonight;
// float variable
// or, a function
float a;
int studytonight() {
..
C Character set
In C language characters are grouped into the following categories,
Letters(all alphabets a to z & A to Z).
Digits (all digits 0 to 9).
Special characters, ( such as colon :, semicolon ;, period ., underscore _,
ampersand & etc).
White spaces.
What Is Console In C
A console is an operating system window through which a user can communicate with the
operating system or we can say a console is an application in which we can give text as an input
from the keyboard and get the text as an output from the computer end.
printf() and scanf() in C
The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt
library functions, defined in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given statement to the console.
1. printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the console.
1. scanf("format string",argument_list);
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
return 0;
Output
enter a number:5
cube of number is:125
The scanf("%d",&number) statement reads integer number from the console and stores the given value
in number variable.
The printf("cube of number is:%d ",number*number*number) statement prints the cube of number
on the console.
#include<stdio.h>
int main(){
int x=0,y=0,result=0;
scanf("%d",&x);
scanf("%d",&y);
result=x+y;
return 0;
Output
You must be wondering what is the purpose of %d inside the scanf() or printf() functions.
It is known as format string and this informs the scanf() function, what type of input to
expect and in printf() it is used to give a heads up to the compiler, what type of output to
expect.
int i = printf("studytonight");
getchar() & putchar() functions
The getchar() function reads a character from the terminal and returns it as an integer.
This function reads only single character at a time. You can use this method in a loop in
case you want to read more than one character.
The putchar() function displays the character passed to it on the screen and returns the
same character.
This function too displays only a single character at a time. In case you want to display
more than one characters, use putchar() method in a loop.
#include <stdio.h>
void main( )
int c;
printf("Enter a character");
/*
store it in variable c
*/
c = getchar();
/*
in variable c
*/
putchar(c);
}
C printf and scanf functions:
printf() and scanf() functions are inbuilt library functions in C programming language which are
available in C library by default. These functions are declared and related macros are defined in
“stdio.h” which is a header file in C language.
We have to include “stdio.h” file as shown in below C program to make use of these printf() and
scanf() library functions in C language.
1. PRINTF() FUNCTION IN C LANGUAGE:
In C programming language, printf() function is used to print the (“character, string, float, integer,
octal and hexadecimal values”) onto the output screen.
We use printf() function with %d format specifier to display the value of an integer variable.
Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double
and %x for hexadecimal variable.
To generate a newline,we use “\n” in C printf() statement.
Note:
C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf().
All characters in printf() and scanf() functions must be in lower case.
1 #include <stdio.h>
2 int main()
3 {
4 char ch;
5 char str[100];
7 scanf("%c", &ch);
8 printf("Entered character is %c \n", ch);
10 scanf("%s", &str);
12 }
OUTPUT :
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hai
Entered string is hai
The format specifier %d is used in scanf() statement. So that, the value entered is received as an
integer and %s for string.
Ampersand is used before variable name “ch” in scanf() statement as &ch.
It is just like in a pointer which is used to point to the variable. For more information about how
pointer works, please click here.
KEY POINTS TO REMEMBER IN C PRINTF() AND SCANF():
1. printf() is used to display the output and scanf() is used to read the inputs.
2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
3. All syntax in C language including printf() and scanf() functions are case sensitive.
getch():
getch() is a nonstandard function and is present in conio.h header file which is mostly used by MS-
DOS compilers like Turbo C. It is not part of the C standard library or ISO C
Like above functions, it reads also a single character from keyboard. But it does not use
any buffer, so the entered character is immediately returned without waiting for the enter
key.
Syntax:
int getch();
getche()
Like getch(), this is also a non-standard function present in conio.h. It reads a single
character from the keyboard and displays immediately on output screen without waiting
for enter key.
Syntax:
int getche(void);
#include <stdio.h>
#include <conio.h>
// Example for getche() in C
int main()
{
printf("%c", getche());
return 0;
}
Operators in C Language
C language supports a rich set of built-in operators.
An operator is a symbol that tells the compiler to perform a certain mathematical or logical
manipulation.
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Conditional operators
Special operators
Arithmetic operators
C supports all the basic arithmetic operators. The following table shows all the basic
arithmetic operators.
I. Unary Operators: Operators that operates or works with a single operand are
unary operators. For example: (++ , –-)
II. Binary Operators: Operators that operates or works with two operands are
binary operators. For example: (+ , – , * , /)