Вы находитесь на странице: 1из 19

Mehak: 8620967459

Avijit: 7685817762

A.M. Physics Lab


WHAT IS C ?

The C programming language is a structure oriented programming language, developed at


Bell Laboratories in 1972 by Dennis Ritchie. In late seventies C began to replace the more familiar
languages of that time like PL/I, ALGOL etc. C language was invented for implementing UNIX
operating system.
In 1983, the American National Standards Institute (ANSI) established a committee to
provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or
ANSI C, was completed late 1988.

C - PROGRAMMING BASICS:
This C programming basics section explains a simple Hello World C program. Also, it
covers below basic topics as well, which are to be known by any C programmer before writing a C
program.
C programming basic commands to write a C program.
A simple C program with output and explanation.
Steps to write C programs and get the output.

Creation, Compilation and Execution of a C program.How to install C compiler and IDE tool
to run C programming codes.

C PROGRAMMING BASICS TO WRITE A C PROGRAM:


Below are few commands and syntax used in C programming to write a simple C program. Lets see
all the sections of a simple C program line by line.

C Basic commands

Explanation

#include <stdio.h>

This is a preprocessor command that includes standard


input output header file(stdio.h) from the C library before
compiling a C program.

main()

This is the main function from where execution of any C


program begins.

This indicates the beginning of the main function.

/* some comments */

whatever is given inside the command /* */ in any C


program, wont be considered for compilation and
execution.

printf(Hello World! );

printf command prints the output onto the screen.

getch();

This command waits for any character input from


keyboard.

return 0;

This command terminates C program (main function) and


returns 0.
This indicates the end of the main function.

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

A header file is a file with extension .h which contains C function declarations and macro
definitions to be shared between several source files. There are two types ofheader files: the files that
the programmer writes and the files that comes with your compiler.

STEPS TO WRITE C PROGRAMS AND GET THE OUTPUT:


Below are the steps to be followed for any C program to create and get the output. This is common to
all C program and there is no exception whether its a very small C program or very large C program.

Create
Compile
Execute or Run
Get the Output

EXAMPLE:

OUTPUT

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

CREATION, COMPILATION AND EXECUTION OF A C PROGRAM:


If you want to create, compile and execute C programs by your own, you have to install C
compiler in your machine. Then, you can start to execute your own C programs in your machine.

BASIC STRUCTURE OF A C PROGRAM:


Structure of C program is defined by set of rules called protocol, to be followed by
programmer while writing C program. All C programs are having sections/parts which are mentioned
below.
Documentation section
Link Section
Definition Section
Global declaration section
Function prototype declaration section
Main function

User defined function definition section.

You can compare all the sections of a C program with the below C program.

OUTPUT:

DESCRIPTION FOR EACH SECTION OF THE C PROGRAM:

Let us see about each section of a C basic program in detail below.


3

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

Please note that a C program maynt have all below mentioned sections except main function
and link sections.
Also, a C program structure maynt be in below mentioned order.

Sections
Documentation section

Description
We can give comments about the program, creation or modified
date, author name etc. in this section. The characters or words
or anything which are given between /* and */, wont be
considered by C compiler for compilation process.These will be
ignored by C compiler during compilation.
Example: /* comment line1
comment line2 */

Link Section

Header files that are required to execute a C program are


included in this section.

Definition Section

In this section, variables are defined and values are set to these
variables.

Global declaration section

Global variables are defined in this section. When a variable is


to be used throughout the program, can be defined in this
section.

Function prototype declaration


section

Function prototype gives many information about a function


like return type, parameter names used inside the function.

Main function

Every C program is started from main function and this


function contains two major sections called declaration section
and executable section.

User defined function section

User can define their own functions in this section which


perform particular task as per the user requirement.

KEY POINTS TO REMEMBER IN C PROGRAMMING BASICS:


1. C programming is a case sensitive programming language.
2. Each C programming statement is ended with semicolon (;) which are referred as statement
terminator.
3. printf() command is used to print the output onto the screen.

4.

C programs are compiled using C compilers and displays output when executed.

main() Function:
Any C program is nothing but a combination of functions. main()is one of them. Empty
parentheses after main are necessary.
The set of statements belonging to a function are enclosed within pair of braces.
For example,
main()
{
statement 1;
4

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


statement 2;
statement 3;
}

printf() and scanf() Function:


printf() is a function which is used to print on the screen the value contained in variable.
The general form of printf() statement is,
printf(<format string>, <list of variables>);
<format string> could be,
%f
for printing real values
%d
for printing integer values
%c
for printing character values
%lf
for double.
If we want to make a provision for supplying the values of variables through the keyboard, a
statement is used called scanf() should be used. This is illustrated in the program shown below.

