Professional Documents
Culture Documents
Page|2
CONTENT:
1. Introduction …………………………………………………….. 3
2. Data type……………………………………………………… .. 7
3. Conditional statement………………………………………… . 17
4. Loop…………………………………………………………… 25
5. Pointer…………………………………………………………..29
6. Function…………………………………………………………32
7. String…………………………………………………………… 38
8. Arrays……………………………………………………………41
9. Structure & union………………………………………………..44
10.Introduction to linux………………………………….......48
Page|4
Page|5
Page|6
Page|7
INTRODUCTION TO C
What is C ?
C is a general –purpose programming language that is
extremely popular, simple and flexible. It is machine
independent Structured programming language which
is used extensively in various applications.
‘C’ is a powerful programming language which is
strongly associated with UNIX operating system.
Features of C Programming
language:
modularity
Features of
modularity statically type
C middle level
language
general purpose language
language
languages, their performance rate gets down effectively. It’s fast because
statically typed languages are faster than dynamically typed languages.
• Modularity: The concept of storing C programming language code in the
form of libraries for further future uses known as modularity. C language has
its own library to solve common problems like in this we can use a particular
function by using header files stored in its library.
• Statically type: C programming language is a statically typed language .
meaning the type of the variable is checked at the time of compilation but not
at run time. Means each time a programmer type a program they have to
mention the type of variables used.
• General purpose language: From system programming to photo editing
software, C programming is used in various applications .some of the
common applications are
Operating system:Windows, linux, iOS , Android , OXS
Databases: PostgreSQL, Oracle, MySQL, MS SQL Server etc.
• Rich set of Built-in Operators: It is a diversified language with a set of
built-in operators which are used in writing complex or simplified C programs.
• Libraries with rich functions: Robust beginner coder to code with
ease.
• Middle-Level Language: As it a middle level language so it has
combined form of both capabilities of assembly language and features of high
level language.
P a g e | 10
Fundamentals of C
Digits 0-9
Identifiers: Identifiers are the fundamental building blocks of a program which are
P a g e | 14
Operator precedence:
1.Postfix/prefix increment (++) : the increment operator is used to
increase some value by 1.
2.Postfix/prefix decrement(--) : the decrement
P a g e | 18
P a g e | 19
P a g e | 20
P a g e | 21
P a g e | 22
P a g e | 23
{
P a g e | 27
P a g e | 28
P a g e | 29
P a g e | 30
P a g e | 31
if(num%2==0) printf(“even
number”); else printf(“odd
number”);
}
• Nested if statement:
The ANSI standard specifies that 15 levels of nesting must be supported. In
C ,an else statement always refers to the nearest if statement in the same
block and not already associated with if. For ex:
main()
{
int num;
printf(“Enter a number:”); scanf(“%d”,&num);
if(num > 0)
{
If(num %2 ==0)
printf(“even number”); else
printf(“odd number”);
}
else
{
if(num < 0)
printf(“negative number”); else
printf(“ number is zero”);
P a g e | 36
}
}
P a g e | 37
The conditional expressions are evaluated from the top downward. As soon
P a g e | 38
as a true
Condition is found, the statement associated with it is executed, and the rest
of the ladder is bypassed, if none of the conditions is true, then the final else
statement will be executed.
case constant2;
statement; break;
default:
statement; break;
}
FOR EXAMPLE:
Q. Create a C program to perform basic functions of an ATM using switch case.
void main()
{
P a g e | 39
}
break; case 3:
printf("\n ENTER THE AMOUNT TO DEPOSIT");
scanf("%lu", &deposit); amount = amount + deposit;
printf("YOUR BALANCE IS %lu", amount); break; case 4:
printf("\n THANK U USING ATM");
break; default:
printf("\n INVALID CHOICE");
}
printf("\n\n\n DO U WISH TO HAVE ANOTHER
TRANSCATION?(y/n):\n");
fflush(stdin);
scanf("%c", &transaction); if (transaction == 'n'||
transaction == 'N') k = 1;
} while
(!k);
printf("\n\n THANKS FOR USING OUT ATM SERVICE");
}
OUTPUT:
P a g e | 41
#include<stdio.h>
#include<conio.h>
#include<math.h> int main(){ int dat,
mont, years; printf("Enter the year : ");
scanf("%d", &years); printf("\n Enter the
month : "); scanf("%d", &mont);
printf("\n Enter the date : "); scanf("%d",
&dat); weekday(dat, mont, years);
return 0;
}
int weekday(int date, int month, int year) {
int dayWeek, yr, yd; yr = year % 100; yd = year / 100; printf("\nThe
Date Given is : %d / %d / %d \n\n", date, month, year); dayWeek = 1.25 * yr +
findm(month, year) + date - 2 * (yd % 4); dayWeek = dayWeek % 7; switch
(dayWeek){ case 0: printf("Day of Week of the Date is : Saturday"); break;
case 1: printf("Day of Week of the Date is : Sunday"); break;
case 2: printf("Day of Week of the Date is : Monday"); break;
case 3: printf("Day of Week of the Date is : Tuesday"); break;
case 4: printf("Day of Week of the Date is : Wednesday");
break;
case 5: printf("Day of Week of the Date is : Thursday");
break;
case 6: printf("Day of Week of the Date is : Friday"); break;
default: printf("The Given input data is wrong");
}
return 0;
}
int findm(int months, int yearss){ int
findmonth, leapyr;
P a g e | 42
LOOP
It executes a block of statements number of times until the condition
becomes False.
Loops are of two types:
Entry control loop Exit
control loop.
C programming provides us:
1.while
2.do-while
3.for loop
FOR LOOP:
Syntax:
for(conditions)
{
//statements
}
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of of times.
WHILE LOOP:
P a g e | 47
Syntax:
while(condition)
{
// statements
}
while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
The while loop can be thought of as a repeating if statement.
DO-WHILE LOOP :
Syntax: do
{
//statements
}
while(conditions);
do while loop is a control flow statement that executes a block of code at least once, and then either repeatedly
executes the block, or stops executing it, depending on a given boolean condition at the end of the block.
EXAMPLE:
Q. Create a C program to to print a pyramid of alphabets by using for loop .
#include <stdio.h>
void main()
{ int i,
j;
char alph = 'A'; int
n,blk;
int ctr = 1;
");
scanf("%d", &n);
printf(" ");
for (j = 0; j <= (ctr / 2); j++) {
printf("%c ", alph++);
P a g e | 49
}
P a g e | 50
P a g e | 51
P a g e | 52
11 12 13 14 15
#include <stdio.h>
int main()
{ int i, j, k, N;
printf("Enter N: ");
scanf("%d", &N);
k = 1;
printf("\n");
}
return 0;
}
P a g e | 53
P a g e | 54
P a g e | 55
P a g e | 56
P a g e | 57
POINTER
Pointer is a fundamental part of C. If you cannot use pointers properly then you
have basically lost all the power and flexibility that C allows. The secret to C is in
its use of pointers.
C uses pointers a lot because:
• It is the only way to express some computations.
• It produces compact and efficient code.
• Pointers provided an easy way to represent multidimensional arrays.
• Pointers increase the execution speed.
• Pointers reduce the length and complexity of program. C uses pointers
explicitly with arrays, structures and functions.
The unary operator & gives the “address of a variable''. The indirection or
dereference operator * gives the “contents of an
P a g e | 58
#include<stdio.h>
void main()
{
char *a; char
x='C'; a=&x;
printf("%c",*a);
(*a)++;
printf("%c",*a);
}
P a g e | 59
Q. Create a C program to store the address of integer variable by using pointer and
pointer to pointer.
#include<stdio.h>
void main()
{
int *pa; int **pb; int
a=10; pa=&a;
pb=&pa; printf("%x",
pa); printf("\n%x",
pb); printf("\n%d",
*pa); printf("\n%d",
**pb);
}
P a g e | 60
P a g e | 61
P a g e | 62
P a g e | 63
P a g e | 64
FUNCTION
Functions are a group of statements that have been given a name. This allows you to break
your program down into manageable pieces and reuse your code.
8. Debugging (removing error) becomes very easier and fast using the function sub-
programming.
9. Functions are more flexible than library functions.
10. Testing (verification and validation) is very easy by using functions. 11. User can build a
customized library of different functions used in daily routine having specific goal and link
with the main program similar to the library functions.
The functions are classified into standard functions and user-defined functions.
The standard functions are also called library functions or built in functions. All standard
functions, such as sqrt(), abs(), log(), sin(), pow() etc. are provided in the library of functions.
These are selective.
These functions must be written by the user (programmer), hence, the name userdefined
functions.
match the variables in the definition by type and number. A semicolon must be
present, when the function is called within the main() function. follows:
The general form of a ANSI method of function call is as function_name
(actual parameters
2.Defining functions:
Defining a function means writing an actual code for the function which does a
specific and identifiable task. Suppose you are defining a function which
computes the square of a given number. Then you have to write a set of
instructions to do this.
The general form of a ANSI method of function definition is as follows:
3.Function declaration:
Function declaration means specifying the function as a variable depending on the
return value return value. It is declared in the declaration part of the main
P a g e | 67
program. The default return value from a function is always an integer. If the
function is returning a value other than an integer, it must be declared with the data
type of the value it returns.
4.Function Arguments/Parameters:
arguments and parameters are the variables used in a program and a
function.variables used in the function definition (called function) are called
parameters.
The call by value method of passing arguments to a function copies the actual
value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the argument.
By default, C programming uses call by value to pass arguments
For ex:
#include <stdio.h>
int sum(int a, int b)
{
int c=a+b;
return c;
}
P a g e | 68
int main()
{
int var1 =10; int
var2 = 20;
int var3 = sum(var1, var2);
printf("%d", var3);
return 0;
}
For ex:
#include<stdio.h>
main()
{
int a,b,c;
printf("Enter Two Values: ");
scanf("%d %d",&a,&b); c=add(&a,&b);
printf("The Sum is: %d",c);
}
P a g e | 69
EXAMPLE:
Q. Create a function in C language to increment the value of integer by 100 by
using pointer.
#include<stdio.h>
void ch(int *num)
{
printf("Before adding values inside fun num= %d \n",*num);
(*num)+=100;
printf("After adding values inside functio num= %d \n",*num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n",x); ch(&x);
printf("After function call x=%d \n",x); return 0;
}
P a g e | 70
P a g e | 71
P a g e | 72
P a g e | 73
STRINGS
#include<stdio.h>
#include<string.h>
#include<conio.h> int
main()
{
char a[20]="programing "; char
b[10]="language"; char c[20]="programing
language";
char d[20]; strcat(a,b);
printf("%s",a); strcpy(d,c);
if(strcmp(a,d)==0)
printf("\nmatch");
getch();
return 0;
}
#include<conio.h> void
main()
!= '\0'; i++);
P a g e | 75
{ k = i-
1;
}
P a g e | 76
P a g e | 77
P a g e | 78
P a g e | 79
ARRAY
An array is a collection of variables of the same type that are referenced by a common name. In
C, all arrays consists of contiguous
memory locations. The lowest address corresponds to the first element,
P a g e | 80
and the highest address to the last element. Arrays may have from one to several dimensions.
A specific element in an array is accessed by an index.
For example
long:
int sample[10];
In C, all arrays have zero as the index of their first element. This declares an integer array that
has ten elements, sample[0] through sample[9].
FOR EX.
Two-dimensional arrays:
To declare two-dimensional integer array num of size int num[3][4];
Left Index determines row Right index determines column
Two dimensional arrays are stored in a row-column matrix where the first index indicates
the row and the second indicates the column. This means that the right most index changes
faster than the leftmost when accessing the elements in the array in the order in which they
are actually stored in memory
Example: main ()
{
int t, I, num [3][4]; for (t=0;
t<3; t++) for (i=0; i<4; ++i)
num [t][i] = (t * 4) + I + 1; for
(t=0; t<3; t++)
{
for (i=0; i<4; ++i) printf
(“%3d”, num[t][i]); printf
(“\n”);
}
}
#include <stdio.h>
void main ()
{ int arr[3][3],i,j;
for (i=0;i<3;i++)
{ for
(j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j); scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n"); for(i=0;i<3;i++)
{ printf("\n");
for (j=0;j<3;j++)
{ printf("%d\t",arr[i][j]);
}
}
}
P a g e | 83
P a g e | 84
P a g e | 85
P a g e | 86
STRUCTURE
A structure is a user defined data type. using a structure we have the ability to
define a new type of data considerably more complex than the types we have been
using.
A structure is a collection of variables referenced under one name providing a
convenient means of keeping related information together. The variables which
make up a structure are called structure elements.
P a g e | 87
struct addr
{
char street[40];
int postalcode;
}
For example:
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int age; char branch[10];
char gender;
};
int main()
{
struct Student s1;
s1.age = 19;
strcpy(s1.name, "neha");
UNIONS
WITH UNION DIFFERENT TYPES OF VALUES CAN BE STORED AT
SAME LOCATION IN DIFFERENT TIMES.SP0ACE IS ALLOCATED TO
ACCOMMODATE THE LARGEST MEMBER DATA TYPE.THEY ARE
SYNTACTICALLY IDENTICAL TO STRUCTURES.
SYNTAX:
union union-tag-optional
{
member-declarations
}union-name- optional Union
name.member ptr-to-union -> member
FOR EXAMPLE:
unionJob
{
P a g e | 90
char name[32];
workerNo;
} uJob;
struct structJob
char name[32];
P a g e | 91
float salary;
P a g e | 92
P a g e | 93
C Macros
A macro is a segment of code which is replaced by the value of macro.
Macro is defined by #define directive.
There are two types of macros:
1) Object-like Macros
2) Function-like Macros
1. Object-like Macros
LINUX
Linux is the best known and most used open source operating
system. As an operating system
Linux is software that sits underneath all the other software on
a computer, receiving request from those programs and relaying
these request to the computer hardware.
P a g e | 98
P a g e | 99
P a g e | 100
Advantages of Linux
The main advantage of Linux, is it is an open-source operating
system, means the source code is easily available for everyone .
In terms of security, Linux is more secure than any other operating
system. The software updates in Linux are easy and frequent.
Linux is freely available to use on the internet.
It has large community support.
It provides high stability. It rarely slows down or freezes and there is
no need to reboot it after a short time.
It maintain the privacy of the user.
It is network friendly.
P a g e | 101
It is fast and easy to install from the web. It can also install in any
hardware even in your old computer system.
It performs all tasks properly even if it has limited space on the hard
disk.
Disadvantages of Linux
It is not much user-friendly. So, it may be confusing for beginners.
It has small peripheral hardware drivers as compared to windows.
Directory navigation
Command Utility
pwd- Get the full path of the current working directory.
Command Utility
ls -l List the files and directories in the current directory in long (table) format (It is
recommended to use -l with ls for better readability).
ls -ld dir-name List information about the directory dir-name instead of its contents.
ls -a List all the files including the hidden ones (File names starting with a . are
hidden files in Linux).
rm -rf - Will remove the directory dir recursively, ignoring nonexistent files and
will never prompt for anything.
rmdir - Will remove the directory dir-name, if it's empty. This command can only
remove empty directories.
mkdir - Create a directory dir-name.
touch - a file filename, if it doesn't exist, otherwise change the timestamp of the file
to current time.