Interesting Programs
Interesting Programs
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("GFG");
system("test");
return 0;
}
// C Program
int printf(const char *format, ...);
int main() {
printf( "Hello World" );
return 0;
}
C++ language: We can’t directly put the declaration of printf() function as in previous case due to the
problem of Name mangling in C++. See this to know more about Name mangling. Therefore we just
need to declare the printf() inside extern keyword.
extern "C" {
int printf(const char *format, ...);
}
int main() {
printf( "Hello World" );
return 0;
}
Write a C program to print “Geeks for Geeks” without using a semicolon
printf() returns the total number of characters written to stdout. Therefore it can be used as a
condition check in an if condition, while condition, switch case and Macros.
#include<stdio.h>
int main()
{
if (printf("Geeks for Geeks") )
{ }
}
#include<stdio.h>
int main() {
// ASCII value of ; is 59
if (printf("%c", 59)) { }
}
#include<stdio.h>
#define N 10
#include <stdio.h>
int main()
{
printf("\x47 \n");
printf("\x45 \n");
printf("\x45 \n");
printf("\x4b \n");
printf("\x53");
return 0;
}
#include <stdio.h>
#define get(x) #x
int main()
{
printf(get(vignesh));
return 0;
}
What is the difference between single quoted and double quoted declaration
of char array?
In C, when a character array is initialized with a double-quoted string and the array size is not
specified, the compiler automatically allocates one extra space for string terminator ‘\0’.
#include <stdio.h>
int main()
{
// size of arr[] is 6 as it is '\0' terminated
char arr[] = "geeks";
printf("%lu", sizeof(arr));
return 0;
}
On the other hand, when the character array is initialized with comma comma-separated list of
characters and the array size is not specified, the compiler doesn’t create extra space for the string
terminator ‘\0’.
#include <stdio.h>
int main()
{
// arr[] is not terminated with '\0'
// and its size is 5
char arr[] = { 'g', 'e', 'e', 'k', 's' };
printf("%lu", sizeof(arr));
return 0;
}
#include <iostream>
using namespace std;
int main() {
int no = 8;
(no & 1 && cout << "odd" )|| cout << "even";
return 0;
}
Print substring of a given string without using any string function and loop
in C
#include <stdio.h>
// This function prints substring of str[] between low and high indexes (both inclusive).
void mysubstr(char str[], int low, int high) {
printf("%.*s", high - low + 1, (str + low));
}
int main() {
char str[] = "geeksforgeeks";
mysubstr(str, 1, 3);
return 0;
}
#include <iostream>
using namespace std;
label:
sum += array[index++];
if (index < N) goto label;
return sum;
}
// Driver Code
int main() {
int N = 5, sum = 0;
int array[] = { 1, 2, 3, 4, 5 };
sum = operate(array, N);
cout << sum;
}
Print 1 to 100 in C++ Without Loops and Recursion
#include <iostream>
using namespace std;
int main()
{
short sum = 0;
update:
sum++;
cout << sum << endl;
if (sum == 100) return 0;
goto update;
}
int main(){
// Define an array
int arr[] = { 11, 22, 33, 44 };