C Programming Language Basics Tutorial for Beginners

  1. Brief History of C Programming Language
  2. Character Set in C Programming Language
  3. Data Types in C Programming Language
  4. Variable in C Programming Language
  5. Keywords in C Programming Language
  6. Basic Structure of a C Program
  7. Preprocessor Directive in C language
  8. Declaring a Variable in C Programming Language
  9. C Program Compilation Process
  10. Input Output in C Programming Language
  11. Input Functions in C Programming Language
  12. Operators in C Programming Language
  13. Control Constructs in C Programming with Examples
  14. Looping Constructs in C Programming with Examples
  15. Predefined Character Handling Functions with Examples
  16. Type Casting in C Programming Language with Example

Brief History of C Programming Language

C language is a structural programming language which was developed Dennis Ritchie in the year 1972 at Bell Laboratories. Basic origins of the C programming language are – B programming language which was invented by Ken Thompson at Bell Labs and BCPL (Basic Combined Programming Language) which was designed by Martin Richards. C programming language has the features from these two languages and has some extra features also.


Character Set in C Programming Language

  1. Alphabets (a to z, A to Z) (a≠A)
  2. All digits (0-9)
  3. Special characters ?, %, /, #, &, $ etc.

Data Types in C Programming Language

We have two types of data types in C

  1. Basic Data Types
  2. Derived Data Types

Basic Data Types in C Language

A data type specifies the type of the data that is being stored in a variable.
We have 5 basic data types:

Data Type Storage Size Value Range
char

unsigned char

signed char

1 byte

1 byte

1 byte

-128 to 127 or 0 to 255

0 to 255

-128 to 127

int

unsigned int

2 or 4 bytes

2 or 4 bytes

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

0 to 65,535 or 0 to 4,294,967,295

short

unsigned short

2 bytes

2 bytes

-32,768 to 32,767

0 to 65,535

long

unsigned long

 4 bytes

4 bytes

 -2,147,483,648 to 2,147,483,647

0 to 4,294,967,295

float 4 bytes up to 6 digits of precision
double 8 bytes up to 10 digits of precision

Derived Data Types in C

These are user defined data types which are derived from the basic data types. These are arrays, pointers, unions, structures, enumerations etc.

Variable in C Programming Language

A named location in the memory to refer to a particular value is called a variable or identifier. We can modify the value of a variable. Variable naming conventions are as follows:

  1. A variable name must be started with an alphabet or an underscore (_)
  2. Once a variable starts with an alphabet or an underscore it can be followed by digits.
  3. A variable shouldn’t contain special characters like space, ?, #, * etc.
  4. The length of the variable name must be at maximum 8 characters and it will ignore the remaining characters.
  5. A variable can have special characters $ and underscore (_)

Keywords in C Programming Language




A Keyword is a predefined word which is reserved for a special purpose and so we shouldn’t use these keywords as variable names. In C programming language we have totally 32 keywords which are as follows:

auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
Basic Structure of a C Program
Preprocessor Directives
Global Declarations // optional
main ( )
{
Local Declarations
Program Statements (actual logic)
}
Definitions of User Defined Functions // optional

Preprocessor Directive in C language

  • A statement that starts with #include is called a preprocessor directive. It must be always the first statement in your C program.
  • We can solve a problem in two ways. Either by using a predefined functions or by writing user defined functions. Code for all the predefined functions is placed in the header files.
  • When we use a PDF the corresponding code must be included in our program through a preprocessor directive and a corresponding header file. For example, #include <string.h>

Declaring a Variable in C Programming Language

â–º Declaring a variable is nothing but giving information about that variable to the compiler. We can declare a variable by using the syntax:
<data type> <variable name>;

â–º Single declaration: int a; int b;
â–º Multiple declaration: int a, b;

â–º Note that each statement in C program must be terminated with a semi-colon (;)

â–º We can even initialize the value of a variable by declaring it.
For example, int a=10, b=20;

â–º If we don’t declare a variable it will have a garbage value.
â–º We have two types of declarations. They are:

  1. Global Declarations in C

    A variable which is declared outside the main function is called a global variable. A global variable can be accessed by any of the functions throughout the program.

  2. Local Declarations in C

    A variable which is declared within the delimiters of any function is called a local variable. It can be accessed only within that functions. Each C program must have a main function.

