C has GOTO statement but one must ensure not to use too much of goto statement in their program because its functionality is limited and it is only recommended as a last resort if structured solutions are much more complicated. First let us see what the goto statement does, its syntax and functionality. The goto is a unconditional branching statement used to transfer control of the program from one statement to another. Syntax of goto statement is: [Read more…] about Why to avoid goto in C
Write a program in C programming language to print weekdays using switch statement
Simple C Program to print weekdays using switch statement with weekday number. In this C program you input any number between 1 to 7 and print name of days in week using switch case statement. [Read more…] about Write a program in C programming language to print weekdays using switch statement
What are the limitations with switch statement
Switch statement is a powerful statement used to handle many alternatives and provides good presentation for C program. But there are some limitations with switch statement which are given below: [Read more…] about What are the limitations with switch statement
When is a switch statement better than multiple if statements
switch statement is generally best to use when you have more than two conditional expressions based on
a single variable of numeric type. For instance, rather than the code
if (x == 1)
printf(“x is equal to one.\n”);
else if (x == 2)
printf(“x is equal to two.\n”);
else if (x == 3)
printf(“x is equal to three.\n”);
else
printf(“x is not equal to one, two, or three.\n”);
the following code is easier to read and maintain:
switch (x)
{
case 1: printf(“x is equal to one.\n”);
break;
case 2: printf(“x is equal to two.\n”);
break;
case 3: printf(“x is equal to three.\n”);
break;
default: printf(“x is not equal to one, two, or three.\n”);
break;
}
Notice that for this method to work, the conditional expression must be based on a variable of numeric type in order to use the switch statement. Also, the conditional expression must be based on a single variable. For instance, even though the following if statement contains more than two conditions, it is not a candidate for using a switch statement because it is based on string comparisons and not numeric comparisons:
char* name = “Lupto”;
if (!stricmp(name, “Isaac”))
printf(“Your name means ‘Laughter’.\n”);
else if (!stricmp(name, “Amy”))
printf(“Your name means ‘Beloved’.\n “);
else if (!stricmp(name, “Lloyd”))
printf(“Your name means ‘Mysterious’.\n “);
else
printf(“I haven’t a clue as to what your name means.\n”)
is a default case necessary in a switch statement
No, but it is not a bad idea to put default statements in switch statements for error- or logic-checking purposes. For instance, the following switch statement is perfectly normal: [Read more…] about is a default case necessary in a switch statement
Define Operator, Operand, and Expression in ‘C’
An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables. C has a rich set of operators which can be classified as [Read more…] about Define Operator, Operand, and Expression in ‘C’
What is Precedence of Operators
When number of operators occurs in an expression the operator which is to be evaluated first is judged by applying priority of operators. The arithmetic operators available in C are [Read more…] about What is Precedence of Operators
What is the difference between ++var and var++
The ++ operator is called the increment operator. When the operator is placed before the variable (++var), the variable is incremented by 1 before it is used in the expression. When the operator is placed after the variable (var++), the expression is evaluated, and then the variable is incremented by 1. [Read more…] about What is the difference between ++var and var++
What is the functionality and restrictions of Modulus Operator
When a division is performed the remainder of the operation is given by modulus operator. The modulus operator is denoted in c by symbol %. Say for instance we have two integer values x and y and then the operation x % y called as x modulus y gives the result as (x- (x / y) * y). Say if x=18 and y =5 the x % y gives value as (18- (18 / 5) * 5) which gives (18-15) which results in 3 in other words the remainder of the division operation. [Read more…] about What is the functionality and restrictions of Modulus Operator
How does the prefix and postfix operator on expression
The operators present in prefix and postfix are [Read more…] about How does the prefix and postfix operator on expression
Which bitwise operator is suitable for checking whether a particular bit is ON or OFF
Example: Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF (0) . Since we want to check the bit number 3, the second operand for AND operation we choose is binary 00001000, which is equal to 8 in decimal. [Read more…] about Which bitwise operator is suitable for checking whether a particular bit is ON or OFF
What is Expressions ? Type of Expression
An expression is a combination of variables constants and operators written according to the syntax of C language. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Some examples of C expressions are shown in the table given below. [Read more…] about What is Expressions ? Type of Expression
What happens when a variable is not declared in function definition
Generally in C program the function definition and calling takes the form as given below: [Read more…] about What happens when a variable is not declared in function definition
How to assign values during declaration of variables
Declaring variables tells the compiler the data type the variable is assigned and no storage area is allocated during this stage. It is possible to assign values during declaration itself. In order to see how this can be done let us see an example. [Read more…] about How to assign values during declaration of variables
What’s the best way to declare and define global variables
First, though there can be many declarations (and in many translation units) of a single “global” (strictly speaking, “external”) variable or function, there must be exactly one definition. (The definition is the declaration that actually allocates space, and provides an initialization value, if any.) [Read more…] about What’s the best way to declare and define global variables
What happens when a variable is not initialized in main function
when a variable is not initialized in main function it contains garbage value. This can be well seen from the example below. [Read more…] about What happens when a variable is not initialized in main function
What is scope & storage allocation of register, static and local variables
Register variables: belong to the register storage class and are stored in the CPU registers. The scope of the register variables is local to the block in which the variables are defined. The variables which are used for more number of times in a program are declared as register variables for faster access. [Read more…] about What is scope & storage allocation of register, static and local variables
What is scope & storage allocation of extern and global variables
Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The scope of the extern variables is Global. [Read more…] about What is scope & storage allocation of extern and global variables
Where is an Auto Variables Stored
Main memory and CPU registers are the two memory locations where auto variables are stored. Auto variables are defined under automatic storage class. They are stored in main memory. Memory is allocated to an automatic variable when the block which contains it is called and it is de-allocated at the completion of its block execution. [Read more…] about Where is an Auto Variables Stored
What is a Register Variable
Register variables are stored in the CPU registers. Its default value is a garbage value. Scope of a register variable is local to the block in which it is defined. Lifetime is till control remains within the block in which the register variable is defined. [Read more…] about What is a Register Variable
Static Variable in C
Static variable in C is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. A static variable can be initialized by using keyword static before variable name. As the name indicates, the static variables retain their value as long as the program executes.
This is useful if we want to isolate pieces of a program to prevent accidental changes in global variables. You can declare a static variable by using the static storage class access specifier. For example, [Read more…] about Static Variable in C
Difference Between Declaring a Variable and Defining a Variable
Declaration of a variable in C hints the compiler about the type and size of the variable in compile time. Similarly, declaration of a function hints about type and size of function parameters. No space is reserved in memory for any variable in case of declaration. [Read more…] about Difference Between Declaring a Variable and Defining a Variable
What is a Variables? Declaration of Variables.
A variable is an object whose value may change during execution of a program. It is a memory location used to store a data value. A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program. Variable names are case sensitive. Others are constants whose values cannot be changed during the execution of the program. However, their values have to be declared. In a program a variable may be declared as below. [Read more…] about What is a Variables? Declaration of Variables.
How do You Decide Which Integer Type to Use
If you might need large values (above 32,767 or below -32,767), use long. Otherwise, if space is very important (i.e. if there are large arrays or many structures), use short. Otherwise, use int. If well-defined overflow characteristics are important and negative values are not, or if you want to steer clear of sign-extension problems when manipulating bits or bytes, use one of the corresponding unsigned types. (Beware when mixing signed and unsigned values in expressions, though.) Although character types (especially unsigned char) can be used as “tiny” integers, doing so is sometimes more trouble than it’s worth, due to unpredictable sign extension and increased code size. [Read more…] about How do You Decide Which Integer Type to Use
Define the term Scope, Visibility And Lifetime of a Variable
An object is recognized by the computer by either its identifier or name. The object may be a variable of basic type or a function, a structure, or a union. The macro names and macro variables do not figure in the scope because macros are replaced by the preprocessor token sequences before the semantic phase of program translation. An identifier may also represent different objects in different scopes. [Read more…] about Define the term Scope, Visibility And Lifetime of a Variable
Data Types along with Their Sizes and Ranges
Both variables and the constants may have different types of values. In C language, different forms of data are categorized into a few abstract data types. For example, suppose you are asked to keep the record of the number of passengers in a bus. A variable say N, which is used to denote this number, will have value in whole numbers because the number of passengers cannot be a fractional number. Similarly, for number of students in a class, number of apples in a basket, number of pages in a book, or number of houses in a colony, etc., the values must be in whole numbers. Whole numbers form a category called integers. In C language, this type is coded as int, a short form for integer.
Numbers with a decimal point such as 2.4 or 3.14159 form another category or type, which when declared use float or double before their names. A float refers to a decimal floating point number with single precision (7 significant digits after the decimal point), whereas double is used for numbers with double precision (up to 15 digits after decimal point).
If a variable stands for names of students or for names of cities or names of rivers, etc., its values would be strings of characters such as “John”, “Delhi”, or “Ganga”.
An object of a given data type is stored in a section of memory having a discreet size. Objects of different data types require different amounts of memory. Therefore, it is necessary to specify the type of data that a variable will be having so that the compiler allocates appropriate memory size and address for storing its value. Following table shows the size and range of the basic data types.
Sizes and Ranges of Data Types :
Furthermore, there are other types such as void. The void is in fact devoid of any type mentioned above; void cannot be used with any data. It is mainly used with functions that do not return any data and pointers. Void is also called incomplete type. Two data types, the Boolean type, denoted in C as Bool, and the numeration type (type code enum) have been added in C-99. However, in C++ language, the code for Boolean type is bool. C language also allows user-defined types such as names of structures, and unions.
What is a Storage class
A storage class specifies how the variables are used in the program.
Static storage class or variable
A variable is declared to be static by prefixing its normal declaration with the keyword static, as in static int a;
Since the property of a variable may be stated in any order we could also use int static a; Static variables can be declared within a function. These variables retain their values from the previous call. i.e., the value that they had before returning from the function.
Register storage class
Register variables are stored in the register of the microprocessor. The number of variable which can be declared register are limited. This means that the variable has a maximum size equal to the register size. If more variables are declared as register variable, they are treated as auto variables. A program that uses register variables executes faster as compared to similar program without register variable.
Automatic storage class or variables
The scope of an automatic variable is only for the block, or any block within the block, in which it appears. All variables declared within a function are auto by default. A variable can be defined as automatic by placing the keyword auto at the beginning of the variable declaration.
Example: auto int a;
External Storage class or Global variable
External variables are global to the file in which they are defined. External variables are used only when all functions needed a particular variable. It retains the assigned value within the scope. In the following program variable i is an external variable.
Example: extern int a;
What is Constants? Type of constant
There are many different types of data values that are implicitly declared as constants in C. The value of a constant cannot be changed during execution of the program, neither by the programmer nor by the computer. The character ‘A’ is a constant having numerical value equal to 65 in decimal number system. [Read more…] about What is Constants? Type of constant
Enumeration Constant in C
Enumeration is a data type, which is coded as enum, may be used to define user’s own data type and define values that the variable can take. The enumeration type is an integral data type. This can help in making program more readable. enum definition is similar to that of a structure. Its syntax is as follows: [Read more…] about Enumeration Constant in C
Anatomy of C Program
We have some compiler preprocessor commands. This includes various #include files. Then comes the main function. Some name can also be given to the main function. Then, we have the variable declarations used in the main code. Then we have sub-functions. [Read more…] about Anatomy of C Program