The document provides an introduction to the C programming language, covering its history, uses, basic concepts, and key functions. It discusses how C was created at Bell Labs to develop the UNIX operating system, its widespread adoption, and importance. The document outlines common C data types, control flow statements like conditionals and loops, functions and their structure, and input/output functions like printf, scanf, gets and puts. It provides examples of basic C programs and how functions, conditionals, and I/O are implemented.
2. Syllabus
Overview of programming
Program control flow
Importance of C
Structure of C functions
Data Types
Standard Input-Output functions
Conditional statements
3. Overview of programming
C is a general-purpose, high-level language that was originally developed by
Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was
originally first implemented on the DEC PDP-11 computer in 1972.
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available
description of C, now known as the K&R standard.
The UNIX operating system, the C compiler, and essentially all UNIX application
programs have been written in C. C has now become a widely used professional
language for various reasons −
Easy to learn
Structured language
It produces efficient programs
It can handle low-level activities
It can be compiled on a variety of computer platforms
4. 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.
Today's most popular Linux OS and RDBMS MySQL have been written in C.
5. Program control flow
Control statements enable us to specify the flow of program control; ie, the
order in which the instructions in a program must be executed. They make it
possible to make decisions, to perform tasks repeatedly or to jump from one
section of code to another.
There are four types of control statements in C:
Decision making statements
Selection statements
Iteration statements
Jump statements
6. Importance of C
As a middle-level language, C combines the features of both high-level and
low-level languages. It can be used for low-level programming, such as
scripting for drivers and kernels and it also supports functions of high-level
programming languages, such as scripting for software applications etc.
C is a structured programming language which allows a complex program to
be broken into simpler programs called functions. It also allows free
movement of data across these functions.
Various features of C including direct access to machine level hardware APIs,
the presence of C compilers, deterministic resource use and dynamic memory
allocation make C language an optimum choice for scripting applications and
drivers of embedded systems.
C language is case-sensitive which means lowercase and uppercase letters are
treated differently.
7. Cont….
C is highly portable and is used for scripting system applications which form a
major part of Windows, UNIX, and Linux operating system.
C is a general-purpose programming language and can efficiently work on
enterprise applications, games, graphics, and applications requiring calculations,
etc.
C language has a rich library which provides a number of built-in functions. It also
offers dynamic memory allocation.
C implements algorithms and data structures swiftly, facilitating faster
computations in programs. This has enabled the use of C in applications requiring
higher degrees of calculations like MATLAB and Mathematica.Riding on these
advantages, C became dominant and spread quickly beyond Bell Labs replacing
many well-known languages of that time, such as ALGOL, B, PL/I, FORTRAN,
etc. C language has become available on a very wide range of platforms, from
embedded microcontrollers to supercomputers.
8. Structure of C functions
A function is a group of statements that together perform a task. Every C program
has at least one function, which is main(), and all the most trivial programs can
define additional functions.
You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division is such that each
function performs a specific task.
A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can
call. For example, strcat() to concatenate two strings, memcpy() to copy one
memory location to another location, and many more functions.
A function can also be referred as a method or a sub-routine or a procedure, etc.
9. Defining a Function
The general form of a function definition in C programming language is as follows
−
return_type function_name( parameter list ) {
body of the function
}
A function definition in C programming consists of a function header and a
function body. Here are all the parts of a function −
Return Type − A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
Function Name − This is the actual name of the function. The function name and
the parameter list together constitute the function signature.
10. Cont….
Parameters − A parameter is like a placeholder. When a function is invoked,
you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
Function Body − The function body contains a collection of statements that
define what the function does.
11. Example
Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
12. Data Types
A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
13. There are the following data types
in C language
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
14. 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.
int: As the name suggests, an int variable is used to store an integer.
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.
15. Standard Input-Output functions
Input means to provide the program with some data to be used in the
program and Output means to display data on screen or write the data to a
printer or a file.
C programming language provides many built-in functions to read any given
input and to display data on screen when there is a need to output the result.
Before moving forward with input and output in C language, check these
topics out to understand the concept better :
Data Types in C
Variables in C
Function in C
16. scanf() and printf() functions
The standard input-output header file, named stdio.h contains the definition of
the functions printf() and scanf(), which are used to display output on screen and
to take input from user respectively.
#include<stdio.h>
void main()
{
// defining a variable
int i;
/*
displaying message on the screen
asking the user to input a value
*/
17. Cont….
printf("Please enter a value...");
/*
reading the value entered by the user
*/
scanf("%d", &i);
/*
displaying the number as output
*/
printf( "nYou entered: %d", i);
}
18. Cont….
Format String Meaning
%d Scan or print an integer as
signed decimal number
%f Scan or print a floating point
number
%c To scan or print a character
%s To scan or print a character
string. The scanning ends at
whitespace.
19. 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.
20. EXAMPLE
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character");
/*
Take a character as input and
store it in variable c
*/
21. Cont…
c = getchar();
/*
display the character stored
in variable c
*/
putchar(c);
}
22. gets() & puts() functions
The gets() function reads a line from stdin(standard input) into the buffer
pointed to by str pointer, until either a terminating newline or EOF (end of
file) occurs. The puts() function writes the string str and a trailing newline to
stdout.
str → This is the pointer to an array of chars where the C string is stored.
(Ignore if you are not able to understand this now.)
#include<stdio.h>
void main()
{
/* character array of length 100 */
char str[100];
24. Conditional statements
Conditional Statements in C programming are used to make decisions based
on the conditions.
Conditional statements execute sequentially when there is no condition
around the statements.
If you put some condition for a block of statements, the execution flow may
change based on the result evaluated by the condition.
This process is called decision making in 'C.‘
In 'C' programming conditional statements are possible with the help of the
following two constructs:
1. If statement
2. If-else statement
25. Cont….
If statement
It is one of the powerful conditional statement. If statement is responsible for
modifying the flow of execution of a program. If statement is always used
with a condition. The condition is evaluated first before executing any
statement inside the body of If. The syntax for if statement is as follows:
if (condition)
instruction;
The condition evaluates to either true or false. True is always a non-zero
value, and false is a value that contains zero. Instructions can be a single
instruction or a code block enclosed by curly braces { }.
26. EXAMPLE
#include<stdio.h>
int main()
{
int num1=1;
int num2=2;
if(num1<num2) //test-condition
{
printf("num1 is smaller than num2");
}
return 0;
}
29. The general form of if-else is as follows:
if (test-expression)
{
True block of statements
}
Else
{
False block of statements
}
Statements;
30. EXAMPLE
#include<stdio.h>
int main()
{
int num=19;
if(num<10)
{
printf("The value is less than 10");
}
else
{
printf("The value is greater than 10");
}
return 0;
}