Beruflich Dokumente
Kultur Dokumente
Ans.
1. C is highly portable, i.e., C programs written for one computer can be run on another with little or no
modification.
2. C language is well suited for structured programming, which requires user to think of any problem
in terms of function modules or blocks. A proper collection of these modules would make a complete
program.
3…
Q2. What will happen if we use more than one main function?
Ans. The compiler cannot understand which one marks the beginning of the program.
The empty pair of parentheses immediately following main indicates that the function main has no
arguments (or parameters).
All the statements between those curly braces form the function body. The function body contains a set
of instructions to perform a given task.
The lines beginning with /* and ending with */ are known as comment lines. These are used in a program
to enhance its readability and understanding. Comment lines are not executable statements and thereore
anything between /* and */ is ignored by the compiler. Comment lines cannot be nested in C.
/* = = = /* = = = = */ = = = */
printf is a standard C function for printing output. Predefined means that it is a function that has already
been written and compiled, and linked together with our program at the time of linking.
Every statement in C should end with a semicolon (;) mark.
Errors in C/C++
Error is an illegal operation performed by the user which results in abnormal working of the program.
Programming errors often remain undetected until the program is compiled or executed. Some of the
errors inhibit the program from getting compiled or executed. Thus errors should be removed before
compiling and executing.
The most common errors can be broadly classified as follows.
Type of errors
1. Syntax errors: In computer science, a syntax error is an error in the syntax of a
sequence of characters or tokens that is intended to be written in a particular
programming language. For compiled languages, syntax errors are detected at
compile-time. Errors that occur when you violate the rules of writing C/C++ syntax are known
as syntax errors. This compiler error indicates something that must be fixed before the code can
be compiled. All these errors are detected by compiler and thus are known as compile-time errors.
Most frequent syntax errors are:
while(.)
In the given example, the syntax of while loop is incorrect. This causes a syntax error.
2. Run-time Errors : Errors which occur during program execution(run-time) after successful
compilation are called run-time errors. One of the most common run-time error is division by
zero also known as Division error. These types of error are hard to find as the compiler doesn’t
point to the line at which the error occurs.
For more understanding run the example given below.
// C program to illustrate
// run-time error
#include<stdio.h>
void main()
{
int n = 9, div = 0;
// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;
8. In the given example, there is Division by zero error. This is an example of run-time error i.e
errors occurring while running the program.
9. Linker Errors: These error occurs when after compilation we link the different object files with
main’s object using Ctrl+F9 key(RUN). These are errors generated when the executable of the
program cannot be generated. This may be due to wrong function prototyping, incorrect header
files. One of the most common linker error is writing Main() instead of main().
// C program to illustrate
// linker error
#include<stdio.h>
13. Logical Errors : On compilation and execution of a program, desired output is not obtained when
certain input values are given. These types of errors which provide incorrect output but appears
to be error free are called logical errors. These are one of the most common errors done by
beginners of programming.
These errors solely depend on the logical thinking of the programmer and are easy to detect if we
follow the line of execution and determine why the program takes that path of execution.
// C program to illustrate
// logical error
int main()
{
int i = 0;
#include <stdio.h>
#define merge(a, b) a##b
int main()
{
printf("%d ", merge(12, 34));
}
// Output: 1234
Run on IDE
6) A token passed to macro can be converted to a string literal by using # before it.
#include <stdio.h>
#define get(a) #a
int main()
{
// GeeksQuiz is changed to "GeeksQuiz"
printf("%s", get(GeeksQuiz));
}
// Output: GeeksQuiz
Run on IDE
7) The macros can be written in multiple lines using ‘\’. The last line doesn’t need to have ‘\’.
#include <stdio.h>
#define PRINT(i, limit) while (i < limit) \
{ \
printf("GeeksQuiz "); \
i++; \
}
int main()
{
int i = 0;
PRINT(i, 3);
return 0;
}
// Output: GeeksQuiz GeeksQuiz GeeksQuiz
Run on IDE
8) The macros with arguments should be avoided as they cause problems sometimes. And
Inline functions should be preferred as there is type checking parameter evaluation in inline
functions. From C99 onward, inline functions are supported by C language also.
For example consider the following program. From first look the output seems to be 1, but it
produces 36 as output.
#define square(x) x*x
int main()
{
int x = 36/square(6); // Expended as 36/6*6
printf("%d", x);
return 0;
}
// Output: 36
Run on IDE
If we use inline functions, we get the expected output. Also the program given in point 4 above
can be corrected using inline functions.
inline int square(int x) { return x*x; }
int main()
{
int x = 36/square(6);
printf("%d", x);
return 0;
}
// Output: 1
Run on IDE
9) Preprocessors also support if-else directives which are typically used for conditional
compilation.
int main()
{
#if VERBOSE >= 2
printf("Trace Message");
#endif
}
Run on IDE
10) A header file may be included more than one time directly or indirectly, this leads to
problems of redeclaration of same variables/functions. To avoid this problem, directives
like defined, ifdef and ifndef are used.
11) There are some standard macros which can be used to print program file (__FILE__),
Date of compilation (__DATE__), Time of compilation (__TIME__) and Line Number in C code
(__LINE__)
#include <stdio.h>
int main()
{
printf("Current File :%s\n", __FILE__ );
printf("Current Date :%s\n", __DATE__ );
printf("Current Time :%s\n", __TIME__ );
printf("Line Number :%d\n", __LINE__ );
return 0;
}
/* Output:
Current File :C:\Users\GfG\Downloads\deleteBST.c
Current Date :Feb 15 2014
Current Time :07:04:25
Line Number :8 */
Benefits of C language
1. As a middle level language, C combines the features of both high level and low level languages.
It can be used for low-level programming, such as scripting for drivers and kernels and it also
supports functions of high level programming languages, such as scripting for software
applications etc.
2. C is a structured programming language which allows a complex program to be broken into
simpler programs called functions. It also allows free movement of data across these functions.
3. Various features of C including direct access to machine level hardware APIs, presence of C
compilers, deterministic resource use and dynamic memory allocation make C language an
optimum choice for scripting applications and drivers of embedded systems.
4. C language is case-sensitive which means lowercase and uppercase letters are treated
differently.
5. C is highly portable and is used for scripting system applications which form a major part of
Windows, UNIX and Linux operating system.
6. C is a general purpose programming language and can efficiently work on enterprise applications,
games, graphics, and applications requiring calculations etc.
7. C language has a rich library which provides a number of built-in functions. It also offers dynamic
memory allocation.
8. C implements algorithms and data structures swiftly, facilitating faster computations in programs.
This has enabled the use of C in applications requiring higher degrees of calculations
like MATLAB and Mathematica.
Escape Sequences in C
In C programming language, there are 256 numbers of characters in character set. The entire character
set is divided into 2 parts i.e. the ASCII characters set and the extended ASCII characters set. But apart
from that, some other characters are also there which are not the part of any characters set, known as
ESCAPE characters.
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\? Question Mark
\0 Null
// C program to illustrate
// \a escape sequence
#include <stdio.h>
int main(void)
"is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a");
return (0);
Run on IDE
Output:
My mobile number is 7873923408.
// C program to illustrate
// \b escape sequence
#include <stdio.h>
int main(void)
// compilers.
printf("Hello Geeks\b\b\b\bF");
return (0);
Run on IDE
Output:
// C program to illustrate
// \n escape sequence
#include <stdio.h>
int main(void)
printf("Hello\n");
printf("GeeksforGeeks");
return (0);
Run on IDE
Output:
Hello
GeeksforGeeks
// C program to illustrate
// \t escape sequence
#include <stdio.h>
int
main(void)
printf("Hello \t GFG");
return (0);
Run on IDE
Output:
Hello GFG
The escape sequence “\t” is very frequently used in loop based pattern printing programs.
// C program to illustrate
// \v escape sequence
#include <stdio.h>
int main(void)
printf("Hello friends");
return (0);
Run on IDE
Output:
Hello Friends
Welcome to GFG
// sequence
#include <stdio.h>
int main(void)
return (0);
Run on IDE
Output: (Depend upon compiler)
ends
#include <stdio.h>
int main(void)
printf("Hello\\GFG");
return (0);
Run on IDE
Output: (Depend upon compiler)
Hello\
GFG
Explanation : It contains two escape sequence means it after printing the \ the compiler read the
next \ as as new line character i.e. \n, which print the GFG in the next line
// C program to illustrate \' escape
// sequence/ and \" escape sequence to
#include <stdio.h>
int main(void)
return 0;
Run on IDE
Output:
// C program to illustrate
// \? escape sequence
#include <stdio.h>
int main(void)
printf("\?\?!\n");
return 0;
Run on IDE
Output:
??!
#include <stdio.h>
int main(void)
// digits(0....7).
char* s = "A\0725";
printf("%s", s);
return 0;
Run on IDE
Output:
A:5
Explanation : Here 000 is one to three octal digits(0….7) means there must be atleast one octal digit
after \ and maximum three.Here 072 is the octal notation, first it is converted to decimal notation that is
the ASCII value of char ‘:’. At the place of \072 there is : and the output is A:5.
// C program to illustrate \XHH escape
// sequence
#include <stdio.h>
int main(void)
char* s = "B\x4a";
printf("%s", s);
return 0;
Run on IDE
Output:
BJ
Explanation : Here hh is one or more hexadecimal digits(0….9, a…f, A…F).There can be more than
one hexadecimal number after \xHere’\x4a’ is a hexadecimal number and it is a single char. Firstly it
will get converted in to decimal notation and it is the ASCII value of char ‘J’. Therefore at the place of
\x4a, we can write J. So the output is BJ.
C/C++ Tokens
A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified
as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
1. Keyword: Keywords are pre-defined or reserved words in a programming language. Each
keyword is meant to perform a specific function in a program. Since keywords are referred names
for a compiler, they can’t be used as variable names because by doing so, we are trying to assign
a new meaning to the keyword which is not allowed. You cannot redefine keywords. However,
you can specify text to be substituted for keywords before compilation by using C/C++
preprocessor directives.C language supports 32 keywords which are given below:
2.
3. auto double int struct
4. break else long switch
5. case enum register typedef
6. char extern return union
7. const float short unsigned
8. continue for signed void
9. default goto sizeof volatile
10. do if static while
While in C++ there are 31 additional keywords other than C Keywords they are:
11. Identifiers: Identifiers are used as the general terminology for naming of variables, functions
and arrays. These are user defined names consisting of arbitrarily long sequence of letters and
digits with either a letter or the underscore(_) as a first character. Identifier names must differ in
spelling and case from any keywords. You cannot use keywords as identifiers; they are reserved
for special use. Once declared, you can use the identifier in later program statements to refer to
the associated value. A special kind of identifier, called a statement label, can be used in goto
statements.
There are certain rules that should be followed while naming c identifiers:
They must begin with a letter or underscore(_).
They must consist of only letters, digits, or underscore. No other special
character is allowed.
It should not be a keyword.
It must not contain white space.
It should be up to 31 characters long as only first 31 characters are significant.
Some examples of c identifiers:
NAME REMARK
_A9 Valid
C program:
void main()
int a = 10;