Header menu

_________________________________________________________________________________

Wednesday, 6 September 2017

Type Conversion in expressions in C

When constants and variable of different types are mixed in an expression, they are converted to the same type . The C compiler will convert all operands to the type of largest operand. This is done on an operation by operation basis following type conversion rules.

1. All chars and short int are converted to int. All floats are converted to doubles.

2. For all operand pairs: if one of the operands is double, the other operand is converted to double. Otherwise, if one of the operands is long, the other operand is converted to long.; if one of the operands is unsigned, the other is converted to unsigned

Tuesday, 5 September 2017

How to use Extern in C ? / How to use same global variable in multiple files ?

If you try to declare two global variables with the same name your c compiler will print the error message duplicate variable name, which means that the compiler does not know which variable you are using at any one moment. The same type of problem occurs if you simply declare all yours global variables in each  file. you would actually be trying to create two copies of each variable. When you attempt to link your modules together you will get error message duplicate label.

The solution is to declare all of your global variables in one file and use extern-modified declaration in other files

As illustrated below:

First we create a file with global variables 

fileb.c

int a=100;
int b=200;

Now we use these global variables in other file.

file.c

#include<stdio.h>
#include<conio.h>
#include<D:\inputs\fileb.c>
extern int a;
extern int b;
int main()
{

    printf("This is main file and a & b are called from other file\n");
    printf("The value of A and B from other file is %d and %d",a,b);
    getch();
    return 0;
}

OUTPUT 

This is main file and a & b are called from other file
The value of A and B from other file is 100 and 200
 

Sunday, 3 September 2017

Use of %C, %d, %e, %f, %s in printf

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<conio.h>
int main()
{
    //"Use of %C, %d, %e, %f, %s  in printf
   
    char ch='A';                                                              // single quote is used for char
    printf("The char enttered is %c",ch);
    int i=1001;
    printf("\n The integer enttered is %d",i);
    double j=3454000;
    printf("\n The integer enttered in scientific notation is %e",j);  
    float f=34.546;
    printf("\n The float enttered is %f",f);  
    char s[] = "Welcome to codesplanet.blogspot.in";        // double quote is used for string
    printf("\n The string entered is : %s",s);
  
    getch();
    return 0;
    }

Multiplication in C

#include<stdio.h>
#include<conio.h>
int main()
{
    int i=14;
    int j=15;
    int k=i*j;
    printf(" The product is %d",k);
    getch();
    return 0;
    }

Saturday, 2 September 2017

Hello World !! in C++

#include<iostream>
#include<stdio.h>
#include<conio.h>

using namespace std;
int main()
{
    cout<< "Hello World! Welcome to C++";
    getch();
    return 0;
    }