0% found this document useful (0 votes)
49 views

Basics of C Language

C is a procedural programming language initially developed by Dennis Ritchie in 1972. It was mainly developed as a system programming language to write operating systems. C has features like low-level access to memory and a simple set of keywords that make it suitable for system programming tasks like operating system and compiler development. The typical structure of a C program includes header files, a main function, variable declarations, program logic in the function body, and a return statement.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Basics of C Language

C is a procedural programming language initially developed by Dennis Ritchie in 1972. It was mainly developed as a system programming language to write operating systems. C has features like low-level access to memory and a simple set of keywords that make it suitable for system programming tasks like operating system and compiler development. The typical structure of a C program includes header files, a main function, variable declarations, program logic in the function body, and a return statement.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

is a procedural programming language. It was initially developed by


Dennis Ritchie in the year 1972. It was mainly developed as a system
programming language to write an operating system. The main features of C
language include low-level access to memory, a simple set of keywords, and
clean style, these features make C language suitable for system
programming like an operating system or compiler development.

Beginning with C programming:

1. Structure of a C program
After the above discussion, we can formally assess the structure of a C program.
By structure, it is meant that any program can be written in this structure only.
Writing a C program in any other structure will hence lead to a Compilation Error.
The structure of a C program is as follows:

The components of the above structure are:


1. Header Files Inclusion: The first and foremost component is the inclusion of the
Header files in a C program.
A header file is a file with extension .h which contains C function declarations
and macro definitions to be shared between several source files.
Some of C Header files:
 stddef.h – Defines several useful types and macros.
 stdint.h – Defines exact width integer types.
 stdio.h – Defines core input and output functions
 stdlib.h – Defines numeric conversion functions, pseudo-random network
generator, memory allocation
 string.h – Defines string handling functions
 math.h – Defines common mathematical functions
Syntax to include a header file in C:
#include
2. Main Method Declaration: The next part of a C program is to declare the main()
function. The syntax to declare the main function is:
Syntax to Declare main method:
int main()
{}
3. Variable Declaration: The next part of any C program is the variable
declaration. It refers to the variables that are to be used in the function. Please
note that in the C program, no variable can be used without being declared. Also
in a C program, the variables are to be declared before any operation in the
function.
Example:
int main()
{
int a;
.
.
4. Body: Body of a function in C program, refers to the operations that are
performed in the functions. It can be anything like manipulations, searching,
sorting, printing, etc.
Example:
int main()
{
int a;

printf("%d", a);
.
.
5. Return Statement: The last part in any C program is the return statement. The
return statement refers to the returning of the values from a function. This return
statement and return value depend upon the return type of the function. For
example, if the return type is void, then there will be no return statement. In any
other case, there will be a return statement and the return value will be of the
type of the specified return type.
Example:

int main()
{
int a;

printf("%d", a);

return 0;
}

C Basic commands Explanation

#include <stdio.h> This command includes standard input output header


file(stdio.h) from the C library before compiling a C program

int main() It is the main function from where C program execution begins.

{ Indicates the beginning of the main function.

/ Whatever written inside this command “/*   */” inside a C


*_some_comments_*/ program, it will not be considered for compilation and execution.

printf(“Hello_World! “); This command prints the output on the screen.

getch(); This command is used for any character input from keyboard.

return 0; This command is used to terminate a C program (main function)


and it returns 0.

} It is used to indicate the end of the main function.

Program to Display "Hello, World!"

#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}

Output

Hello, World!

How "Hello, World!" program works?


 The #include is a preprocessor command that tells the compiler to include
the contents of stdio.h (standard input and output) file in the program.
 The stdio.h file contains functions such as scanf() and printf() to take input
and display output respectively.
 If you use the printf() function without writing #include <stdio.h>, the
program will not compile.
 The execution of a C program starts from the main() function.
 printf() is a library function to send formatted output to the screen. In this
program, printf() displays Hello, World! text on the screen.
 The return 0; statement is the "Exit status" of the program. In simple
terms, the program ends with this statement.

2. int main() – Here main() is the function name and int is the return type of this function.
Every C program must have this function because the execution of program begins with
the main() function. The 0 return value of this function represents successful execution
of program while the return value 1 represents the unsuccessful execution of program.
This is the reason we have return 0; statement at the end of this main function.

Erros in c
Syntax errors: 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:
 Missing Parenthesis (})
 Printing the value of variable without declaring it
 Missing semicolon like this:

// C program to illustrate

// syntax error

#include<stdio.h>

Void main()

    int x = 10;

    int y = 15; 

      

    printf("%d", (x, y)) // semicolon missed

}
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;

  

    // logical error : a semicolon after loop

    for(i = 0; i < 3; i++);

    {

        printf("loop ");

        continue;

    } 

    getchar();

    return 0;

}
Precedence of operators
The precedence of operators determines which operator is executed first if
there is more than one operator in an expression.
Let us consider an example:

int x = 5 - 17* 6;

In C, the precedence of  *  is higher than  -  and  = . Hence,  17 * 6  is evaluated
first. Then the expression involving  -  is evaluated as the precedence of  -  is
higher than that of  = .
Here's a table of operators precedence from higher to lower. The property
of associativity will be discussed shortly.

Operators Precedence & Associativity Table

Operator Meaning of operator Associativity

() Functional call
[] Array element reference
Left to right
-> Indirect member selection
. Direct member selection
! Logical negation
~ Bitwise(1 's) complement
+ Unary plus
- Unary minus
++ Increment
Right to left
-- Decrement
& Dereference (Address)
* Pointer reference
sizeof Returns the size of an object
(type) Typecast (conversion)

* Multiply
/ Divide Left to right
% Remainder

+ Binary plus(Addition)
Left to right
- Binary minus(subtraction)

<< Left shift


Left to right
>> Right shift

< Less than


<= Less than or equal
Left to right
> Greater than
>= Greater than or equal

== Equal to
Left to right
!= Not equal to

& Bitwise AND Left to right

^ Bitwise exclusive OR Left to right

| Bitwise OR Left to right

&& Logical AND Left to right


|| Logical OR Left to right

?: Conditional Operator Right to left

= Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference Right to left
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift

, Separator of expressions Left to right


10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30

You might also like