Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Saturday, 19 August 2017

Understanding the execution of if-else block using fork

Here is a brief explanation on how both if/else gets execute in case of forking and piping example. Consider the below example:

[code]
#include <stdio.h>
#include <unistd.h>
int main() {
  pid_t pid;
  pid = fork();
  if(pid == 0) {
    printf("Hi Coding-Scripting");
  }
  else {
    printf("Bye.! Coding-Scripting");
  }
  return EXIT_SUCCESS;
}
[/code]

Explanation:

Whenever the call to fork() function is made, it replicates the whole code written down below the fork. So, now the two processes are created child process, with PID =0, and parent process with PID = X. So, the child execute the if block and exits, and after that parent executes the else block and exits. In this manner, both the if-else blocks will get execute.

Friday, 11 August 2017

Code to generate 32 random ascii characters

Hi,

It is a code to generate random 32 ASCII characters, you can modify it according to your need. You can use it for generating Passwords, encrypting passwords and many more things.



[code]
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main (int argc, char *argv[])
{
char st[31];
int temp;
srand(time(0));
for (int c=0;c<32;c++)
{
for(int i=1;i<2;i++)
{
temp=0;
while(temp<32 || temp>126)
   temp = rand()%126;
st[c] = temp;

}
}
cout<<st;
return 0;
}
[/code]

Secure coding practices for C scanf



This section contains about the secure coding practices which helps to make your code secure, reliable and fast.

1. Avoid using scanf():

Why => It leads to buffer overflow.
How to protect => use with %ms, and free. to allocate the space dynamically.

Source:

Mark's blog


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

int main(void)
{
    char *str;
    printf("Enter your name:\n");
    scanf("%ms", &str);

    printf("Hello %s!\n", str);
    free(str);
    return 0;
}

Thanks

Friday, 23 June 2017

Hello World program without using any semi colon in C

Question: Write the hello world program . . . Without using any semi-colon's ";" ?
Answer:

#include <stdio.h>

void main()
{
if(printf("Hello World")) { }
}

Monday, 20 March 2017

How to write in bold using ncurses in C

To use ncurses in your program, you first need to install ncurses. Once installed, write the below code:

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

int main() {
  int ch;
  initscr();
  raw();
  keypad(stdscr, TRUE);
  noecho();
  printw("Type any character to see it in bold\n");
  ch = getch();
  if (ch == KEY_F(1))
    printw("F1 Key pressed");
  else {
    printw("The pressed key is ");
    attron(A_BOLD);
    printw("%c", ch);
    attroff(A_BOLD);
  }
  refresh();
  getch();
  endwin();
  return EXIT_SUCCESS;
}

To compile it, use the ncurses library using
$ gcc file.c -lncurses

Once compiled, it will generate a executable named a.out

Type any character to see it in bold
The pressed key is A

If you need any help installing ncurses, write in comment section.