C Program Compilation Process

  • Before running any C program it has to be compiled. Compilation is nothing but producing machine level code. In C it uses a predefined software called compiler to produce the code.
  • When we compile a C program it will produce another file with same name but with different extension .obj which is the equivalent assembly level language code of C file.
    [program.c]→[program.obj]→[program.exe]
  • During compilation it will allocate memory for the variables and it links the code of header file with your program. After this it will produce another file called executable .exe file which is the machine language equivalent of the source .c code. We get the output through the executable file.
  • Press Alt+F9 to compile a C program.
  • Press Ctrl+F9 to run a C program.
  • To view output on the screen press Alt+F5

Input Output in C Programming Language

  • For each and every program we need to take the input and produce output. Corresponding to all this input and output operations there are standard input and output devices.
  • Standard input device in C is keyboard and the standard output device is monitor.
  • We have another standard error file (std err) to which the corresponding device is monitor.

Input Functions in C Programming

These are the functions which will take input from the user and in this we have two types of functions:

Unformatted Input Functions in C language

In these input functions we cannot specify any format for the output.

getchar ( ) getch ( ) getche ( ) putchar ( )

getchar function

It is used to accept a character from the user input. It displays the character accepted, on the screen and waits for the user to press a key.

main() { char ch; ch = getchar(); putchar(ch); }

It will print a value for the variable.

getch function

This function will not display the character on the screen while accepting and it wont wait for the user to press any key.

getche function

Echos the character while accepting but won’t wait for the use to press any key. â–º There is also input functions gets() and puts() which are used to handle more than one character at a time. And to clear the screen user clrscr();

Formatted Input Output Functions in C

Through these functions we can specify the format of the input or the output. In this we have two functions

  1. scanf( )

    It is an input function which is used to take the input from the keyboard. This function takes two parameters.
    Syntax is scanf (“format specifier”, &input-variable);
    â–º Format specifier indicates the format of the input to be taken.

    %c for character.

    %d or %i for integer.

    %u for unsigned integer.

    %s for string.

    %ld for long digit.

    %f for float

    For example: scanf(“%c”, &ch);
    This will take one character from the keyboard. Ampersand & indicates the value of the variable must be read into the address of the variable. We can read more than one value by using a single scanf statement.
    For example:

    int a, b;

    scanf(“%d%d”,&a,&b);

  2. printf( )

    printf is an output function which is used to print something on the console meaning monitor. It can take either one or two arguments. If it takes a single argument it must be a constant string. If it takes two arguments then syntax is
    printf(“format-specifier”, conversion-string);
    Here conversion string converts the value of the format specifier into its original value. For example, to print the value of a character use printf(“%c”, ch);
    â–º Note that we can combine both the constant string and the format specifier in a single printf statement. For example,
    printf(“the value of ch is: %c”, ch);

Escape Sequences in C Language

Escape sequences are used to format the output on the screen.

  • n → newline character
  • t → tab space
  • v → vertical tab space
  • b → beep
  • \ → prints a backslash
  • r → carriage return

Operators in C Programming Language

An expression is a collection of operators and operands. Here operands are the variables on which we perform some operations and operators are the symbols that indicate the type of the operation. For example, a = b + c ; Here a, b, c are called operands whereas + and = are operators. Operators used in C Programming Language are as follows:

Assignment Operators

This is used to assign a value to a variable. In this we have to types of operators:

  1. Simple Assignment Operator =
  2. Compound Assignment Operator which is combined with some other operator.
    These are +=, -=, *=, /= and %=
    For example, a+=2 â–º a=a+2
    a-=3 â–º a=a-3
    b%=5 â–º b=b%5, here % is the modulus operator. This operator will get the remainder of division.
    For example, if a=8%2 then it will get a=0

Arithmetic Operators in C





These are used to do arithmetic operations. In this we have to types:

  1. Unary Arithmetic Operators which will work on a single operand. These are -, ++ and –. -a will negate the value of a. ++ and — are called increment and decrement operators respectively. Increment Operator will increment the value of a variable by 1. In this we have two types namely pre-increment and post-increment.
    (i) Pre-increment operator will first increment value of variable and then it uses the variable in the expression whereas
    (ii) Post-increment will first use the variable in the expression and then its value will be incremented. Example:

    Pre Increment Post Increment
    a=2
    b=++a
    b=3
    a=3
    a=3
    b=a++
    b=3
    a=4

    Similarly we have pre-decrement and post-decrement operators.

    Pre Decrement Post Decrement
    a=5
    b=–a
    b=4
    a=4
    a=5
    b=a–
    b=5
    a=4
  2. Binary Arithmetic Operators are those working on two operands. These are +, -, *, /, %

Relational Operators in C

