Monday, December 31, 2018

Preprocessor Directives - Good Bye 2018, Welcome 2019


Example of Preprocessor Directives:

Following program shows example of use of #include and #define in the C program.

#include<stdio.h>
#include<Windows.h>
#define p printf
#define s Sleep

int main()
{
    int i;
    for(i=0; i<5; i++)
    {
p("..... Good Bye 2018 .....");
    s(500);
    system("cls");
    s(500);
    }
    
    for(i=0; i<5; i++)
    {
p("..... WEL COME 2019 .....");
    s(500);
    system("cls");
    s(500);
    }
    return 0;
}

Output of the program:

This program will blink five times 

..... Good Bye 2018 .....

and then blinks five times

..... WEL COME 2019 .....

Visit C Program Practicals Blog for more details.



Preprocessor Directives


Preprocessor Directives - Overview

It is a program that is invoked by the compiler to process code before compilation.

Once preprocessor directives are applied; changes are applied to the original source code; and the new source code file without directives is created.

Preprocessing is one of the preliminary operation on C and C++ program files before they are passed to the compiler. The preprocessed source program file must be a valid C or C++ program. It allows us to perform the following:
  • Replace tokens in the current file with specified replacement
  • Insert files within the current file
  • Based on given condition- compile sections of the current file
  • Apply machine-specific rules to specified sections of code

A token is a series of characters delimited by white space. The allowed white space characters in as a preprocessor directives are space, horizontal tab, vertical tab, form feed, new-line character and comments.

Some of the preprocessor directives are:
  • #include Inserts text from another source file.
  • #define Defines a macro.
  • #undef Removes a preprocessor macro definition.
  • #error Defines text for a compile-time error message.
  • #if Conditionally suppresses portions of source code
Preprocessor directives begin with the # token and # is followed by a preprocessor keyword. The # is not part of the directive name and can be separated from the name with white spaces.


Example of Preprocessor Directives:



Sunday, December 16, 2018

Flowchart to find largest of 3 numbers


Flowchart to find Largest of Three Numbers

Following flowchart is used to find largest number from the given three numbers. This flowchart is prepared using RAPTOR tool.

Flowchart to find largest of given three numbers.
Fig.: Flowchart to find largest of given three numbers.
We may add one more conditional block in the flowchart to display appropriate message when all three or two of three numbers are equal.


You may like to visit following top 10 Flowchart Examples:

Friday, December 7, 2018

Factorial of number - flowchart

Flowchart of Factorial of a Given Number

According to Wikipedia; the Factorial of a non-negative integer N, denoted by N!, is the product of all positive integers less than or equal to N. 

For example: 4! = 4 X 3 X 2 X 1 = 24

The flowchart of finding factorial of a given number using RAPTOR tool is given below:

Factorial of number - flowchart
Fig: Flowchart of Factorial of a given number.

For more such interesting flowchart example visit Flowchart page. 


Wednesday, November 28, 2018

Array MCQs Set-2 Answer


C Programming - MCQs based on an Array Set-2 Answer


For declaration int number[10]; suppose the starting address of number[0] is 2202 and the size of int is 4 bytes in the system; then, the address of number[1] will be ______.
A. 2206
B. 2204
C. 2203
D. none of these
ANSWER: A

For declaration char fname[15]; suppose the starting address of fname[0] is 1200. Then, the address of fname[1] will be ______.
A. 1204
B. 1202
C. 1201
D. none of these
ANSWER: C

Array can be initialized at ____________ .
A. declaration time
B. runtime
C. declaration and runtime
D. none of these
ANSWER: C

Which of the following is correct definition of one dimensional array declaration?
A. data_type array_name[arraysize];
B. array_name data_type[arraysize];
C. data_type arraysize[array_name];
D. none of these
ANSWER: A

Which of the following is correct syntax of one dimensional array declaration?
A. int array1[10];
B. array1 int[10];
C. int 10[array1];
D. none of these
ANSWER: A


Tuesday, November 20, 2018

Array MCQs Set-2


C Programming - MCQs based on an Array Set-2

 

For declaration int number[10]; suppose the starting address of number[0] is 2202 and the size of int is 4 bytes in the system; then, the address of number[1] will be ______.
A. 2206
B. 2204
C. 2203
D. none of these 

For declaration char fname[15]; suppose the starting address of fname[0] is 1200. Then, the address of fname[1] will be ______.
A. 1204
B. 1202
C. 1201
D. none of these 

Array can be initialized at ____________ .
A. declaration time
B. runtime
C. declaration and runtime
D. none of these

Which of the following is correct definition of one dimensional array declaration?
A. data_type array_name[arraysize];
B. array_name data_type[arraysize];
C. data_type arraysize[array_name];
D. none of these

Which of the following is correct syntax of one dimensional array declaration?
A. int array1[10];
B. array1 int[10];
C. int 10[array1];
D. none of these

