Tutorialspoint

C Accessing array out of bounds

c

#include <stdio.h> 

int main() { 
   int arr[] = {1,2,3,4,5};  
   printf("The elements of array : "); 
   for(int i = 0; i<6; i++)
      printf(" %d",arr[i]);
   return 0; 
} 

C write long strings in Multi-lines

c

#include<stdio.h> 

int main() { 
   char *str = "This is the method "
               "to write long strings "     
               "in multiple lines in C"; 
   puts(str); 
   return 0; 
} 

C Data Types cannot use to create array

c

#include <stdio.h>
#include <stdlib.h>

int main(){
   void arr1[4];
   printf("A void array");
   return 0;
}

C passing parameter to a function

c

#include <stdio.h> 

const int R = 4; 
const int C = 3; 
void func(int a[R][C]) {   
   int i, j; 
   for (i = 0; i < R; i++) 
      for (j = 0; j < C; j++) 
      a[i][j] += 5; ; 
} 
  
int main() { 
   int a[R][C];
   int i, j;
   for (i = 0; i < R; i++) 
      for (j = 0; j < C; j++) 
         a[i][j] = i+j; 
   printf("Initial 2-D array is:\n");
   for (i = 0; i < R; i++) {
      for (j = 0; j < C; j++) {
         printf("%d ", a[i][j]); 
      }   
      printf("\n");
   }
   func(a);    
   printf("Modified 2-D array is:\n");
   for (i = 0; i < R; i++) {
      for (j = 0; j < C; j++) {
         printf("%d ", a[i][j]); 
      }   
      printf("\n");
   }
   return 0; 
}

C 2D array dynamically allocated single pointer

c

#include <stdio.h> 
#include <stdlib.h> 
  
int main() { 
   int row = 2, col = 3; 
   int *arr = (int *)malloc(row * col * sizeof(int)); 
   int i, j; 
    
   for (i = 0; i < row; i++) 
      for (j = 0; j < col; j++) 
         *(arr + i*col + j) = i + j;    
   printf("The matrix elements are:\n");
   for (i = 0; i < row; i++) {
      for (j = 0; j < col; j++) {
         printf("%d ", *(arr + i*col + j)); 
      }     
      printf("\n");
   }
   free(arr); 
   return 0; 
} 

Compile and Execute C Online

c

#include <stdio.h>
 
int main (){
   int a[5];
   int i,j;
       
   for (i = 0;i<5;i++) {
      a[i] = i+100; 
   }

   for (j = 0;j<5;j++) {
      printf("Element[%d] = %d\n", j, a[j] );
   }
   return 0;
}

C pointer

c

#include<stdio.h>

int main () {
   int  a = 8;  
   int  *ptr;      
   ptr = &a;       
   printf("Value of variable a: %d\n", a);
   printf("Address of variable a: %d\n", ptr);
   return 0;
}

C function-level static variables

c

#include<stdio.h> 

int func() {
   static int num = 0; 
   num += 5; 
   return num; 
}    
int main(){ 
   for(int i = 0; i<5; i++) {
      printf("%d\n", func()); 
  }
  return 0; 
}

C integer promotion

c

#include <stdio.h> 

int main() { 
   char x = 68;
   char y = 34;
    
   printf("The value of x is: %d", x);
   printf("\nThe value of y is: %d", y);
    
   char z = x/y;
   printf("\nThe value of z : %d", z);
    
   return 0; 
}

C variable declarations

c

#include <stdio.h> 

int main() { 
   int _4a = 3;
   int b = 12;

   printf("The value of variable _4a : %d", _4a);
   printf("\nThe value of variable b : %d", b);
    
   return 0; 
}

1 2 3 4 5 6 7 ... 61 Next
Advertisements
Loading...