These are used to find the relation between two variables. Relational operators are >, <, >=, <=, != and ==

Logical Operators in C

These are used to connect two or more expressions. Examples are &&, ||, ^ (i) In the case of && (and) the entire expression will be true if all are true. (ii) In case of || (or) the entire expression will be true if either of them is true. (iii) ^ (not) operator will negate the conditions. ^(a > b) = a < b

Sizeof() / Pseudo Operator in C

This operator will get the size of a variable or a datatype in the form of integer. For example, Printf(“%d”,sizeof(int)); â–º 2

Bitwise Operator in C

This will do bitwise comparisons. That means these will keep on comparing each bit in the operand. In this we have shift operators which are used to shift the bits to the left or right. Left shift will double the value of a variable whereas right shift will half the value.

Ternary Operator (?:) in C

This operator is used to replace if else constructed. For example consider the expression C = A>B?A:B In the above if condition A>B is true then A will be assigned to C otherwise value after the color that is B will be assigned to C.

Indirection Operator in C

This is used to access the value of a variable indirectly that is through a pointer variable.

Control Constructs in C Programming Language

Control Constructs are used to change the flow of control of a C program. In this we have two types of constructs: Conditional Control Constructs and Unconditional Control Constructs.

Conditional Control Constructs

These constructs will change the flow of control depending on a condition. Conditional Control Constructs are: Simple if, if-else-if, nested if, while loop, do while loop and for loop.

Unconditional Control Constructs

These constructs will change the flow of control of a program without the need of any condition. These are: Break, goto, exit, continue.

Simple if statement in C

This construct is used to check for a condition. Syntax:

if(condition)
{
statements;
}
else
{
statements
}

Note that when we have a single statement in an if construct then delimiters are optional otherwise they are compulsory. In the above syntax first the control will check for the condition. If it meets true it will execute the statements in if construct otherwise it will jump to else construct and execute those statements.

Example C Programs Using Simple If Construct

/* C Program to check whether the given number is +ve or -ve */

#include<stdio.h>
main()
{
float a;
printf(“Enter any Number:”);
scanf(“%f”, &a);

if(a<0)
printf(“\n %f is -ve”, a);

else
printf(“\n %f is +ve”, a);

}

/* C Program to check whether the given number is even or odd. */

#include<stdio.h>
main() {
int a;

printf(“\n Enter any number you want to check:”);
scanf(“%d”, &a);

if(a%2==0)
printf(“\n %d is Even”, a);

else
printf(“\n %d is Odd”, a);
}

if else if Statement in C Language

When we have more than one condition we must go for if-else-if construct. Syntax is:

if(condition)
{
statements;
}
else if(condition)
{
statements;
}
else if(condition)
{
statements;
}
. . . . . . . .
. . . . . . . .
. . . . . . . .

/* C Program to find largest among 3 numbers using if else if statement */

#include <stdio.h>
int main(){
float x, y, z;
printf(“Enter three numbers: “);
scanf(“%f %f %f”, &x, &y, &z);
if(x>=y && x>=z)
printf(“Largest number = %.2f”, x);
else if(y>=x && y>=z)
printf(“Largest number = %.2f”, y);
else
printf(“Largest number = %.2f”, z);
return 0;
}

Nested-If Construct in C Language

If there’s condition within another condition it is called nested-if. Syntax is:

if(condition)
{
if(condition)
{
statements;
}
}

/* C Program to find the largest among three number using nested if */

#include<stdio.h>
main() {
float a,b,c;
printf(“Enter three numbers::”);
scanf(“%f  %f  %f”,&a,&b,&c);

if(a>b && a>c)
printf(“\n %.2f is greatest”, a);

else if(b>c && b>a)
printf(“\n %.2f is greatest”, b);

else if(c>a && c>b)
printf(“\n %.2f is greatest”, c);
}

Looping Constructs in C Programming

The main purpose of looping constructs is to reduce the code and to make the program efficient. We use looping constructs when the number of statements are to be repeated at the same location in a program. While loop Syntax is:

while(condition)
{
statements;
}

/* C Program to Print 10 Natural Numbers Using While Loop */

#include<stdio.h>
int main() {
int a= 1;
while (a <= 10) {
printf(“\n %d”, a);
a++; }
}

/* C Program to Print n Natural Numbers Using While Loop */

#include<stdio.h>
main()
{
int i=1, n;
printf(“Enter value of n:”);
scanf(“%d”, &n);
while(i<=n)
{
printf(“\n %d”, i);
i++; }
}

/* C Program to Print Even Numbers up to the Given Limit Using While Loop */