Output:

Note:
5

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

1. 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.
2. 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.
3. 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.
4. To generate a newline,we use \n in C printf() statement.
5. printf() is used to display the output and scanf() is used to read the inputs.
6. printf() and scanf() functions are declared in stdio.h header file in C library.
7. All syntax in C language including printf() and scanf() functions are case sensitive.

KEYWORDS IN C LANGUAGE:
Keywords are pre-defined words in a C compiler.
Each keyword is meant to perform a specific function in a C program.
Since keywords are referred names for compiler, they cant be used as variable name.
C language supports 32 keywords which are given below.

auto
const
double
float
int
short
struct

break
continue
else
for
long
signed
switch

case
default
enum
goto
register
sizeof
typedef

char
do
extern
if
return
static
union

unsigned

void

volatile

while

Decision Control statement:


In decision control statements (if-else and nested if), group of statements are executed when
condition is true. If condition is false, then else part statements are executed.
There are 3 types of decision making control statements in C language. They are,
a) if statements
b) if else statements
c) nested if statements.

a)if statements:
The general form of if statement looks like this:

if(this condition is true)


{
Do this;
}

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


Example:

Output:

b)if else statements:


The general form of if statement looks like this:
if (condition)
{
Statement1;
Statement2;
}
else
{
Statement3;
Statement4;
}
In these type of statements, group of statements are executed when condition is true. If condition is
false, then else part statements are executed.

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


EXAMPLE:

Output:

c) nested if-else statements:


The general form ofnested if-else statement looks like this:
if (condition1)
{
Do this;
}
else
{
if(condition2)
do this;
else
{
Do this;
And do this;
}
}
8

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

In nested if control statement, if condition1 is false, then condition2 is checked and


statements are executed if it is true. If condition2 also gets failure, then else part is executed.

EXAMPLE:

Output:

Loop control statements:


Loop control statements in C are used to perform looping operations until the given condition
is true. Control comes out of the loop statements once condition becomes false.
There are 3 types of loop control statements in C language. They are,
A)
B)
C)
D)

for loop
while loop
do-while loop

A) for loop:
The general form of for loop statement is as under:
for(initialize counter ; test counter ; increment counter)
{
Do this;
Do this;
And do this;
}

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

In for loop control statement, loop is executed until condition becomes false.

EXAMPLE:

Output:

B) while loop:
The general form of while loop statement is as under:

initialize loop counter;


while(test loop counter using a condition)
{
Do this;
And this;
increment loop counter;
}

In while loop control statement, loop is executed until condition becomes false.

10

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


EXAMPLE:

Output:

C) do-while loop:
In do-while loop control statement, while loop is executed irrespective of the condition for first time.
Then 2nd time onwards, loop is executed until condition becomes false.
The do-while loop looks like this:

do
{
this;
and this;
and this;
}while(this condition is true)

There is a minor difference between the working of while and do-while loops. This difference is the
place where the condition is tested.The while tests the condition before executing any of the
statements within the while loop. As against this, the do-while tests the condition after having
executed the statements within the loop.

11

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


EXAMPLE:

Output:

DIFFERENCE BETWEEN while & do while LOOPS IN C LANGUAGE:


while

Loop is executed only


when condition is true.

do while
Loop is executed for first time irrespective of the condition.
After executing while loop for first time, then condition is
checked.

C Case control statements:


The statements which are used to execute only specific block of statements in a series of blocks are
called case control statements.
There are 4 types of case control statements in C language. They are,
A.
B.
C.
D.

switch
break
continue
goto

A. switchcase statements in C:
switch case statements are used to execute only specific case statements based on the switch
expression. Below is the syntax for switch case statement.

12

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


switch (expression)
{
case label1:
statements;
case label2:
statements;
default:
statements;
}

EXAMPLE PROGRAM FOR SWITCHCASE STATEMENT:

Output:
You are on right track
You are on wrong track
Reread textbook

B. break statements in C:
break statement is used to terminate the while loops, switch case loops and for loops from the
subsequent execution.

EXAMPLE:

13

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


Output:

C. continue statements in C:
continue statement is used to continue the next iteration of for loop, while loop
and do-while loops. So, the remaining statements are skipped within the loop
for that particular iteration.
EXAMPLE:

Output:

A. goto STATEMENT IN C:
goto statements is used to transfer the normal flow of a program to the specified label in the program.
Below is the syntax for goto statement in C.
{
.
go to label;
..
LABEL:
statements;
}
14

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