Check your ANSWER here...


For more details visit: C Program Practicals - Question Bank 

* * *



Saturday, November 17, 2018

Moving Object on Screen


This program will move designed object (arrow sign "==>") on screen without use of <graphics.h> and system("cls") or clrscr() function.

#include <stdio.h>
#include <windows.h>

COORD c = {0, 0};
void setxy (int x, int y)
{
 c.X = x; c.Y = y; // Set X and Y coordinates
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

int main() 

  int i;
  for(i=0; i<20; i++)
  {
    system("color A1");

    setxy(i, 5);
    printf("==>");

    setxy(i-3, 5); //For clearing previous position of arrow
    printf("   ");

    Sleep(200);
  }
  return 0;  
}

Output of Program

This program will move ==> sign on screen from Left side to Right side.


For more such interesting programs visit: C Program Practicals/Games

Friday, November 9, 2018

How to change text color in C

This practical will show how to change text color in C Program.

It will Blink Colorful New Year Message on screen without <graphics.h>.

#include<stdio.h>
#include<windows.h>
int main()
{
  int i;
  char *str[] = {"color A1", "color B1","color C1", "color D1"};

  for(i=0; i<4; i++)
  {  
    system(str[i]);
    printf(".....Wishing You and Your Family.....\n\n");
    printf(".....Happy and Prosperous New Year.....");
    Sleep(500);
    system("cls");
    Sleep(500);
  }
  return 0;
}

Output of Program: Following colorful message will blink on screen.

Tuesday, November 6, 2018

String - Predict Output-2 Answer

Predict Program Output Based on C String

Output of following C Programs are not very simple. These are simple but very interesting top c programs based on C Strings asked during viva and interviews.

Program-1

#include<stdio.h>
int main()
{
    char str[] = "%d";
    str[1] = 'c';
    printf(str, 97);
    return 0;
}

Output: a
Justification:
-> Statement char str[] = "%d" will initialize str[] to "%d".
-> str[1] = 'c'; assigns 'c' to str[1] which replaces "%d" with "%c".
-> printf() function prints character of ASCII value 97; hence final output is a.
 
Program-2

#include<stdio.h>
int main()
{
    printf("C Program Practicals", "C Programming", "ICP");
    return 0;
}

Output: C Program Practicals
Justification: Comma operator has Left to Right associativity; printf() starts printing with first double quote and ends at second double quote hence final output is C Program Practicals.

Program-3

#include<stdio.h>
int main()
{
    char *names[] = { "AAA", "BBB", "CCC"};
    int i;
    char *temp;
   
  temp = names[0];
    names[0] = names[1];
    names[1] = temp;
   
  for(i=0; i<=2; i++)
        printf("%s ", names[i]);
    return 0;
}

Output: BBB AAA CCC
Justification: Array names[] will be initialized with AAA BBB CCCThen using temp variable, first and second array elements are inter-changed; resulting in final output as BBB AAA CCC.

Program-4

#include<stdio.h>
#include<string.h>
int main()
{
    char name[] = "Gujarat\0India\0";
    printf("%d", strlen(name));
    return 0;
}

Output: 7

Justification: String initialization terminates are first ' \0 '; hence name[] contains only "Gujarat"; strlen(name) function return 7 which is finally printed.

Read more interesting programs at C Program Practicals - Question Bank.

Monday, November 5, 2018

String - Predict Output-2

Predict Program Output Based on C String

Output of following C Programs are not very simple. These are simple but very interesting top c programs based on C Strings asked during viva and interviews.

Program-1

#include<stdio.h>
int main()
{
    char str[] = "%d";
    str[1] = 'c';
    printf(str, 97);
    return 0;
}

Program-2

#include<stdio.h>
int main()
{
    printf("C Program Practicals", "C Programming", "ICP");
    return 0;
}

Program-3

#include<stdio.h>
int main()
{
    char *names[] = { "AAA", "BBB", "CCC"};
    int i;
    char *temp;
   
  temp = names[0];
    names[0] = names[1];
    names[1] = temp;
   
  for(i=0; i<=2; i++)
        printf("%s ", names[i]);
    return 0;
}

Program-4

#include<stdio.h>
#include<string.h>
int main()
{
    char name[] = "Gujarat\0India\0";
    printf("%d", strlen(name));
    return 0;
}
Check your Answer here...


Read more interesting programs at C Program Practicals - Question Bank.

Sunday, November 4, 2018

C Pointer - Predict output- Answer

Predict Program Output Based on C Pointers

Output of following C Programs are not very simple. These are simple but very interesting top c programs asked during viva and interviews.


Program-1


#include<stdio.h> 
int main() 

    int x = 10, *y, *z; 
    y = &x;  
    z = y; 
    printf("%d, %d, %d", x, *y, *z); 
    return 0; 
}

