To store signed integers, we need to reserve one bit for the sign of the integer. It is usual convention to use a 0 bit to indicate positive sign and a 1 bit to indicate a negative sign. [Read more…] about Storage of Signed Integers
Pre-Processor Directive in C
Macros are small functions (generally single line functions) which may be dealt with the help of preprocessor directive #define. Here, we shall discuss only the directive #define which is also used to define constants. A macro may or may not have parameters. An advantage of using a macro is that if a program involves a large number of the function calls of a small function the overburden of function calls can make the program inefficient; in case of macro, the code is substituted wherever the macro occurs. Thus, a programmer does not have to repeat the code again and again in the source code of the program while the function call is eliminated. However, a disadvantage of using macro is that data types are not included in the macro nor are these checked by the compiler. A few illustrations of macros are given below. [Read more…] about Pre-Processor Directive in C
Precision for Characters and Strings in C
The field width and precision setting may be used for characters and strings as well. However, these have different meaning. See the following code: [Read more…] about Precision for Characters and Strings in C
Explicit Display of + and – Signs in Output in C
In general, the sign is not displayed with positive values. However, if we desire to display the + sign, we may add it in the control string. If the display is desired to be left justified as well as with+ sign, add + or ++between the % sign and the conversion character; for example, the following code for integers. [Read more…] about Explicit Display of + and – Signs in Output in C
Precision Setting in C
Precision is specified by the number of digits after the decimal point for the outputs for float as well as double numbers. If precision is not specified, it would be according to the default setting in the computer which is generally 6 digits. The precision may be specified by a period(.) followed by a positive number equal to the number of digits desired. An illustration is given below. [Read more…] about Precision Setting in C
Type Casting in C
Type casting or type conversion refers to change of type of variables or pointers or user-defined objects from one type to another type. The need for type conversion arises in some operations involving two operands of different types, or even of same type. The example given below illustrates this concept. [Read more…] about Type Casting in C
C storage class Specifiers
An object is a space in memory with a name or an identifier. The lifetime of an object is the storage duration of the object in a program, that is, lifetime is the portion of program execution during which the object exists with a constant address in the program and retains the last stored value. Three types of storage durations are defined in C, that is, automatic, static, and allocated. [Read more…] about C storage class Specifiers
Data Types in C
Data types in C are specified or identified as the data storage format that tells the compiler or interpreter how the programmer enters the data and what type of data they enter into the program. Data types are used to define a variable before use in a program. Data types determine the size of the variable, space it occupies in storage. [Read more…] about Data Types in C
Function of printf and scanf in C
The input into a program and output from a program is the basic requirements of any useful program. For input from standard input device, i.e., keyboard, the function scanf () is used and for output to the standard output device, i.e., monitor, the function printf () is used. Both these functions can take any number of arguments. The first argument is the formatting string enclosed between double quotes and consists of conversion characters. For printf () function, the formatting string may also contain any text that needs to be displayed on the monitor along with the values of variables. For example, if we want to display the value of an integer variable n, we may write the code as [Read more…] about Function of printf and scanf in C
Identifiers in C Language
When we declare a variable name and its type, the compiler allocates a block of memory for placing its value. In fact, for the computer, this allocated block of memory is the variable and it recognizes it by its name. The sizes of memory blocks allocated for different types of data may vary on different computers depending on the hardware, the operating system, and the compiler used. The names of variables or identifiers should be carefully selected. The general guidelines are as follows: [Read more…] about Identifiers in C Language
typedef in c
The typedef feature allows us to give an alternative (possibly short and more meaningful) name to an existing data type and improve program readability. For example, instead of using the int data type to declare variables to represent marks in three subjects, we can associate a more meaningful name (say Marks)for the int data type using typedef as: [Read more…] about typedef in c
Enumerated Types in C
Consider that we need to work with the colours in a rainbow, e. g., to paint a rainbow on the screen. We thus have to work with seven colours, namely, violet, indigo, blue, green, yellow, orange and red. These colours can be represented using integer values starting with 0. This enables us to use various program constructs such as conditions or loops to process these colours. However, programs written using such code often become difficult to understand as can be seen from the statement given below. [Read more…] about Enumerated Types in C
How to Formatted Output using the printf Function
The printf function allows the values of argument expressions to be printed in various formats. A large number of conversion characters are provided for printing expressions of different types. Also, the possibility of using several optional fields along with these conversion characters makes printf a very powerful and complicated function. [Read more…] about How to Formatted Output using the printf Function
Formatted Input – the scanf Function
The scanf standard library function is used to read one or more values from the standard input (keyboard) and assign them to specified variables. A typical call to the scanf function takes the following form: [Read more…] about Formatted Input – the scanf Function
Formatted Output – the printf Function
The printf (print formatted) standard library function is used to print the values of expressions on standard output (i. e., display) in a specified format. A typical call to the printf function takes the form of a C statement as [Read more…] about Formatted Output – the printf Function
Arithmetic Expressions in C
An arithmetic expression contains only arithmetic operators and operands. We know that the arithmetic operators in C language include unary operators (+ – ++ — ), multiplicative operators (* / %) and additive operators (+ – ). The arithmetic operands include integral operands (various int and char types) and floating-type operands (float, double and long double). [Read more…] about Arithmetic Expressions in C
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