C Comments

 

C Comments

Comments can be used to insert any informative piece which a programmer does not wish to be executed. It could be either to explain a piece of code or to make it more readable. In addition, it can be used to prevent the execution of alternative code when the process of debugging is done.


Comments can be singled-lined or multi-lined.

 

Single Line Comments

  • Single-line comments start with two forward slashes (//).
  • Any information after the slashes // lying on the same line would be ignored (will not be executed).

 

An example of how we use a single-line comment


#include <stdio.h>
 
int main()
{
    //This is a single line comment
    printf("Hello World!");
    return 0;
}

 

Multi-line comments

  • A multi-line comment starts with /* and ends with */.
  • Any information between /* and */ will be ignored by the compiler.

 

An example of how we use a multi-line comment


#include <stdio.h>
 
int main()
{
    /* This is a
    multi-line
    comment */
    printf("Hello World!");
    return 0;
}

C Variables

 

C Variables

Variables are containers for storing data values.

 

In C, there are different types of variables.


For example


  • an integer variable defined with the keyword int stores integers (whole numbers), without decimals, such as 91 or -13.
  • a floating point variable defined with keyword float stores floating point numbers, with decimals, such as 99.98 or -1.23.
  • a character variable defined with the keyword char stores single characters, such as 'A' or 'z'. Char values are bound to be surrounded by single quotes.

 

Declaration

We cannot declare a variable without specifying its data type. The data type of a variable depends on what we want to store in the variable and how much space we want it to hold. The syntax for declaring a variable is simple:


data_type  variable_name;


OR


data_type  variable_name = value;

 

Naming a Variable

There is no limit to what we can call a variable. Yet there are specific rules we must follow while naming a variable:


  • A variable name can only contain alphabets, digits, and underscores(_).
  • A variable cannot start with a digit.
  • A variable cannot include any white space in its name.
  • The name should not be a reserved keyword or any special character.

 

A variable, as its name is defined, can be altered, or its value can be changed, but the same is not true for its type. If a variable is of integer type, then it will only store an integer value through a program. We cannot assign a character type value to an integer variable. We can not even store a decimal value into an integer variable.

C User Input/Output

 

User Input/Output

We have already learned how the printf() function is used to output values in C. Another method, which goes by the name, scanf(), is used to get user input.

 

The scanf() function takes two arguments:


  • the format specifier of the variable (as shown in the example below)
  • the reference operator (&myNum), which stores the memory address of the variable. This is where the input data goes to.

Syntax

scanf("format specifier",&variable_name);

&-specifes the address of the variable.

One such example demonstrates how a program takes input from the user. 


#include <stdio.h>
 
int main()
{
    int marks;
    char name[30];
    printf("Enter student's name: ");
    scanf("%s", name);
    printf("Enter marks in English: ");
    scanf("%d", &marks);
 
    printf("Hello %s! You have scored %d in English!", name, marks);
    return 0;
}

 

Input


Enter student's name: Maxon
Enter marks in Maths: 98


Output


Hello Maxon! You have scored 98 in English!

 

You must note that we didn’t have to specify the reference operator (&) in cases of strings if we have specified the size of the strings already. This is an exception.