You are on page 1of 10

Format specifiers:

%%:

Without %%:

Void main(){

Printf(“My marks is 52%”);

Output:

My marks is 52(with some warnings)

Using %%:

Void main(){

Printf(“My marks is 52%%”);

Output:

My marks is 52%(without warnings)

%o,%x and %i:

(to covert value to convert octal)

Void main()

Int x=10;

Printf(“%d in octal is %o”,x,x);//covert to octal

Printf(“%d in octal is %x”,x,x);//Convert to hexa

//octal number In c is preceded by 0

//if we give input in octal let it be 012 now %d consider as decimal but ist octal so to convert it we use %i
//so we need to scan value of x using %i to take input in hexa,octal or any format

%p: access the address in a hexadecimal

Difference between / and %: / can be applied both on int and float whereas % only on integer values

So to apply % on float we need to fmod() function

f1=10.0 ,f2=2.0

Printf(“%f”,fmod(f1,f2));

Abs():

To find absolute value and it can be done on int

Printf(“%d”,abs(-89));//cant be used for float

To find absolute value for float

Printf(“%f”,fabs(-89.2));

-14%3=-2

-14%-3=-2

14%-3=2 // only numerator sign need to be checked to find the sign of output

Print(2*5*2) #calculate from right to left 5**2=25 and 2**25

void main(){

int a=10,b=5;

int c;

c=((a>20)&&(b++));

printf("%d%d%d",a,b,c);

}
First condition checked its false so no need to check the next condition so exedcution of b++ does not
happen

c=((a<20)&&(b++)); //now the value of b will be changed to 6

print(‘x’==(‘x’ or ‘y’)) // prints True since or need to check only one condition (i.e.)’x’ x==x so, True

print(‘y’==(‘x’ or ‘y’)) // prints False since or need to check only one condition (i.e.)’x’ y!=x so, False

print(‘x’==(‘x’ and ‘y’))//prints False since and need to evaluate two condition’s first x and y now

//x!=y, False

print(‘y’==(‘x’ and ‘y’)) //prints True since and need to evaluate two condition’s first x and y now

Global Array has its values as Zero whereas local declared arrays will have the garbage value

Int a[10];

Void main(){

For(i=0;i<10;i++)

Printf(“%d ”,a[i]);

Output: 0 0 0 0 0 0 0 0 0 0

Void main(){

Int a[10]; // if this is Array declared as static the output is same as previous i.e. an array of zeros

For(i=0;i<10;i++)

Printf(“%d ”,a[i]);

Output: 1 0 4203609 0 0 0 17 0 0 0
Placement of header is not important in c:

Void main(){

#include<stdio.h>

Printf(“hello ”);// since we are using printf function after declaring header file so it doesn’t throw error

#include<filename.h>// pre-defined header file

#include”filename.h” //user defined header files

Static key word

void like() {

static int count=0;//Static Key word: to update the value every time function called

count++;

printf("%d",count);

int A=10;

void F1(){

printf("A=%d",A);

printf("B=%B",B); //Error: Though B is global but it declared after the function block

int B=20;

void main(){

int i;

for(i=0;i<10;i++)

like();

F1();

F2();
}

void F2(){ //Warning: Function should be declared before main if we are writing the function block

after the main block

printf("A=%d",A);

printf("B=%B",B);

Void main(){

Int a[2]={1,2,3,4,5};

For(int i=0;i<5;i++)

Printf(“%d”,a[i]); //it prints all 5 values first 2 are given and remaining are Garbage values

Output: 1 2 1 3 10097536->garbage

Designated Initializer

void main(){

int a[10]={[1]=1,[3]=2,[1]=10,[4]=3,3,4};// at index 1->1 is stored at index 3->2 is stored

int i=0; //gain we updated value at index 1

for(i=0;i<10;i++){ //3,4 stores after index preceding index 4

printf("%d is the value at index %d\n ",a[i],i);

Output:

0 is the value at index 0

10 is the value at index 1


0 is the value at index 2

2 is the value at index 3

3 is the value at index 4

3 is the value at index 5

4 is the value at index 6

0 is the value at index 7

0 is the value at index 8

0 is the value at index 9

 If the size of array size is not mention the array will take the highest index plus 1

3D array

A[2][3][5]//5 matrix’s of 2 rows and 3 colomns

List in python

L1=[1,2,3]

L2=l1

L2.append(22)

L1=append(25)

Print(l1)

Output:

[1, 2, 3, 22, 25] #both values are append to l1 and l2 will effect each individual list

Type1. To overcome it we use .copy() (Shallow copy)

L1=[1,2,3]

L2=l1.copy()

L2.append(22)
L1=append(25)

Print(l1)

Output:

[1, 2, 3, 25]

Type2. Deepcopy

Import copy # need to import the copy

L1=[1,2,3]

L2=copy.deepcopy(L1)

L2.append(22)

L1=append(25)

Print(L1)

Output:

[1, 2, 3, 25]

Pattern:

#include<stdio.h>

void main(){

char c1='A',c2;

int i,j,t,n;

t=0;

scanf("%d",&n);

for(i=0;i<n;i++){

for(j=0;j<=i;j++){

printf("%c ",c1);

c1++;

}
printf("\n");

Output:

BC

DEF

GHIJ

KLMNO

Convert the above pattern into the given below only by adding one line without Removing

AB

ABC

ABCD

ABCDE

Code:

void main(){

char c1='A',c2;

int i,j,t,n;

t=0;

scanf("%d",&n);

for(i=0;i<n;i++){

for(j=0;j<=i;j++){

printf("%c ",c1);

c1++;
}

c1='A';

printf("\n");

C program to rlace all 0’s in a number to 1’s:

#include<stdio.h>

#include<math.h>

void num(int n){

int r,count,k,sum;

sum=0;

k=0;

while(n>0){

r=n%10;

if(r==0){

r=1;

sum=sum+r*pow(10,k);

else{

sum=sum+r*pow(10,k);

k=k+1;

n=n/10;

}
printf("%d",sum);

void main(){

int m;

scanf("%d",&m);

num(m);

Input: 80101

Output:81111

Print the non prime integers in an array :

Output:

200

100

31

13

97

10

20

11

200 100 10 20

You might also like