We would start writing basic programs in C now. You need to have required software installed and configured in your system. Refer to the article of Hello World and ensure that you are able to compile and run the program.

Understanding the basic syntax

Syntax is basically the grammar or arrangement of words in language. Not every detail is explained at his point to keep things simple but necessary things are covered. Look at the program given below


#include <stdio.h>
void main()
{
    printf("Hello World");
}
    1. #include <stdio.h>

In simple words, understand the above line as instruction to compiler “Hey C compiler, Include(or refer to) this stdio.h file when you are compiling the source code, so that you can understand the meaning of words and functions used such as printf(), scanf() etc.”
It is because stdio.h file is present with compiler which has meaning(definition) of various functions defined in it which are frequently used.

    1. void main()

It is the point from where execution of program starts. If there is no main(), there is no output.
void is a return type which you will understand later with concept of functions.
The curly braces (curly brackets ” { } “) define the scope, i.e. the functioning code should be within these braces to make it work.

Programming in C

Now we’ll begin by writing few basic programs in C and understand the syntax and working in Sequential Flow.


#include <stdio.h>
int main()
{
    //Declaring Identifiers(variables)
    int num_a;
    int num_b;
    int num_result;
    //Take input from user when program is running
    printf("Enter a value of num_a: ");
    scanf("%d",&num_a);
    printf("Enter a value of num_b: ");
    scanf("%d",&num_b);
    //Adding numbers and assigning to identifier
    num_result = num_a + num_b;
    //Printing result as Output
    printf("Sum is %d", num_result);
}

Output

 

Add-2-Numbers

  • Swap 2 Numbers

It swaps the values in 2 variables using a third variable.


#include
int main()
{
    int num_a;
    int num_b;
    int num_temp; //Temporary variable used for swapping.
    printf("Enter a value of num_a: ");
    scanf("%d",&num_a);
    printf("Enter a value of num_b: ");
    scanf("%d",&num_b);
    //Assigning value of num_a in num_temp variable.
    num_temp = num_a; //num_a and num_temp has same value
    //Assigning value of num_b into num_a
    num_a = num_b;
    //Assigning value of num_temp which has original value of num_a to num_b
    num_b = num_temp;
    //Printing Output
    printf("Value of num_a: %d", num_a);
    printf("n"); //To print next output in next line
    printf("Value of num_b: %d", num_b);
}

Output

 

Swap-2-numbers

Assignments

Note: Try solving these on your own before searching for solutions on internet. These are very simple assignments. Assignment 5 to 8 are optional at this point.

    1. Implement programs using arithmetic operators

Add
Subtract
Multiply
Divide
Remainder

    1. Find the last digit of given integer input

Input: 4857
Output: 7
Hint: Modulus Operator

    1. Use Increment and Decrement Operators
    2. Use Next Line Character (“n”) to print “Hello World” in 2 lines

Also check what happens with “t” character.

  1. Print “Hello World” with Double Quotes included in Output
  2. use sizeof() operator and understand it’s functionality
  3. Swap 2 numbers without using 3rd variable
  4. Find ASCII code of various characters

Summary

We have understood the basic syntax of programming in C. It is highly recommended to complete the assignments mentioned above to get a better hands on of programming before proceeding further. These basics are extremely important in order to do programming in any language.

By admin

Leave a Reply

%d bloggers like this: