C PROGRAMMING: ABOUT C PROGRAMMING AND DATA TYPES PART-2

ABOUT C PROGRAMMING AND DATA TYPES PART-2

About C programming:

  • Portable- You can move C programs from one platform to another, and run it without any or minimal changes.
  • General-purpose- C programming can be used to develop operating systems, databases and so on.
  • Procedural language- Instructions in a C program are executed step by step.
  • Speed- C programming is faster than any other programming languages live Python, Java, etc.
To know more about the C programming, just click on the about c programming.

our first c program :

To create a simple C program which prints “Welcome to the C programming” on the screen, use a text editor to create a new file(e.g. programming.c – the file extension must be .c). Now we are going to write the source code:

#include<stdio.h>
int main ()
{
     printf("Welcome to the C programming");
return 0;
}

output :

Welcome to the C programming
#include<stdio.h>

This line tells the compiler to include the contents of the standard library header file stdio.h in the program.

data types :

In C programming, data types are declarations for variables.The most basic data type in C is integer, float, char. For example,

int myname;

Here myname is a variable of int (integer) type. One common error while declaring data types is : example,

  • int my name; ( This is incorrect )
  • int myname; ( This is correct ) here data type used is an integer.
  • Alert: Never leave space between the single word to whom you are declaring data type as shown above.
TypeSize (bytes)Format Specifier
intat least 2, usually 4%d, %i
char1%c
float4%f
double8%lf
short int2 usually%hd
unsigned intat least 2, usually 4%u
long intat least 4,usually 8%ld, %li
long long intat least 8%lld, %lli
unsigned long intat least 4%lu
unsigned long long intat least 8%llu
signed char1%c
unsigned char1%c
long doubleat least 10,usually 12 or 16%Lf

integer :

An integer is whole numbers that can have both zero, positive, and negative values. It doesn’t contain any decimal value. If you enter the value in decimal form after declaring it as an integer the output will always be an integer. Let’s make it more clear through coding:

But before coding lets recall the use of scanf() function. For example,scanf(“%d”, &b); The program will read in an integer value that the user enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b. an ampersand ( & ) read the address of the integer entered by the user.

#include<stdio.h>
int main()
{
    int i;
     printf(" Enter the value of i :");
     scanf("%d",&i);
     printf(" Value of i= %d",i);
return 0;
}

output :

Enter the value of i: 4
Value of i= 4

important space sequences in c:

n and t are the two important space sequence in C, which we discussed first.

  • \n- In a programming language, it is equivalent to pressing enter and start writing from the new line. For example,
#include
int main()
{
    printf("Hello\nWorld");
    return 0;
}


output:

Hello
World
  • \t- In programming language, it is equivalent to pressing four times spacebar, for example,
#include
int main()
{
    printf("Hello\tWorldt");
    return 0;
}

output:

Hello   World

There are many more space sequences, as shown below: ( You don’t need to learn them by-heart)

Here is a table which illustrates the use of escape sequences in C:

  1. \n (New line) – We use it to shift the cursor control to the new line
  2. \t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same line.
  3. \a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.
  4. \r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
  5.  \(Backslash) – We use it to display the backslash character.
  6.  (Apostrophe or single quotation mark) – We use it to display the single-quotation mark.
  7.  (Double quotation mark)- We use it to display the double-quotation mark.
  8.  (Null character) – We use it to represent the termination of the string.
  9. ? (Question mark) – We use it to display the question mark. (?)
  10. \nnn (Octal number)- We use it to represent an octal number.
  11. \xhh (Hexadecimal number) – We use it to represent a hexadecimal number.
  12. \v (Vertical tab)
  13. \b (Backspace)
  14. \e (Escape character)
  15. \f (Form Feed page break)

float and double:

Float and double holds real numbers. For example,

#include<stdio.h>
int main()
{
    float cost=43.74;
    double salary=4566.00;
    printf("Cost of the chocolate is=%f\n",cost);
    printf("Salary of the one person is=%lf\n",salary);
    return 0;
}

output:

Cost of the chocolate is=43.740002
Salary of the one person is=4566.000000

In C programming, floating-point numbers can also be represented in exponential form. For example,

#include<stdio.h>
int main()
{
    float data= 10e2;
    printf("Given data is=%f",data);
return 0;
}
We will continue our learning of data types in our next article.
But for now I will recommend you if you dont visit our previous articles, so, firstly have a look on them for more better understanding. The articles are provided below:
  1. C PROGRAMMING FOR BEGINNERS
  2. C PROGRAMMING: STRUCTURE AND DATA TYPES PART-1
Scroll to Top