Output: 10, 10, 10
Justification: Address of variable x is assigned to pointer variable y. *y means value stored at memory address stored in y which is 10. Value stored in pointer y is stored in z; hence z also prints 10, resulting in output 10, 10, 10.

Program-2

#include "stdio.h" 
int main() 

    char str[] = { 'A', 'B', 'C', 'D' }; 
    char *p = &str[1]; 
    *p++;
    printf("%c ", *p);
    *p++;
    printf("%c ", *p);


Output: C D
Justification: Address of str[1] with value 'B' is stored in pointer p; which is incremented by one and printing 'C' first and then 'D'.


Program-3

#include "stdio.h" 
int main() 

    int marks[] = { 50, 60, 70, 80 }; 
    int *p = &marks[0]; 
    *p++;
    printf("%d ", *p);
    *p = *p+2;
    printf("%d ", *p);


Output: 60 62
Justification: -Address of marks[0] is stored in pointer p; which is incremented by one, hence prints 60 first. 


*p = *p+2;

Value of *p is 60; 2 is added to value it resulting in 62. Hence final output is 60 62.

Program-4

#include <stdio.h>
int main() 

    int marks[] = { 50, 60, 70, 80 }; 
    int *p = &marks[0]; 
    *p++;
    printf("%d ", *p);
    *p++;
printf("%d ", *p);


Output: 60 70
Justification: Address of marks[0] is stored in pointer p; this memory address is incremented by one; and points to second element of an array and prints 60. Again is it incremented by one resulting final output as 60 70. 

Program-5

#include <stdio.h> 
int main() 

    int *ptr; 
    *ptr = 5; 
    printf("%d", *ptr); 
    return 0; 



Output: This will results in run time error. 
Justification: Pointer variable (*ptr) cannot be initialized.


Wednesday, October 31, 2018

C String - Predict output- Answer


Predict Program Output Based on C String


Output of following C Programs are not very simple. These are simple but very interesting top c programs based on C Strings asked during viva and interviews.

Program-1


#include <stdio.h> 
int main () 

    char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    printf("Greeting message: %s\n", greeting ); 
    return 0; 
}

Output: Greeting message: Hello
Justification: Variable greetings will be initialized at compile time. '\0' is end of string character, and will not be printed along with Hello.


Program-2

#include "stdio.h" 
int main() 

    char str[] = { 'A', 'B', 'C', 'D' }; 
    char *p = &str[1]; 
    *p++;
    printf("%c ", *p);
    *p++;
    printf("%c ", *p);


Output: C D
Justification: Memory address of str[1] will be assigned to character pointer (*p). The value stored ar str[1] is 'B'. Statement *p++ will increment its value by one; which results in value 'C' which is printed. Again *p++ will increment its value by one; which results in 'D' which is printed resulting in final output as C D



Program-3


#include<stdio.h>
#include<string.h>
int main()
{
    char str1[20] = "C Program";
    char str2[20] = "C Practicals";
    printf("%s\n", strcpy(strcat(str1, str2), str2));
    return 0;
}

Output: C Practicals
Justification: Inner function strcat will be executed first, which will append the str2 (i.e. C Practicals) into str1 variable. Then, str2 (i.e. C Practicals) will be copied to str1; which will be the final output printed on screen.


Program-4


#include<stdio.h>
#include<string.h>
int main()
{
    printf("%d", strlen("01234567"));
    return 0;
}

Output: 8
Justification: strlen() function find the length of "01234567" which will be printed on screen.


Program-5


#include<stdio.h>
int main()
{
    printf(10 + "C Program Practicals");
    return 0;
}

Output: Practicals
Justification: First 10 characters will be ignored while printing a string "C Program Practicals"; hence output is Practicals.


Sunday, October 28, 2018

C String - Predict output


Predict Program Output Based on C String


Output of following C Programs are not very simple. These are simple but very interesting top c programs based on C Strings asked during viva and interviews.


Program-1


#include <stdio.h> 
int main () 

    char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    printf("Greeting message: %s\n", greeting ); 
    return 0; 
}

Program-2


#include "stdio.h" 
int main() 

    char str[] = { 'A', 'B', 'C', 'D' }; 
    char *p = &str[1]; 
    *p++;
    printf("%c ", *p);
    *p++;
    printf("%c ", *p);


Program-3


#include<stdio.h>
#include<string.h>
int main()
{
    char str1[20] = "C Program";
    char str2[20] = "C Practicals";
    printf("%s\n", strcpy(strcat(str1, str2), str2));
    return 0;
}

Program-4


#include<stdio.h>
#include<string.h>
int main()
{
    printf("%d", strlen("01234567"));
    return 0;
}


Program-5


#include<stdio.h>
int main()
{
    printf(10 + "C Program Practicals");
    return 0;
}

Click here to check ANSWER

Read more interesting programs at C Program Practicals - Question Bank.