Provide by:-bca-wala.blogspot.com
Ankit Rajbansi😍
Unit-2:- Expression and Operators Precedence- C Programming
DESCRIPTION | OPERATORS | ASSOCIATIVITY |
Function Expression | () | Left to Right |
Array Expression | [] | Left to Right |
Structure Operator | -> | Left to Right |
Structure Operator | . | Left to Right |
Unary minus | – | Right to Left |
Increment/Decrement | ++, — | Right to Left |
One’s compliment | ~ | Right to Left |
Negation | ! | Right to Left |
Address of | & | Right to Left |
Value of address | `*` | Right to Left |
Type cast | (type) | Right to Left |
Size in bytes | sizeof | Right to Left |
Multiplication | `*` | Left to Right |
Division | / | Left to Right |
Modulus | % | Left to Right |
Addition | + | Left to Right |
Subtraction | – | Left to Right |
Left shift | << | Left to Right |
Right shift | >> | Left to Right |
Less than | < | Left to Right |
Less than or equal to | <= | Left to Right |
Greater than | > | Left to Right |
Greater than or equal to | >= | Left to Right |
Equal to | == | Left to Right |
Not equal to | != | Left to Right |
Bitwise AND | & | Left to Right |
Bitwise exclusive OR | ^ | Left to Right |
Bitwise inclusive OR | | | Left to Right |
Logical AND | && | Left to Right |
Logical OR | || | Left to Right |
Conditional | ?: | Right to Left |
Assignment | =, *=, /=, %=, +=, -=, &=, ^=, |=, <<=, >>= | Right to Left |
Comma | , | Right to Left |
Console based I/O and related I/-
Input means to provide the program with some data to be used in the program and Output means to display data on screen or write the data to a printer or a file.
C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.
In this tutorial, we will learn about such functions, which can be used in our program to take input from user and to output the result on screen.
All these built-in functions are present in C header files, we will also specify the name of header files in which a particular function is defined while discussing about it.
scanf()
and printf()
functions
The standard input-output header file, named stdio.h
contains the definition of the functions printf()
and scanf()
, which are used to display output on screen and to take input from user respectively.
#include
<stdio
.h
>
1 | |
void
main()
{
// defining a variable
int i
;
1 | /* |
displaying message on the screen
asking the user to input a value
*/
printf("Please enter a value...");
1 | /* |
reading the value entered by the user
*/
scanf("%d",
&i
);
1 | /* |
displaying the number as output
*/
printf(
"\nYou entered: %d", i
);
}
When you will compile the above code, it will ask you to enter a value. When you will enter the value, it will display the value you have entered on screen.
You must be wondering what is the purpose of %d
inside the scanf()
or printf()
functions. It is known as format string and this informs the scanf()
function, what type of input to expect and in printf()
it is used to give a heads up to the compiler, what type of output to expect.
Format String | Meaning |
%d | Scan or print an integer as signed decimal number |
%f | Scan or print a floating point number |
%c | To scan or print a character |
%s | To scan or print a character string. The scanning ends at whitespace. |
We can also limit the number of digits or characters that can be input or output, by adding a number with the format string specifier, like "%1d"
or "%3s"
, the first one means a single numeric digit and the second one means 3 characters, hence if you try to input 42
, while scanf()
has "%1d"
, it will take only 4
as input. Same is the case for output.
In C Language, computer monitor, printer etc output devices are treated as files and the same process is followed to write output to these devices as would have been followed to write the output to a file.
NOTE : printf()
function returns the number of characters printed by it, and scanf()
returns the number of characters read by it.
int i
=
printf("studytonight");
In this program printf("studytonight");
will return 12
as result, which will be stored in the variable i
, because studytonight has 12 characters.
getchar()
& putchar()
functions
The getchar()
function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You can use this method in a loop in case you want to read more than one character. The putchar()
function displays the character passed to it on the screen and returns the same character. This function too displays only a single character at a time. In case you want to display more than one characters, use putchar()
method in a loop.
#include
<stdio
.h
>
1 |
void
main(
)
{
int c
;
printf("Enter a character");
/*
Take a character as input and
store it in variable c
*/
c
=
getchar();
/*
display the character stored
in variable c
*/
putchar(c
);
}
When you will compile the above code, it will ask you to enter a value. When you will enter the value, it will display the value you have entered.
gets()
& puts()
functions
The gets()
function reads a line from stdin(standard input) into the buffer pointed to by str
pointer, until either a terminating newline or EOF (end of file) occurs. The puts()
function writes the string str
and a trailing newline to stdout.
str
→ This is the pointer to an array of chars where the C string is stored. (Ignore if you are not able to understand this now.)
#include
<stdio
.h
>
1 |
void
main()
{
/* character array of length 100 */
char str
[100];
printf("Enter a string");
gets( str
);
puts( str
);
getch();
}
When you will compile the above code, it will ask you to enter a string. When you will enter the string, it will display the value you have entered.
Difference between scanf()
and gets()
The main difference between these two functions is that scanf()
stops reading characters when it encounters a space, but gets()
reads space as character too.
If you enter name as Study Tonight using scanf()
it will only read and store Study and will leave the part after space. But gets()
function will read it completely.
Header Files in C
Header files contain definitions of functions and variables, which is imported or used into any C program by using the pre-processor #include statement. Header file have an extension “.h” which contains C function declaration and macro definition.
Each header file contains information (or declarations) for a particular group of functions. Like stdio.h header file contains declarations of standard input and output functions available in C which is used for get the input and print the output. Similarly, the header file math.h contains declarations of mathematical functions available in C.
Types of Header Files in C
- System Header Files: It is comes with compiler.
- User header files: It is written by programmer.
Why need of header files
When we want to use any function in our c program then first we need to import their definition from c library, for importing their declaration and definition we need to include header file in program by using #include. Header file include at the top of any C program.
For example if we use printf() in C program, then we need to include, stdio.h header file, because in stdio.h header file definition of printf() (for print message on screen) is written in stdio.h header file.
Syntax Header Files in C
#include<stdio.h>
How to use Header File in Program
Both user and system header files are include using the pre-processing directive #include. It has following two forms:
Syntax
#include<file>
This form is used for system header files. It searches for a file named file in a standard list of system directives.
Preprocessor Directives –
The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation (Proprocessor direcives are executed before compilation.). It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs. A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define
directive.
Preprocessing directives are lines in your program that start with #
. The #
is followed by an identifier that is the directive name. For example, #define
is the directive that defines a macro. Whitespace is also allowed before and after the #
.
The #
and the directive name cannot come from a macro expansion. For example, if foo
is defined as a macro expanding to define
, that does not make #foo
a valid preprocessing directive.
All preprocessor directives starts with hash # symbol.
List of preprocessor directives :
#include
#define
#undef
#ifdef
#ifndef
#if
#else
#elif
#endif
#error
#pragma
1. #include
The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined and user-defined header files. If included file is not found, compiler renders error. It has three variants:
#include <file>
This variant is used for system header files. It searches for a file named file in a list of directories specified by you, then in a standard list of system directories.
#include "file"
This variant is used for header files of your own program. It searches for a file named file first in the current directory, then in the same directories used for system header files. The current directory is the directory of the current input file.
#include anything else
This variant is called a computed #include
. Any #include
directive whose argument does not fit the above two forms is a computed include
2. Macro’s (#define)
Let’s start with macro, as we discuss, a macro is a segment of code which is replaced by the value of macro. Macro is defined by #define
directive.
Syntax
#define token value
There are two types of macros:
- Object-like Macros
- Function-like Macros
1. Object-like Macros
The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants. For example:
#define PI 3.1415
Here, PI is the macro name which will be replaced by the value 3.14. Let’s see an example of Object-like Macros :
#include <stdio.h>
#define PI 3.1415
main()
{
printf("%f",PI
);
}
Output:
1 | 3.14000 |
2. Function-like Macros
The function-like macro looks like function call. For example:
#define MIN(a,b) ((a)<(b)?(a):(b))
Here, MIN is the macro name. Let’s see an example of Function-like Macros :
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void
main()
{
printf("Minimum between 10 and 20 is: %d\n",
MIN(10,20));
}
Output:
1 | Minimum between 10 and 20 is: 10 |