Basics of C Programming

Basics are the roots of Foundation

Welcome Programmers, As we all know how important is to clear our basics while learning any new thing, so let's have a glimpse of the common C programming basics which are not just easy to learn but also memorizable.


Fundamental Terms in C Programming

  • Source Code: It is a code containing all the code data including the main function and other parts of the code that are necessary for the smooth running of the code. C Source code file ends with a '.c' extension.
  • Keywords: These are a set of reserved words in the C language, whose meaning is already defined in the compiler and are used for various purposes. There are 32 keywords in the C language
  • Identifiers: These are the names given to our program elements such as variables, arrays and functions. These consist of only alphabets, numbers, or underscores.
  • Variable: A meaningful name was given to a data storage location in computer memory

Comments:

In C Programming, single-line comments are given // symbol. Any statement after // symbol would be considered a comment
Also for multi-line comments/*  */ symbol is used. Anything between this would be considered as comments

example program:

#include<stdio.h>
/* THIS 
      IS 
      A 
 MULTI-LINE 
 COMMENT     
*/
int main()
{
printf("Hello world"); //THIS IS A SINGLE LINE COMMENT   
return 0;
}

OUTPUT :

Hello world

Main Data Types in C:


Primitive data types are the fundamental types, whereas derived data types are the ones which are derived from the primitive ones. Whereas user-defined data types are the ones that can be created by the user whenever needed like arrays, Strings, structures etc.

User-defined data types can be considered of somewhat higher level which will be taught in further lessons.

Input/Output statements in C:

  1. printf() function : The printf function(stands for print formatting) is used to display the information required by the user and also prints the values of variables.It takes data values and convert them to text stream using fromatting functions. Syntax: printf("format specifiers and control strings",argument1,arg2...argn);
  2. scanf() function : The function scanf (stands for scan formating) and is used to read formatted data from the keyboard.It takes the text stream from the keyboard,extracts and formats data from the stream according to a format control string and then stores the data in specified prorgam variables. Syntax: scanf("control string",arg1,arg2...argn);

Illustration:

int num;                       //variable declaration
scanf("%d",&num);    //getting an input from the user
printf("%d',num);        //printing to the screen

  • int num tells that a variable of data type int has been declared by the name num, which can be used to store any numerical value, but not decimal ones.
  • Did you noticed a & sign in scanf statement, this is to signify the address of the variable in which we are going to store the value. The overall statement means that the input would be stored at the location of the variable in the memory.
  • In print there is no & sign, this is because here we are only calling the name of the variable which gives us the value without any error.

 Examples:

int i=10;
char c='a';
float f=10.2;
printf("%d",i);
printf("%c",c);
printf("%f",f);

OUTPUT
 
10a10.2

Here the output is somewhat combined, but we imagined that it would be printed line by line. To do so we need to add '\n' character in control string.

int i=10;
char c='a';
float f=10.2;
printf("%d\n",i);
printf("%c\n",c);
printf("%f\n",f);

OUTPUT

10
a
10.2

Also the %d, %c, and %f are the format specifiers used to particular data types.
 
float n;                      
scanf("%f",&n);    
printf("%0.2f',n);

OUTPUT

Lets us consider the input was 10.2223
so printf function will give 10.22 

This is because of %0.2f specifier, the 0.2 signifies that there would be only 2 digits after the decimal, if it had been .1 then there would be only 1 digit, giving us the output 10.2.
You can see no round-off nothing is done.

int n;                      
scanf("%2d",&n);    
printf("%d',n);

Here we can give a 2 digit input only because of the %2d specifier property.

More references for I/O formatting in C :




Best Books for learning C Programming : 





Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post