#include<stdio.h>
main()
{
int i=2, l;
printf(“Specify the limit:”);
scanf(“%d”, &l);
while(i<=l)
{
printf(“\n %d”, i);
i+=2; }
}

Nested While Loop in C

A while loop within another while loop is called nested while loop. Syntax is:

while(condition)
{
statements;
while(condition)
{
statements;
}
statements;
}

do while loop in C Programming

In do while loop, the while condition comes last and so for the first iteration the statements are executed without satisfying condition and for the following iteration, while condition has to be satisfied. Syntax is:

do
{
statements;
}while(condition);

/* C Program to find a number is Armstrong or not using do while loop */

#include <stdio.h>
#include <math.h>
main() {
int num, res=0, rem, temp, digits=0;
printf(“\n Enter any number you want to check Armstrong or not:”);
scanf(“%d”, &num);
temp=num;
// Find number of digits
do
{
digits++;
temp = temp/10;
} while (temp != 0);

temp = num;

do
{
rem = num%10;
res = res + pow(rem, digits);
num = num/10;
} while(num>0);

if(res==temp)
printf(“\n %d is an Armstrong number”, temp);
else printf(“\n %d is not an Armstrong number”, temp);
}

for loop in C

Syntax is
for(expression1; expression2; expression3)
Here,
expression1 indicates the initialization part.
expression2 indicates the conditional part.
expression3 indicates increment or decrement part.

The control will come to the initialization part only once. Then it loops through the conditional and increment part. We can specify more then one initialization but they must be separated with commas. Even we can eliminate all the expressions but the loop must be satisfied with the conditions and increments. Example:

  1. Program to print first 10 natural numbers using for loop

    #include<stdio.h>
    main()
    {
    int i;
    for(i=1; i<=10; i++)
    printf(“\n %d”, i);
    return 0;
    }

Predefined Character Handling Functions in ctype.h

    1. islower()

      It will check whether the given character is in lowercase or not. It will take a character as an argument. If the given character is in lowercase, it will return a non-zero positive value or true otherwise it will return a negative value for false.

      /* C Program to check whether the given character is in lowercase or not by using predefined function islower() */

      #include<stdio.h>
      #include<ctype.h>
      main()
      {
      char ch;
      printf(“Enter any character:”);
      scanf(“%c”, &ch);
      if(islower(ch))
      printf(“\n character is lowercase”);
      else
      printf(“\n character is uppercase”);
      }

    2. isupper() checks whether the given character is in uppercase or not.
    3. isdigit() checks whether the given character is digit or not.
    4. isspace() checks whether the given character is space or not.
    5. isalnum() checks whether the given character is alphanumeric (A-Z, a-z, 0-9) or not.
    6. isalpha()
    7. checks whether the given character is an alphabet or not.

 

  • tolower()

converts the given uppercase character to lowercase ch=tlower(ch);

 

 

  • toupper() converts the given lowercase character into uppercase.

 

 

 

 

/* C Program to output the ASCII value of any entered character */

#include<stdio.h>
#include<ctype.h>
main() {
char ch;
printf(“Enter any character:”);
scanf(“%c”, &ch);
printf(“\n ASCII value of %c is:%d”, ch, toascii(ch));
}

/* C Program to convert lowercase to uppercase and vice versa */

#include<stdio.h>
#include<ctype.h>
main() {
char ch;
printf(“Enter any character:”);
scanf(“%c”, &ch);
if(islower(ch))
{
ch=toupper(ch); printf(“\n Uppercase letter is:%c”, ch);
}
else printf(“\n Lowercase letter is:%c”, tolower(ch));
}

Type Casting in C Programming Language with Example




Conversion of data type of a variable is called as type casting. It occurs in the mixed mode expression. A mixed mode expression is one in which all the variables are of different datatypes whereas a single mode expression is a one in which all the operands are of same data type.

Type casting is of two types – Implicit type casting and Explicit type casting.
Here’s an example program involving implicit type casting:

#include<stdio.h>
main()
{
int i=10, k;
float j=10.5;
k=i+j;
printf(“Value of k is %d”, k);
}

In the above program j will get automatically converted into integer which is according to the resultant variable k’s data type and output is Value of k is 20

In Explicit type casting the user needs to explicitly convert the datatype of a variable. Here is the example program for explicit type casting.

#include<stdio.h>
main()
{
int i=10, j=4;
float k;
k=(float)i/(float)j;
printf(“Value of k is %f”,k);
}

Here output is Value of k is 2.500000

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top