Skip to main content

Posts

Introduction To C++ Programming - Part 1.

Hello World: // File name: hello_world.cpp // A simple C++ program which prints "Hello World!" on the screen. #include <iostream>        // header file to support the C++ I/O system. using namespace std;     // telling the compiler to use namespace "std", // where the entire C++ library is declared. int main() { cout << "Hello World!" <<  endl; /* *  cout is the standard output device on the screen. *  << causes the expression on its right to be directed to the device on its left. *  return 0; indicates the successful termination of the "main" function. */ return 0; } Output: Hello World! Variables And Constants: // File name: variable.cpp // Demonstrate the use of variables and constants. #include <iostream> using namespace std; const int CONST_VAL = 5; // declaring a constant. Its value can't be changed.

The Basics Of C Programming - Part 6.

You may encounter situations, when a block of code needs to be executed multiple times. Loops are used in programming languages to repeat a specific block until some end condition is met. There are three loops in C programming: 1.    for loop 2.    while loop 3.    do...while loop A while loop in C programming repeatedly executes a target statement as long as a given condition is true. The syntax of a while loop is: while(condition)  { statement(s); } Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and if the condition is true (nonzero), codes inside the body of while loop is evaluated. The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop. The key point to note is that a  while  loop might not execute at all. When the condition is tested and the result is false...

The Basics Of C Programming - Part 5.

Conditional statements are used to execute a statement or a group of statement based on certain conditions. In the C programming language, you can typically use the following conditional statements: 1.    if statement 2.    if-else statement 3.    ternary statement or ternary operator 4.    nested if…else statement 5.    switch statement The syntax of an if statement is: if(boolean_expression) { /* statement(s) to execute if the boolean expression is true */ } If the Boolean expression is true, code inside the if statement is executed. If the Boolean expression is false, then the first set of code after the end of the if statement (after the closing curly brace) is executed. Example: #include <stdio.h> int main () { int a = 10; if( a < 20 ) { printf("a is less than 20\n"); } printf("value of a is %d\n", a); return 0; } Output: a is less than 20 value of a is 10

The Basics Of C Programming - Part 4.

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators: 1.    Arithmetic Operators 2.    Relational Operators 3.    Logical Operators 4.    Bitwise Operators 5.    Assignment Operators 6.    Misc. Operators We will look into the way each operator works in the below: Arithmetic Operators The following table shows all the arithmetic operators supported by C language. Assume variable a holds 10 and variable b holds 20 then: Operator Description Example + Adds two operands. a + b = 30 − Subtracts second operand from the first. a − b = -10 * Multiplies both operands. a * b = 200 / Divides numerator by de-numerator. b / a = 2 % Modulus Operator and remainder of...

The Basics Of C Programming - Part 3.

There are a number of different C input commands, the most useful of which is the scanf command. To read a single integer value into the variable called a you can use: scanf("%d",&a); When the program reaches the scanf statement it pauses to give the user time to type something on the keyboard and continues only when users press Enter or Return, to signal that he, or she, has finished entering the value. Then the program continues with the new value stored in a. In this way, each time the program is run the user gets a chance to type in a different value to the variable and the program also gets the chance to produce a different result! The final missing piece in the jigsaw is using the printf function, the one we use to print the value currently being stored in a variable. To display the value stored in the variable a you can use: printf("The value stored in a is %d",a); Note: the scanf function does not prompt for an input. You should ge...

The Basics Of C Programming - Part 2.

A variable is just a named area of storage that can hold a single value (numeric or character). C is very fussy about how you create variables and what you store in them. It demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it. There are five basic data types associated with variables: int - integer: a whole number. float - floating point value: i.e. a number with a fractional part. double - a double-precision floating point value. char - a single character. void - valueless special purpose type. One of the confusing things about the C language is that the range of values and the amount of storage that each of these types takes is not defined. This is because in each case the 'natural' choice is made for each type of machine. You can call variables what you like, although it helps if you give them sensible names that give you a hint of what they're being u...

The Basics Of C Programming - Part 1.

Your first program is going to do is print the message "Hello World" on the screen. The program is a short one, to say the least. Here it is: #include <stdio.h> main() { printf("Hello World\n"); } The first line is the standard start for all C programs - main() . After this comes the program's only instruction enclosed in curly brackets {} . The curly brackets mark the start and end of the list of instructions that make up the program - in this case just one instruction. Notice the semicolon marking the end of the instruction. You might as well get into the habit of ending every C instruction with a semicolon - it will save you a lot of trouble! Also notice that the semicolon marks the end of an instruction - it isn't a separator as is the custom in other languages. If you're puzzled about why the curly brackets are on separate lines I'd better tell you that it's just a layout convention to help you spot matching b...

C Interview Questions And Answers - Part 5.

What will be output of the following program? #include<stdio.h> int main() { int a = 2, b = 7, c = 10; c = a == b; printf("%d",c); return 0; } Output: 0 == is a relational operator which returns only two values. 0: If a == b is false 1: If a == b is true Since a = 2 and b = 7, so a == b is false, thus c = 0 What will be output of following program? #include<stdio.h> #include<string.h> int main() { char *ptr1 = NULL; char *ptr2 = 0; strcpy(ptr1," c"); strcpy(ptr2,"questions"); printf("\n%s %s",ptr1,ptr2); return 0; } Output: Run time error.  W e cannot assign any string constant in null pointer by strcpy function.

C Interview Questions And Answers - Part 4.

What is the output of following c program? #include<stdio.h> int main()  { int goto=5; printf("%d",goto); return 0; } Output:  Compilation error.  Because  it declared an invalid variable name. goto is a keyword in c language and variable name cannot be any keyword. What is the output of following c program? #include<stdio.h> int main() { long int 1a=5l; printf("%ld",1a); return 0; } Output: Compilation error. Because variable name cannot start with numeric value.

C Interview Questions And Answers - Part 3.

What is the meaning of NULL? Answer: NULL is a macro constant which has been defined in several header files such as stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as #define NULL 0 Example 1: What is the output of following c program? #include "stdio.h" int main()`{ if(!NULL) printf("I know preprocessor"); else printf("I don't know preprocessor"); } Output: I know preprocessor Explanation: !NULL = !0 = 1, i.e. any non-zero number means true. Example 2:   What is the output of following c program? #include "stdio.h" int main() { int i; static int count; for(i=NULL;i<=5;) { count++; i+=2; } printf("%d",count); } Output: 3  What is the output if you execute following c code? #include<stdio.h> int main() { int i; for(i=0;i<5;i++) { int i=10; printf(" %d",i); i++; } return 0; } Output: 10 10 10 10 10 Explanation: The default sto...

C Interview Questions And Answers - Part 2.

What is printf()? Answer: printf() is an inbuilt library function in C library by default. This function is declared and related macros are defined in “stdio.h” header file. printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. What is scanf()? Answer: scanf() is an inbuilt library function in C library by default. This function is declared and related macros are defined in “stdio.h” header file. scanf() function is used to read character, string, numeric data from keyboard. What is void in C? Answer: Void is an empty data type that has no value. We use void data type in functions when we don’t want to return any value to the calling function. Example: void sum (int a, int b); – This function won’t return any value to the calling function. int sum (int a, int b); – This function will return value to the calling function. We use void data type in pointer like “void *p”. It means, po...