C Function:

A function is a self-contained block of statements that perform a coherent task of some kind.
Every C program can be thought of as a collection of these functions.
In other words, C functions are basic building blocks in a program. All C programs are
written using functions to improve re-usability, understandability and to keep track on them.
A large C program is divided into basic building blocks called C function. C function contains
set of instructions enclosed by { } which performs specific operation in a C program. Actually,
Collection of these functions creates a C program.

Follow the example-

#include<stdio.h>
brazil()
{
printf("\nI am in Brazil ");
}
india()
{
printf("\nI am in India ");
}
main()
{
printf("\nI am in main ");
brazil();
india();
}

The output of the above program when executed would be as under:

I am in main
I am in Brazil
I am in India

15

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


USES OF C FUNCTIONS:

C functions are used to avoid rewriting same logic/code again and again in a program.

There is no limit in calling C functions to make use of same functionality wherever required.

We can call functions any number of times in a program and from any place in a program.

A large C program can easily be tracked when it is divided into functions.

The core concept of C functions are, re-usability, dividing a big task into small pieces to
achieve the functionality and to improve understanding ability of very large C programs.

Function Declaration and Prototypes:


Any C function by default returns an int value. More specifically, whenever a call is made to a
function, the compiler assumes that this function would return a value of the type int. If we desire that
a function should return a value other than an int, then it is necessary to explicitly mention so in the
calling function as well as in the called function.
Suppose we want to find out square of a number using a function. This is how this simple
program would look like:

Output

Now ,we want to see another program with two variables:


Suppose ,
f(x,y)=2x+3y-1.
Find f(1.2, 1.5) using C Program.

16

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

Output

What are Arrays


C Array is a collection of variables belongings to the same data type. You can store group of
data of same data type in an array.

For understanding the arrays properly, let us consider the following program:
main( )
{
int x ;
x=5;
x = 10 ;
printf ( "\nx = %d", x ) ;
}

No doubt, this program will print the value of x as 10. Why so? Because when a value 10 is
assigned to x, the earlier value of x, i.e. 5, is lost. Thus, ordinary variables (the ones which we have
used so far) are capable of holding only one value at a time (as in the above example). However, there
are situations in which we would want to store more than one value at a time in a single variable.
Let us try to write a program to find total marks obtained by a class of 5 students in a test.

17

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab

Output:

Note:

Array might be belonging to any of the data types


Array size must be a constant value.
Always, Contiguous (adjacent) memory locations are used to store array elements in memory.
It is a best practice to initialize an array to zero or null while declaring, if we dont assign any
values to array.

TYPES OF C ARRAYS:
There are 2 types of C arrays. They are,
1. One dimensional array
2. Multi dimensional array
Two dimensional array
Three dimensional array
four dimensional array etc

1. ONE DIMENSIONAL ARRAY IN C:


Syntax : data-type arr_name[array_size];
Example:
int age [5];
int age[5]={0, 1, 2, 3, 4};

2. TWO DIMENSIONAL ARRAY IN C:

Two dimensional array is nothing but array of array.


syntax : data_type array_name[num_of_rows][num_of_column];

Example:
int arr[2][2];
int arr[2][2] = {1,2, 3, 4};
18

Mehak: 8620967459
Avijit: 7685817762

A.M. Physics Lab


INBUILT ARITHMETIC FUNCTIONS IN C
Function
Description
abs ( )

This function returns the absolute value of an integer. The absolute value of a
number is always positive. Only integer values are supported in C.

sin( )

This function is used to calculate sine value.

cos( )

Example: sinx is written as sin(x) in c program


This function is used to calculate cosine.

tan( )

Example: cosx is written as cos(x) in c program


This function is used to calculate tangent.

sinh( )

Example: tanx is written as tan(x) in c program


This function is used to calculate hyperbolic sine.

cosh( )

Example: sinhx is written as sinh(x) in c program


This function is used to calculate hyperbolic cosine.

log( )

Example: coshx is written as cosh(x) in c program


This function is used to calculate natural logarithm.

log10( )

Example: logx is written as log(x) in c program


This function is used to calculate base 10 logarithm.
Example: log10 is written as log10(x) in c program

exp( )

This function is used to calculate the exponential e to the xth power.


Example: e-x is written as exp(x) in c program

sqrt( )

This function is used to find square root of the argument passed to this function.
Example: 1 2 is written as sqrt(1-x*x) in c program

pow( )

This is used to find the power of the given number.


Example: x4 is written as pow(x,4) in c program

19

Вам также может понравиться