Unit 1 Introduction To C
Unit 1 Introduction To C
Unit 1 Introduction To C
Introduction to C Programming
C is a High-Level Programming Language Used to Create High Level Program. It is a
General Purpose and Structured Programming. Developed By Dennis Ritchie at AT & T Bell
Laboratories In 1972 In USA. It Is also Called as Procedure Oriented Programming language.
It Is So Popular Language Because It Is Reliable, Simple and Easy to Use. C Language Is
Called "Compiled Languages" Since the Source Code Must First Be Compiled in Order to Run.
Why use C?
C was initially used for system development work, particularly the programs that make-
up the operating system. C was adopted as a system development language because it produces
code that runs nearly as fast as the code written in assembly language.
Some examples of the use of C might be −
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Network Drivers
• Modern Programs
• Databases
• Language Interpreters
• Utilities
History of C
ALGOL (Algorithmic Language) was the first computer programming language to use
a block structure, and it was introduced in 1960. In 1967, Martin Richards developed a
language called BCPL (Basic Combined Programming Language). BCPL was derived from
ALGOL. In 1970, Ken Thompson created a language using BCPL called B. Both BCPL and B
programming languages were typeless. After that, C was developed using BCPL and B by
Dennis Ritchie at the Bell lab in 1972. So, in terms of history of C language, it was used in
mainly academic environments, but at long last with the release of many C compilers for
commercial use and the increasing popularity of UNIX, it began to gain extensive support
among professionals.
Features of C
Simple and Efficient
The C Language is a simple language that is easy to learn even for a beginner and is
super-efficient to use both in terms of the time of development and time of execution.
Portability
C Language program runs the same way everywhere. It means if you have written a
simple C program like a program to find sum of N numbers in C, on your Windows operating
system and then compiled it and run it, you can then take the compiled code and run it on any
other operating system or machine, like, Linux or macOS, etc., your C program will always
return the same result.
Powerful
It has a broad range of features like support for many data types, operators, keywords,
etc., that allows the structuring of code using functions, loops, decision-making statements, etc.
Then there are complex data structures like structures, arrays, etc., and pointers, which makes
C quite resourceful.
Syntax-based Language
There are proper rules for writing the code, and the C language strictly follows them.
If you write anything that is not allowed, you will get a compile-time error, which happens
when the compiler is unable to compile your code because of some incorrect code syntax.
Compiled Language
The C language uses a Compiler to compile the code into object code, which is nothing
but machine code that the computer understands. Hence to run a C language program we have
to install a C language compiler first.
2. Link section: The link section provides instructions to the compiler to link functions from
the system library such as using the #include directive.
3. Definition section: The definition section defines all symbolic constants such using the
#define directive.
4. Global declaration section: There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global declaration
section that is outside of all the functions. This section also declares all the user-defined
functions.
5. main () function section: Every C program must have one main function section. This
section contains two parts; declaration part and executable part
1. Declaration part: The declaration part declares all the variables used in the executable part.
2. Executable part: There is at least one statement in the executable part. These two parts
must appear between the opening and closing braces. The program execution begins at the
opening brace and ends at the closing brace. The closing brace of the main function is the
logical end of the program. All statements in the declaration and executable part end with a
semicolon.
Example
/*First c program*/
#include <stdio.h>
int main ()
{
printf ("welcome to c Programming language.\n");
return 0;
}
Output: welcome to c programming language.
Compilation process in C
Compilation process in C is converting an understandable human code into a Machine
understandable code and checking the syntax and semantics of the code to determine any
syntax errors or warnings present in our C program. Suppose we want to execute our C Program
written in an IDE (Integrated Development Environment). In that case, it has to go through
several phases of compilation (translation) to become an executable file that a machine can
understand.
The Preprocessor
The Preprocessor accepts source code as input and is responsible for
• removing comments
• Interpreting special preprocessor directives denoted by #. For example
• #include -- includes contents of a named file. Files usually called header files.
e.g #include <math.h> -- standard library maths file.
#include <stdio.h> -- standard library I/O file
• #define -- defines a symbolic name or constant. Macro substitution. #define
MAX_ARRAY_SIZE 100
C Compiler
The C compiler translates source to assembly code. The source code is received from
the preprocessor.
Assembler
The assembler creates object code. On a UNIX system you may see files with a .o
suffix (.OBJ on MSDOS) to indicate object code files.
Link Editor
If a source file references library functions or functions defined in other source files
the link editor combines these functions (with main()) to create an executable file.
C tokens
• C tokens are the basic buildings blocks in C language which are constructed together to
write a C program.
• Each and every smallest individual unit in a C program is known as C tokens.
• C tokens are of six types. They are
Keywords (eg: int, while),
Identifiers (eg: main, total),
Constants (eg: 10, 20),
Strings (eg: ―total‖, ―hello‖),
Special symbols (eg: (), {}),
Operators (eg: +, /,-,*)
C keywords
There are certain words reserved for doing specific task, these words are known as
reserved word or keywords. These words are predefined and always written in lower case or
small letter. These keywords cann’t be used as a variable name as it assigned with fixed
meaning.
Some examples are int, short, signed, unsigned, default, volatile, float, long, double,
break, continue, typedef, static, do, for, union, return, while, do, extern, register, enum, case,
goto, struct, char, auto, const etc.
Identifiers
Identifiers are user defined word used to name of entities like variables, arrays,
functions, structures etc. Rules for naming identifiers are:
1) name should only consists of alphabets (both upper and lower case), digits and underscore
(_) sign.
2) first characters should be alphabet or underscore
3) name should not be a keyword
4) since C is a case sensitive, the upper case and lower case considered differently, for example
code, Code, CODE etc. are different identifiers.
5) identifiers are generally given in some meaningful name such as value, net_salary, age, data
etc. An identifier name may be long, some implementation recognizes only first eight
characters, most recognize 31 characters. ANSI standard compiler recognize 31 characters.
Some invalid identifiers are 5cb, int, res#, avg no etc.
Constants
A C constant refers to the data items that do not change their value during the program
execution. Several types of C constants that are allowed in C are:
CONSTANT EXAMPLE
Decimal Constant 10, 20, 450 etc.
Real or Floating-point Constant 10.3, 20.2, 450.0 etc.
Octal Constant 021, 033, 046 etc.
Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.
Character Constant ‘a’, ‘b’, ‘x’, etc.
String Constant “C”, “C program” etc.
1) C const keyword
The const keyword is used to define constant in C programming.
const float PI=3.14;
Now, the value of PI variable can’t be changed.
#include<stdio.h>
int main()
{
const float PI=3.14;
printf(“The value of PI is: %f”,PI);
return 0;
}
Output
The value of PI is: 3.140000
2) C #define preprocessor
The #define preprocessor is also used to define constant.
Syntax:
#define token value
Example
#include<stdio.h>
#define PI 3.14
int main()
{
printf(“The value of PI is: %f”,PI);
return 0;
}
Output
The value of PI is: 3.140000
Data types
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
Data types refer to an extensive system used for declaring variables or functions of different
types before its use. The type of a variable determines how much space it occupies in storage
and how the bit pattern stored is interpreted. The value of a variable can be changed any time.
C has the following 4 types of data types
• basic built-in data types: int, float, double, char
• Enumeration data type: enum
• Derived data type: pointer, array, structure, union
• Void data type: void
Below is a list of ranges along with the memory requirement and format specifiers on the 32-
bit GCC compiler.
Data Type Size Range Format
(bytes) Specifier
Symbolic constant
Symbolic constant is a name that substitute for a sequence of characters and,
characters may be numeric, character or string constant. These constant are generally defined
at the beginning of the program as #define name value , here name generally written in upper
case for example
#define MAX 10
#define CH ‘b’
#define NAME “sony”
Variables
A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in C has a specific type, which determines the size and layout of the
variable's memory; the range of values that can be stored within that memory; and the set of
operations that can be applied to the variable.
Example
int a;
float b;
Variable initialization:
When we assign any initial value to variable during the declaration, is called initialization of
variables. When variable is declared but contain undefined value then it is called garbage value.
The variable is initialized with the assignment operator such as
data type variable name=value;
Example: int a=20;
Format Specifiers
• To print values of different data types using the printf() statement and while taking input
using the scanf() function, it is mandatory to use format specifiers.
• It is a way to tell the compiler what type of data is in a variable.
• Some examples are %c, %d, %f, etc.
unsigned int %u
int %d
signed char %c
unsigned char %c
float %f
double %lf
1. printf():
In C, printf() is a built-in function used to display values like numbers, characters, and
strings on the console screen. It’s pre-defined in the stdio.h header, allowing easy output
formatting in C programs.
Syntax 1
printf(“Format Specifier”, var1, var2, …., varn);
Example
// C program to implement
// printf() function
#include <stdio.h>
int main()
{
// Declaring an int type variable
int a;
return 0;
}
Output
20
Syntax 2:
printf(“Enter the text which you want to display”);
Example
// C program to implement
// printf() function
#include <stdio.h>
int main()
{
// Displays the string written
// inside the double quotes
printf("This is a string");
return 0;
}
Output
This is a string
2. scanf():
In C, scanf() is a built-in function for reading user input from the keyboard. It can read
values of various data types like integers, floats, characters, and strings. scanf() is a pre-defined
function declared in the stdio.h header file. It uses the & (address-of operator) to store user
input in the memory location of a variable.
Syntax
scanf(“Format Specifier”, &var1, &var2, …., &varn);
Example
// C program to implement
// scanf() function
#include <stdio.h>
int main()
{
int num1;
return 0;
}
Output
Enter a integer number: 56
You have entered 56
Why “Unformatted”?:
They are referred to as “unformatted” I/O functions because they do not support format
specifiers. Unlike formatted I/O functions like printf() and scanf(), you cannot use format
specifiers to control the formatting of the data. They display or read data as-is without
formatting options.
• putch()
• getch():
1. getch():
In C, getch() reads a single character from the keyboard without displaying it on the
screen. It immediately returns without requiring the user to press the Enter key. This function
is declared in the conio.h header file and is often used for controlling screen display.
Syntax
getch();
or
variable-name = getch();
Example
// C program to implement getch() function
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
2. getche():
In C, getche() reads a single character from the keyboard, displays it on the screen, and
immediately returns without requiring the user to press the Enter key. This function is declared
in the conio.h header file.
Syntax
getche();
or
variable_name = getche();
Example
// C program to implement the getche() function
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
3. getchar():
In C, getchar() reads a single character from the keyboard and waits until the Enter key
is pressed. It processes one character at a time. This function is declared in the stdio.h header
file
Syntax
Variable-name = getchar();
Example
// C program to implement the getchar() function
#include <conio.h>
#include <stdio.h>
int main()
{
// Declaring a char type variable
char ch;
printf("Enter the character: ");
Output
Enter the character: a
a
4. putchar():
In C, putchar() is used to display a single character at a time, either by passing the
character directly or by using a variable that stores the character. This function is declared in
the stdio.h header file.
Syntax
putchar(variable_name);
Example
// C program to implement the putchar() function
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
return 0;
}
Output
Enter any character: Z
Z
5. gets():
In C, gets() reads a group of characters or strings from the keyboard, and these
characters are stored in a character array. It allows you to input space-separated texts or strings.
This function is declared in the stdio.h header file.
Syntax
char str[length of string in number]; //Declare a char type variable of any length
gets(str);
Example
// C program to implement the gets() function
#include <conio.h>
#include <stdio.h>
int main()
{
// Declaring a char type array of length 50 characters
char name[50];
}
Output
Please enter some texts: jgi bca
You have entered: jgi bca
6. puts():
In C programming, puts() is used to display a group of characters or strings that are
already stored in a character array. This function is declared in the stdio.h header file.
Syntax
puts(identifier_name );
Example
// C program to implement the puts() function
#include <stdio.h>
int main()
{
char name[50];
printf("Enter your text: ");
// Displays string
puts(name);
return 0;
}
Output
Enter your text: jgi bca
Your text is: jgi bca
7. putch():
In C, putch() is used to display a single character provided by the user, and it prints the
character at the current cursor location. This function is declared in the conio.h header file
Syntax
putch(variable_name);
Example
// C program to implement the putch() functions
#include <conio.h>
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character:\n ");
Output
Enter any character:
Entered character is: d
2 They will support format specifiers. They will support not format specifiers.
In Formatted I/0 Functions, we can use Here we can use only character and string
4
all data types. data types