C Programming Study Material
C Programming Study Material
Avijit: 7685817762
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 Basic commands
Explanation
#include <stdio.h>
main()
/* some comments */
printf(Hello World! );
getch();
return 0;
Mehak: 8620967459
Avijit: 7685817762
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.
Create
Compile
Execute or Run
Get the Output
EXAMPLE:
OUTPUT
Mehak: 8620967459
Avijit: 7685817762
You can compare all the sections of a C program with the below C program.
OUTPUT:
Mehak: 8620967459
Avijit: 7685817762
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
Definition Section
In this section, variables are defined and values are set to these
variables.
Main function
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
Output:
Note:
5
Mehak: 8620967459
Avijit: 7685817762
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
a)if statements:
The general form of if statement looks like this:
Mehak: 8620967459
Avijit: 7685817762
Output:
Mehak: 8620967459
Avijit: 7685817762
Output:
Mehak: 8620967459
Avijit: 7685817762
EXAMPLE:
Output:
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
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:
In while loop control statement, loop is executed until condition becomes false.
10
Mehak: 8620967459
Avijit: 7685817762
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
Output:
do while
Loop is executed for first time irrespective of the condition.
After executing while loop for first time, then condition is
checked.
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
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
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 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.
#include<stdio.h>
brazil()
{
printf("\nI am in Brazil ");
}
india()
{
printf("\nI am in India ");
}
main()
{
printf("\nI am in main ");
brazil();
india();
}
I am in main
I am in Brazil
I am in India
15
Mehak: 8620967459
Avijit: 7685817762
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.
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.
Output
16
Mehak: 8620967459
Avijit: 7685817762
Output
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
Output:
Note:
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
Example:
int arr[2][2];
int arr[2][2] = {1,2, 3, 4};
18
Mehak: 8620967459
Avijit: 7685817762
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( )
cos( )
tan( )
sinh( )
cosh( )
log( )
log10( )
exp( )
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( )
19