SlideShare a Scribd company logo
1 of 18
C basics : 
First c program : 
#include<stdio.h> 
main() 
{ 
printf(“hello world..”); 
} 
green part is for including header file, the definition of the used statements will contained by them. 
blue part is for the main function any program should contain main function . 
red part is for the body of the main funtion here we will write the program for serial execution. 
The c-program name will be closed with *.c extension. 
Any program may contain more than one header file. More than one function. But it have to 
contain the main function because execution starts from there. Execution is sequential. 
C character set: 
A to Z in uppercase, a to z in lowercase, digits 0 to 9 and the specila characters like ! # % & * ( ) - 
+ { } [ ]  | “ : ; < > , . ? @ $ ~ blank_space etc. are used. In C we use b n r v t for special 
conditions such as back space, newline, carriage return etc. These are called escape sequence. 
IDENIFIER & KEYWORD: 
Identifiers are the name which we give to the variables, functions etc. Name may contain any 
character except blank space in-between. It may start capital, small letter and undersore but not any 
number and any other special character. Name should not clash with any keyword or statement. 
Keyword is some reserved word donoting some predefined meaning. Like auto, char, int, long, 
float, void, for, while, if, case, switch, struct, sizeof, static etc. 
DATA TYPES: 
There are several data types, three main basic data types are int for integer variable (2 byte), float 
for real variable (4 byte), char for character variable(1 byte). 
We use long int for 4 byte integer variale and double for 8 byte double precision real entry. There 
are signed and unsigned int and char data types also. 
There are also user defined data types that we can define using typedef keyword. 
CONSTANT: 
There are four basic types of constant integer constant, floating point constant, character constant
and string constant. 
In integer constant there are decimal, octal, hexadecimal number system, signed and unsigned 
manner. 
For floating point constant we use decimal point or exponent or both. 
For character constant ASCII character set from 0 to 127 are used. Here we use escape sequence to 
denote some special character. We use the ASCII code or the character in single quotetion to asign. 
In order to asign the string constant we use the sequence of character enclosed by double 
quotetion. 
For defining symbolic constant we have to use #define notation, 
example, 
#define pi 3.14 
#define f(x,y) x*x+y*y // defining a function 
The value of symbolic constant can not be change. 
VARIABLE: 
It is a named location in the memory holding some special type od data. If we want to use a 
variable it should be declared first. 
Syntax of declaration: type var_name / var_list ; 
for example: int a; float b,c,d; 
for any variable if we want to get it's address then we have to put “&” before it. For example; for 
the variable “float b;” “&b” gives the address of the variable b. 
Array is the sequence of same kind of variable. To declare an array w have to give data type of the 
array and the name and the size of the array which should be an integer constant. 
For example, int a[100]; 
here “a” denotes the base address of the array a[100]. For individual address we can get them by 
using &a[10], &a[20] etc. 
For declaring a[100], we reserve 100 memory location sequentially starts from a[0], a[1], ..... 
a[99]. 
There are two kind of variable local and global. 
There are mainly four storage class static, extern, register and auto. 
If we use: const int a=5; then the value of “a” will become read only. 
OPERATORS: 
There are five basic arithmetic operators, addition (+), subtraction (-), multiplication (*), division 
(/) and modulo (%) operations. 
There are unary operators like negetion(-), incrementation(++), decrementation(--) not (!). 
There are logical operators like equal to (==), not equal to (!=), less than (<), less than or equal 
(<=), greater than(>), greater than or equal(>=), and (&&), or (||). 
There are asignment operators like =, +=, *=, /=, %=, -= . where a+=b implies a=a+b . 
There are also conditional operators (? :) 
for example; 
(a<b) ? p=2 : p=3 
if a<b holds then p will be 2 otherwise p will be 3. 
There is another operator named comma operator. 
x=(y=2,y=y+2,y=y*y,y*2); 
this gives the value x as 32. It execute from left to right sequntially and the last value is assigned to 
x.
2 will be assigned to y, then y+2 i.e 4 will be assigned to y, then y=y*y ie y become 16, at last y*2 
i.e 32 is the final value that is assigned to x. 
EXPRESSION: 
Expression is the single data item such as number or character. 
For example, 
a+b 
++a 
b-- 
a==b 
a=(a+b)*c/d*w-a etc. 
STATEMENTS: 
We are declaring statement for some particular task. Three types of statements are there, 
expression statement, compound statement and control statement. 
Example: 
printf(“%d”,45); 
scanf(“%d”,&p); 
s=a+b; 
; // null statement doing nothing. 
BASIC INPUT OUTPUT OPERATIONS: 
For inputing from keyboard we use scanf statement. 
scanf(“%d%f”,&a,&b); 
the yellow portion is used for control string, that gives us instruction that what kind of variable 
should be inputed. %d is for integer, %f is for float, %c is for character, %s is for string input. 
Here we have to store the data in the memory location. So we have to give memory location of the 
variable to store the data. 
For, inputing string we can also use, gets(). 
For inputing single character we can also use getchar(). 
For output we use the statement , 
printf(“the addition of %f and %d is %f n”,a,b,a+b); 
what ever we write in the double quotetion will be printed and the the values corresponds to %f, 
%d etc. will be taken sequentially from the highlighted data area. 
For string output we can use puts(“string” or string_name); 
For character output we can use putchar(“character” or character_name); 
COMMENT LINE: 
If we want to write some lines for our own references which compiler should not read then we 
called those comment lines. We use /* OUR_TEXT_IS_HERE */ or //OUR_TEXT_IS_HERE 
notations. 
For example: 
#include<stdio.h> //including standara input output file 
main()
{ /* 
simple 
program of 
addition 
*/ 
int a=5,b=6; 
printf(“%d”,a+b); 
} compiler will not read the highlighted portion. 
FUNCTION: For complex problem we divide the entire problem into some sun problems. 
Here we can use functions. It not only reduce the complexity but also reduce the program size 
sometimes. Sometimes a spetial calculation is needed several times in the program then we can 
declare that part into another function and call that as many time as it required. Any program must 
contain a function called main function. 
syntax for declaring the function: return_type function_name(arguments) { body } 
For example let us make a code for finding (n c r): 
#include<stdio.h> 
int fact(int n) 
{ 
if(n==0) 
return 1; 
else 
return(n*fact(n-1)); 
} 
main() 
{ 
int n,r; 
puts("enter n and r : "); 
scanf("%d%d",&n,&r); 
printf("answer is %d n",(fact(n)/((fact(n-r))*(fact(r))))); 
} 
HEADER FILES : There are several header files named stdio.h, conio.h, string.h, math.h etc. 
Each containing the definition of the special kind of statements used in the program. For example, 
all standard input, output statements (printf, scanf etc.) are contained in stdio.h. For string.h it 
contains the statements for operating on a string (strlen, strcmp etc.). For basic mathematical 
operations (exp, sin, atan, log, sqrt etc.) we use math.h. 
We use the notation; #include<HEADER_FILE_NAME.h> to include that perticular header file in 
our program. 
We can also create our own header file and include in our program. Header file name will close 
with the *.h extension. 
Here is the program, how we will include header file:
// we save this header file with name head.h 
#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 
typedef struct comp{ 
float a; 
float b; 
}; 
struct comp add (struct comp p,struct comp q) 
{ 
struct comp r; 
r.a=p.a+q.a; 
r.b=p.b+q.b; 
return r; 
} 
struct comp mul (struct comp p,struct comp q) 
{ 
struct comp r; 
r.a=p.a*q.a-p.b*q.b; 
r.b=p.a*q.b+p.b*q.a; 
return r; 
} 
struct comp conj (struct comp p) 
{ 
struct comp r; 
r.a=p.a; 
r.b=-p.b; 
return r; 
} 
struct comp mulr(struct comp p,float q) 
{ 
struct comp r; 
r.a=p.a*q; 
r.b=p.b*q; 
return r; 
} 
struct comp divr(struct comp p,float q) 
{ 
struct comp r; 
r.a=p.a/q; 
r.b=p.b/q; 
return r; 
} 
float modu(struct comp p) 
{ 
float r; 
r=p.a*p.a+p.b*p.b; 
return r; 
} 
struct comp divc (struct comp p,struct comp q)
{ 
struct comp r; 
r=divr(mul(p,conj(q)),modu(q)); 
return r; 
} 
//on the same directory where header file exists give the program name say comp.c 
#include"head.h" 
main() 
{ 
comp a,b,r; 
printf("enter two complex numbers !! "); 
scanf("%f%f%f%f",&a.a,&a.b,&b.a,&b.b); 
r=divc(a,b); 
printf("after division ( %.2f , %.2f ) / ( %.2f , %.2f ) = ( %.2f , %.2f ) 
n",a.a,a.b,b.a,b.b,r.a,r.b); 
} 
If we run the program comp.c it will run. So here in the header file we create our own definitions. 
We can join the above program in one program as; 
#include<stdio.h> 
#include<stdlib.h> 
#include<math.h> 
typedef struct comp{ 
float a; 
float b; 
}; 
struct comp add (struct comp p,struct comp q) 
{ 
struct comp r; 
r.a=p.a+q.a; 
r.b=p.b+q.b; 
return r; 
} 
struct comp mul (struct comp p,struct comp q) 
{ 
struct comp r; 
r.a=p.a*q.a-p.b*q.b; 
r.b=p.a*q.b+p.b*q.a; 
return r; 
} 
struct comp conj (struct comp p) 
{ 
struct comp r; 
r.a=p.a; 
r.b=-p.b;
return r; 
} 
struct comp mulr(struct comp p,float q) 
{ 
struct comp r; 
r.a=p.a*q; 
r.b=p.b*q; 
return r; 
} 
struct comp divr(struct comp p,float q) 
{ 
struct comp r; 
r.a=p.a/q; 
r.b=p.b/q; 
return r; 
} 
float modu(struct comp p) 
{ 
float r; 
r=p.a*p.a+p.b*p.b; 
return r; 
} 
struct comp divc (struct comp p,struct comp q) 
{ 
struct comp r; 
r=divr(mul(p,conj(q)),modu(q)); 
return r; 
} 
main() 
{ 
comp a,b,r; 
printf("enter two complex numbers !! "); 
scanf("%f%f%f%f",&a.a,&a.b,&b.a,&b.b); 
r=divc(a,b); 
printf("after division ( %.2f , %.2f ) / ( %.2f , %.2f ) = ( %.2f , %.2f ) 
n",a.a,a.b,b.a,b.b,r.a,r.b); 
} 
This will give the same answer. 
Here is another example to include header file: 
// header file new.h 
#include<stdio.h> 
#include<math.h> 
float sqa(float a,float b) 
{ 
return(sqrt(a*a+b*b));
} 
float sqs(float a,float b) 
{ 
return(sqrt(a*a-b*b)); 
} 
main() 
{ 
int a,b; 
printf("give a and b"); 
scanf("%d%d",&a,&b); 
printf("%f %f ",sqa(a,b),sqs(a,b)); 
} 
// c-file new.c 
#include "new.h" 
This will run the code in the header file. 
STRING: 
String is array of character followed by a null symbol. 
For using some inbuilt string operations we use the header file <string.h> 
Some of those inbuilt statements are: 
strcmp(a,b): compare strings a and b (lexiographically), give <0 if a<b, give 0 if a=b, give >0 if 
a>b. 
strcat(a,b): concatenate two string a and b, join b with a. 
strcpy(a,b): copy string b to string a. 
etc. 
We can also implement these without out using those inbuilt functions. 
POINTER: 
Pointer is a variable that store the reference or address of a variable of a specific data type. 
To declare a pointer variable pointing to a integer variable a we will declare them as, 
int a,*p; 
p=&a; 
i.e, p is containing the address of a. 
The pointer is declared as <data_type> *<pointer_name>; 
here the pointer can only store the reference of the data of the declared data type of the pointer. 
To store the reference of a pointer we use pointer to pointer, like below: 
int **p1,*p2,a; 
p2=&a; 
p1=&p2; 
and so on. 
Pointer is very useful to passing reference rather than the value to the function. As it denote the 
reference of the variable then change of the value at that reference does change the value actually. 
In first example, *p denote the value of the variable stored at the location p. So in the second 
example the value of a will be, **p1 or *p2.
SOME C-PROGRAMS: 
output : 
* 
*** 
***** 
******* 
********* 
* 
* 
code: 
#include<stdio.h> 
main() 
{ 
int i,n,j,k; 
for(j=0;j<=6;j++) 
{ 
for(i=1;i<10;i++) 
{ 
if((j>=5&&i==5)||((j<5)&&(i>=5-j)&&(i<=5+j))) 
printf("*"); 
else printf(" "); 
} 
printf("n"); 
} 
}
output : 
* * * 
*** *** *** 
***** ***** ***** 
******* ******* ******* 
********* ********* ********* 
* * * 
* * * 
code: 
#include<stdio.h> 
main() 
{ 
int i,n,j,k,p; 
puts("how many tree?? "); 
scanf("%d",&p); 
for(j=0;j<=6;j++) 
{ 
for(i=1;i<=10*p;i++) 
{ 
if((j>=5&&(i==5||i%10==5))||((j<5)&&(i%10>=5-j)&&(i%10<=5+j))) 
printf("*"); 
else printf(" "); 
} 
printf("n"); 
} 
}
output : 
* 
*** 
***** 
******* 
********* 
* 
* 
* 
*** 
***** 
******* 
********* 
* 
* 
* 
*** 
***** 
******* 
********* 
* 
* 
code : 
#include<stdio.h> 
main() 
{ 
int i,n,j,k,p; 
puts("how many tree? "); 
scanf("%d",&n); 
for(p=0;p<n;p++) 
{ 
for(j=0;j<=6;j++) 
{ 
for(i=1;i<10;i++) 
{ 
if((j>=5&&i==5)||((j<5)&&(i>=5-j)&&(i<=5+j))) 
printf("*"); 
else printf(" "); 
} 
printf("n"); 
} 
printf("nn"); 
} 
}
output : 
multiplication of two polynomials. 
code : 
#include<stdio.h> 
main() 
{ 
int a[100],b[100],c[100],m,n,i,j; 
printf("the degrees of 1st and 2nd polynomial "); 
scanf("%d%d",&m,&n); 
printf("enter the co-eff. of 1st plo. (asend.) "); 
for(i=0;i<=m;i++) 
{ 
scanf("%d",&a[i]); 
} 
printf("enter the co-eff. of 2nd pol. (asend.) "); 
for(i=0;i<=n;i++) 
{ 
scanf("%d",&b[i]); 
} 
for(i=0;i<=m+n;i++) 
{ 
c[i]=0; 
} 
for(i=0;i<=n;i++) 
for(j=0;j<=m;j++) 
{ 
c[i+j]=c[i+j]+b[i]*a[j]; 
} 
printf("ans..n"); 
for(i=0;i<=m+n;i++) 
{ 
printf("%d ",c[i]); 
} 
}
output : 
multiplication of two polynomials co-efficients are 3Χ3 matrices. 
code : 
#include<stdio.h> 
void mul(int a[][10],int b[][10],int c[][10],int n) 
{ 
int i,j,k; 
for(i=0;i<n;i++) 
for(j=0;j<n;j++) 
{ 
c[i][j]=0; 
} 
for(i=0;i<n;i++) 
{ 
for(j=0;j<n;j++) 
{ 
for(k=0;k<n;k++) 
{ 
c[i][j]=c[i][j]+a[i][k]*b[k][j]; 
} 
} 
} 
} 
void add(int a[][10],int b[][10],int n) 
{ 
int i,j; 
for(i=0;i<n;i++) 
for(j=0;j<n;j++) 
a[i][j]=a[i][j]+b[i][j]; 
} 
main() 
{ 
int a[10][10][10],b[10][10][10],c[10][10][10],d[10][10],m,n,i,j,p=3,x,y; 
printf("the degrees of 1st and 2nd polynomial "); 
scanf("%d%d",&m,&n); 
printf("enter the co-eff. of 1st plo. (asend.) "); 
for(i=0;i<=m;i++) 
{ 
for(x=0;x<p;x++) 
for(y=0;y<p;y++) 
scanf("%d",&a[i][x][y]); 
} 
printf("enter the co-eff. of 2nd pol. (asend.) "); 
for(i=0;i<=n;i++) 
{ 
for(x=0;x<p;x++) 
for(y=0;y<p;y++) 
scanf("%d",&b[i][x][y]); 
}
/*for(i=0;i<=m+n;i++) 
{ 
c[i]=0; 
}*/ 
for(i=0;i<=n;i++) 
for(j=0;j<=m;j++) 
{ 
mul(b[i],a[j],d,p); 
add(c[i+j],d,p); 
} 
printf("ans..n"); 
for(i=0;i<=m+n;i++) 
{ 
for(x=0;x<p;x++) 
{ 
for(y=0;y<p;y++) 
{ 
printf("%3d ",c[i][x][y]); 
} 
printf("n"); 
} 
printf("nn"); 
} 
}
output : 
birthday paradox. 
code: 
#include<stdio.h> 
#include<math.h> 
main() 
{ 
int i; 
float p,s=1; 
puts("enter the probability ___% : "); 
scanf("%f",&p); 
p=p/100.0; 
for(i=1;;i++) 
{ 
s=s*(365-i+1)/365; 
if((1.0-s)>=p) 
{ 
break; 
} 
} 
printf("%d number of students are neededn",i); 
}
Unwanted output using sleep try the following program: 
#include<stdio.h> 
main() 
{ 
int i; 
for(i=0;i<=5000;i++) 
{ 
printf("%d ",i); 
}s 
leep(2); 
printf("%c %c %c %c",66,67,68,69); 
} 
This is because of the output buffer used by compiler.
New way to calculating, nCr is to break n into two parts (say m & n) and send them into the 
following van() function. 
#include<stdio.h> 
int fact(int n) 
{ 
if(n==0) 
return 1; 
else 
return (n*fact(n-1)); 
} 
int ncr(int n, int r) 
{ 
if(n>=r) 
return (fact(n)/(fact(n-r)*fact(r))); 
else 
return 0; 
} 
int van(int n,int m,int r) 
{ 
int i,p=0; 
for(i=0;i<=r;i++) 
p=p+ncr(n,i)*ncr(m,r-i); 
return p; 
} 
main() 
{ 
int n,m,r; 
puts("enter m, n, r : "); 
scanf("%d%d%d",&m,&n,&r); 
printf("the values are %d and %d n",ncr(m+n,r),van(n,m,r)); 
} 
we can use this method to calculate nCr for large n.
Finding modulas a % b, and the correct answer. 
#include<stdio.h> 
main() 
{ 
int a,b,c,d; 
printf("give numbers a,b to calculate a % b : "); 
scanf("%d%d",&a,&b); 
c=a;d=b; 
if(d<0) 
{ 
c=c*-1; 
d=d*-1; 
} 
while(c<0) 
c+=d; 
c=c%d; 
a=b+a; 
a=a%b; 
printf("using only a % b = %d n correct answer = %d n",a,c); 
}

More Related Content

What's hot

Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-aneebkmct
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsRai University
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsRai University
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshopBAINIDA
 
C# Cheat Sheet
C# Cheat SheetC# Cheat Sheet
C# Cheat SheetGlowTouch
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Dushmanta Nath
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointerskirthika jeyenth
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures Pradipta Mishra
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick referenceArduino Aficionado
 

What's hot (20)

Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Unit 2
Unit 2Unit 2
Unit 2
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
strings
stringsstrings
strings
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
 
C# Cheat Sheet
C# Cheat SheetC# Cheat Sheet
C# Cheat Sheet
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Strings
StringsStrings
Strings
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Strings in c language
Strings in  c languageStrings in  c language
Strings in c language
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
Core c sharp and .net quick reference
Core c sharp and .net quick referenceCore c sharp and .net quick reference
Core c sharp and .net quick reference
 

Similar to C Basics Guide: Variables, Data Types, Operators & More

C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1AmIt Prasad
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CRaj vardhan
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it outrajatryadav22
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programmingIcaii Infotech
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARDTia Ricci
 
C programming language for beginners
C programming language for beginners C programming language for beginners
C programming language for beginners ShreyaSingh291866
 

Similar to C Basics Guide: Variables, Data Types, Operators & More (20)

C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
2. operator
2. operator2. operator
2. operator
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
Input output functions
Input output functionsInput output functions
Input output functions
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
CHAPTER 4
CHAPTER 4CHAPTER 4
CHAPTER 4
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
C tutorial
C tutorialC tutorial
C tutorial
 
C programming language for beginners
C programming language for beginners C programming language for beginners
C programming language for beginners
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 

Recently uploaded

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 

Recently uploaded (20)

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 

C Basics Guide: Variables, Data Types, Operators & More

  • 1. C basics : First c program : #include<stdio.h> main() { printf(“hello world..”); } green part is for including header file, the definition of the used statements will contained by them. blue part is for the main function any program should contain main function . red part is for the body of the main funtion here we will write the program for serial execution. The c-program name will be closed with *.c extension. Any program may contain more than one header file. More than one function. But it have to contain the main function because execution starts from there. Execution is sequential. C character set: A to Z in uppercase, a to z in lowercase, digits 0 to 9 and the specila characters like ! # % & * ( ) - + { } [ ] | “ : ; < > , . ? @ $ ~ blank_space etc. are used. In C we use b n r v t for special conditions such as back space, newline, carriage return etc. These are called escape sequence. IDENIFIER & KEYWORD: Identifiers are the name which we give to the variables, functions etc. Name may contain any character except blank space in-between. It may start capital, small letter and undersore but not any number and any other special character. Name should not clash with any keyword or statement. Keyword is some reserved word donoting some predefined meaning. Like auto, char, int, long, float, void, for, while, if, case, switch, struct, sizeof, static etc. DATA TYPES: There are several data types, three main basic data types are int for integer variable (2 byte), float for real variable (4 byte), char for character variable(1 byte). We use long int for 4 byte integer variale and double for 8 byte double precision real entry. There are signed and unsigned int and char data types also. There are also user defined data types that we can define using typedef keyword. CONSTANT: There are four basic types of constant integer constant, floating point constant, character constant
  • 2. and string constant. In integer constant there are decimal, octal, hexadecimal number system, signed and unsigned manner. For floating point constant we use decimal point or exponent or both. For character constant ASCII character set from 0 to 127 are used. Here we use escape sequence to denote some special character. We use the ASCII code or the character in single quotetion to asign. In order to asign the string constant we use the sequence of character enclosed by double quotetion. For defining symbolic constant we have to use #define notation, example, #define pi 3.14 #define f(x,y) x*x+y*y // defining a function The value of symbolic constant can not be change. VARIABLE: It is a named location in the memory holding some special type od data. If we want to use a variable it should be declared first. Syntax of declaration: type var_name / var_list ; for example: int a; float b,c,d; for any variable if we want to get it's address then we have to put “&” before it. For example; for the variable “float b;” “&b” gives the address of the variable b. Array is the sequence of same kind of variable. To declare an array w have to give data type of the array and the name and the size of the array which should be an integer constant. For example, int a[100]; here “a” denotes the base address of the array a[100]. For individual address we can get them by using &a[10], &a[20] etc. For declaring a[100], we reserve 100 memory location sequentially starts from a[0], a[1], ..... a[99]. There are two kind of variable local and global. There are mainly four storage class static, extern, register and auto. If we use: const int a=5; then the value of “a” will become read only. OPERATORS: There are five basic arithmetic operators, addition (+), subtraction (-), multiplication (*), division (/) and modulo (%) operations. There are unary operators like negetion(-), incrementation(++), decrementation(--) not (!). There are logical operators like equal to (==), not equal to (!=), less than (<), less than or equal (<=), greater than(>), greater than or equal(>=), and (&&), or (||). There are asignment operators like =, +=, *=, /=, %=, -= . where a+=b implies a=a+b . There are also conditional operators (? :) for example; (a<b) ? p=2 : p=3 if a<b holds then p will be 2 otherwise p will be 3. There is another operator named comma operator. x=(y=2,y=y+2,y=y*y,y*2); this gives the value x as 32. It execute from left to right sequntially and the last value is assigned to x.
  • 3. 2 will be assigned to y, then y+2 i.e 4 will be assigned to y, then y=y*y ie y become 16, at last y*2 i.e 32 is the final value that is assigned to x. EXPRESSION: Expression is the single data item such as number or character. For example, a+b ++a b-- a==b a=(a+b)*c/d*w-a etc. STATEMENTS: We are declaring statement for some particular task. Three types of statements are there, expression statement, compound statement and control statement. Example: printf(“%d”,45); scanf(“%d”,&p); s=a+b; ; // null statement doing nothing. BASIC INPUT OUTPUT OPERATIONS: For inputing from keyboard we use scanf statement. scanf(“%d%f”,&a,&b); the yellow portion is used for control string, that gives us instruction that what kind of variable should be inputed. %d is for integer, %f is for float, %c is for character, %s is for string input. Here we have to store the data in the memory location. So we have to give memory location of the variable to store the data. For, inputing string we can also use, gets(). For inputing single character we can also use getchar(). For output we use the statement , printf(“the addition of %f and %d is %f n”,a,b,a+b); what ever we write in the double quotetion will be printed and the the values corresponds to %f, %d etc. will be taken sequentially from the highlighted data area. For string output we can use puts(“string” or string_name); For character output we can use putchar(“character” or character_name); COMMENT LINE: If we want to write some lines for our own references which compiler should not read then we called those comment lines. We use /* OUR_TEXT_IS_HERE */ or //OUR_TEXT_IS_HERE notations. For example: #include<stdio.h> //including standara input output file main()
  • 4. { /* simple program of addition */ int a=5,b=6; printf(“%d”,a+b); } compiler will not read the highlighted portion. FUNCTION: For complex problem we divide the entire problem into some sun problems. Here we can use functions. It not only reduce the complexity but also reduce the program size sometimes. Sometimes a spetial calculation is needed several times in the program then we can declare that part into another function and call that as many time as it required. Any program must contain a function called main function. syntax for declaring the function: return_type function_name(arguments) { body } For example let us make a code for finding (n c r): #include<stdio.h> int fact(int n) { if(n==0) return 1; else return(n*fact(n-1)); } main() { int n,r; puts("enter n and r : "); scanf("%d%d",&n,&r); printf("answer is %d n",(fact(n)/((fact(n-r))*(fact(r))))); } HEADER FILES : There are several header files named stdio.h, conio.h, string.h, math.h etc. Each containing the definition of the special kind of statements used in the program. For example, all standard input, output statements (printf, scanf etc.) are contained in stdio.h. For string.h it contains the statements for operating on a string (strlen, strcmp etc.). For basic mathematical operations (exp, sin, atan, log, sqrt etc.) we use math.h. We use the notation; #include<HEADER_FILE_NAME.h> to include that perticular header file in our program. We can also create our own header file and include in our program. Header file name will close with the *.h extension. Here is the program, how we will include header file:
  • 5. // we save this header file with name head.h #include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct comp{ float a; float b; }; struct comp add (struct comp p,struct comp q) { struct comp r; r.a=p.a+q.a; r.b=p.b+q.b; return r; } struct comp mul (struct comp p,struct comp q) { struct comp r; r.a=p.a*q.a-p.b*q.b; r.b=p.a*q.b+p.b*q.a; return r; } struct comp conj (struct comp p) { struct comp r; r.a=p.a; r.b=-p.b; return r; } struct comp mulr(struct comp p,float q) { struct comp r; r.a=p.a*q; r.b=p.b*q; return r; } struct comp divr(struct comp p,float q) { struct comp r; r.a=p.a/q; r.b=p.b/q; return r; } float modu(struct comp p) { float r; r=p.a*p.a+p.b*p.b; return r; } struct comp divc (struct comp p,struct comp q)
  • 6. { struct comp r; r=divr(mul(p,conj(q)),modu(q)); return r; } //on the same directory where header file exists give the program name say comp.c #include"head.h" main() { comp a,b,r; printf("enter two complex numbers !! "); scanf("%f%f%f%f",&a.a,&a.b,&b.a,&b.b); r=divc(a,b); printf("after division ( %.2f , %.2f ) / ( %.2f , %.2f ) = ( %.2f , %.2f ) n",a.a,a.b,b.a,b.b,r.a,r.b); } If we run the program comp.c it will run. So here in the header file we create our own definitions. We can join the above program in one program as; #include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct comp{ float a; float b; }; struct comp add (struct comp p,struct comp q) { struct comp r; r.a=p.a+q.a; r.b=p.b+q.b; return r; } struct comp mul (struct comp p,struct comp q) { struct comp r; r.a=p.a*q.a-p.b*q.b; r.b=p.a*q.b+p.b*q.a; return r; } struct comp conj (struct comp p) { struct comp r; r.a=p.a; r.b=-p.b;
  • 7. return r; } struct comp mulr(struct comp p,float q) { struct comp r; r.a=p.a*q; r.b=p.b*q; return r; } struct comp divr(struct comp p,float q) { struct comp r; r.a=p.a/q; r.b=p.b/q; return r; } float modu(struct comp p) { float r; r=p.a*p.a+p.b*p.b; return r; } struct comp divc (struct comp p,struct comp q) { struct comp r; r=divr(mul(p,conj(q)),modu(q)); return r; } main() { comp a,b,r; printf("enter two complex numbers !! "); scanf("%f%f%f%f",&a.a,&a.b,&b.a,&b.b); r=divc(a,b); printf("after division ( %.2f , %.2f ) / ( %.2f , %.2f ) = ( %.2f , %.2f ) n",a.a,a.b,b.a,b.b,r.a,r.b); } This will give the same answer. Here is another example to include header file: // header file new.h #include<stdio.h> #include<math.h> float sqa(float a,float b) { return(sqrt(a*a+b*b));
  • 8. } float sqs(float a,float b) { return(sqrt(a*a-b*b)); } main() { int a,b; printf("give a and b"); scanf("%d%d",&a,&b); printf("%f %f ",sqa(a,b),sqs(a,b)); } // c-file new.c #include "new.h" This will run the code in the header file. STRING: String is array of character followed by a null symbol. For using some inbuilt string operations we use the header file <string.h> Some of those inbuilt statements are: strcmp(a,b): compare strings a and b (lexiographically), give <0 if a<b, give 0 if a=b, give >0 if a>b. strcat(a,b): concatenate two string a and b, join b with a. strcpy(a,b): copy string b to string a. etc. We can also implement these without out using those inbuilt functions. POINTER: Pointer is a variable that store the reference or address of a variable of a specific data type. To declare a pointer variable pointing to a integer variable a we will declare them as, int a,*p; p=&a; i.e, p is containing the address of a. The pointer is declared as <data_type> *<pointer_name>; here the pointer can only store the reference of the data of the declared data type of the pointer. To store the reference of a pointer we use pointer to pointer, like below: int **p1,*p2,a; p2=&a; p1=&p2; and so on. Pointer is very useful to passing reference rather than the value to the function. As it denote the reference of the variable then change of the value at that reference does change the value actually. In first example, *p denote the value of the variable stored at the location p. So in the second example the value of a will be, **p1 or *p2.
  • 9. SOME C-PROGRAMS: output : * *** ***** ******* ********* * * code: #include<stdio.h> main() { int i,n,j,k; for(j=0;j<=6;j++) { for(i=1;i<10;i++) { if((j>=5&&i==5)||((j<5)&&(i>=5-j)&&(i<=5+j))) printf("*"); else printf(" "); } printf("n"); } }
  • 10. output : * * * *** *** *** ***** ***** ***** ******* ******* ******* ********* ********* ********* * * * * * * code: #include<stdio.h> main() { int i,n,j,k,p; puts("how many tree?? "); scanf("%d",&p); for(j=0;j<=6;j++) { for(i=1;i<=10*p;i++) { if((j>=5&&(i==5||i%10==5))||((j<5)&&(i%10>=5-j)&&(i%10<=5+j))) printf("*"); else printf(" "); } printf("n"); } }
  • 11. output : * *** ***** ******* ********* * * * *** ***** ******* ********* * * * *** ***** ******* ********* * * code : #include<stdio.h> main() { int i,n,j,k,p; puts("how many tree? "); scanf("%d",&n); for(p=0;p<n;p++) { for(j=0;j<=6;j++) { for(i=1;i<10;i++) { if((j>=5&&i==5)||((j<5)&&(i>=5-j)&&(i<=5+j))) printf("*"); else printf(" "); } printf("n"); } printf("nn"); } }
  • 12. output : multiplication of two polynomials. code : #include<stdio.h> main() { int a[100],b[100],c[100],m,n,i,j; printf("the degrees of 1st and 2nd polynomial "); scanf("%d%d",&m,&n); printf("enter the co-eff. of 1st plo. (asend.) "); for(i=0;i<=m;i++) { scanf("%d",&a[i]); } printf("enter the co-eff. of 2nd pol. (asend.) "); for(i=0;i<=n;i++) { scanf("%d",&b[i]); } for(i=0;i<=m+n;i++) { c[i]=0; } for(i=0;i<=n;i++) for(j=0;j<=m;j++) { c[i+j]=c[i+j]+b[i]*a[j]; } printf("ans..n"); for(i=0;i<=m+n;i++) { printf("%d ",c[i]); } }
  • 13. output : multiplication of two polynomials co-efficients are 3Χ3 matrices. code : #include<stdio.h> void mul(int a[][10],int b[][10],int c[][10],int n) { int i,j,k; for(i=0;i<n;i++) for(j=0;j<n;j++) { c[i][j]=0; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } } void add(int a[][10],int b[][10],int n) { int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]=a[i][j]+b[i][j]; } main() { int a[10][10][10],b[10][10][10],c[10][10][10],d[10][10],m,n,i,j,p=3,x,y; printf("the degrees of 1st and 2nd polynomial "); scanf("%d%d",&m,&n); printf("enter the co-eff. of 1st plo. (asend.) "); for(i=0;i<=m;i++) { for(x=0;x<p;x++) for(y=0;y<p;y++) scanf("%d",&a[i][x][y]); } printf("enter the co-eff. of 2nd pol. (asend.) "); for(i=0;i<=n;i++) { for(x=0;x<p;x++) for(y=0;y<p;y++) scanf("%d",&b[i][x][y]); }
  • 14. /*for(i=0;i<=m+n;i++) { c[i]=0; }*/ for(i=0;i<=n;i++) for(j=0;j<=m;j++) { mul(b[i],a[j],d,p); add(c[i+j],d,p); } printf("ans..n"); for(i=0;i<=m+n;i++) { for(x=0;x<p;x++) { for(y=0;y<p;y++) { printf("%3d ",c[i][x][y]); } printf("n"); } printf("nn"); } }
  • 15. output : birthday paradox. code: #include<stdio.h> #include<math.h> main() { int i; float p,s=1; puts("enter the probability ___% : "); scanf("%f",&p); p=p/100.0; for(i=1;;i++) { s=s*(365-i+1)/365; if((1.0-s)>=p) { break; } } printf("%d number of students are neededn",i); }
  • 16. Unwanted output using sleep try the following program: #include<stdio.h> main() { int i; for(i=0;i<=5000;i++) { printf("%d ",i); }s leep(2); printf("%c %c %c %c",66,67,68,69); } This is because of the output buffer used by compiler.
  • 17. New way to calculating, nCr is to break n into two parts (say m & n) and send them into the following van() function. #include<stdio.h> int fact(int n) { if(n==0) return 1; else return (n*fact(n-1)); } int ncr(int n, int r) { if(n>=r) return (fact(n)/(fact(n-r)*fact(r))); else return 0; } int van(int n,int m,int r) { int i,p=0; for(i=0;i<=r;i++) p=p+ncr(n,i)*ncr(m,r-i); return p; } main() { int n,m,r; puts("enter m, n, r : "); scanf("%d%d%d",&m,&n,&r); printf("the values are %d and %d n",ncr(m+n,r),van(n,m,r)); } we can use this method to calculate nCr for large n.
  • 18. Finding modulas a % b, and the correct answer. #include<stdio.h> main() { int a,b,c,d; printf("give numbers a,b to calculate a % b : "); scanf("%d%d",&a,&b); c=a;d=b; if(d<0) { c=c*-1; d=d*-1; } while(c<0) c+=d; c=c%d; a=b+a; a=a%b; printf("using only a % b = %d n correct answer = %d n",a,c); }