Skip to main content

Structure of C Program

Structure/Syntax of C program 

 Syntax: The syntax of a language describes the possible combinations of symbols that form a correct program.

Preprocessor Directive:

In C programming, a preprocessor directive is a command that instructs the compiler to perform specific actions before the actual compilation of the source code begins. Preprocessor directives start with the '#' symbol and are processed by the preprocessor, a built-in component of the C compiler. They are not part of the C language itself, but they are essential for performing various tasks before the actual code is compiled.

 Common preprocessor directives-

 #include: Used to include header files into the source code, allowing the program to access functions and declarations defined in those header files.

 #define: Used to define constants, macros, or function-like macros for use throughout the program. #ifdef, #ifndef, #else, #endif: Used for conditional compilation, allowing certain code blocks to be included or excluded based on predefined conditions. 

 #error: Used to generate a compilation error with a specified error message.

 #pragma: Provides additional instructions to the compiler for specific purposes, such as compiler-specific optimizations or configurations.

 Header Files: 

 Header files contain function prototypes, data type declarations, macro definitions, and other relevant information that allows the C program to access functions and features defined in external libraries or other source files. Header files have a '.h' extension and are typically included in C programs using the '#include' preprocessor directive.

Commonly used standard C library header files include: 

 stdio.h: Contains standard input/output functions like 'print()' and 'scan()'. 

 styli: Provides functions for dynamic memory allocation, random number generation, and other utility functions.

 math’s: Contains mathematical functions like 'sqrt()', 'sin()', 'cos()', and others.

 string.h: Contains functions for string manipulation, such as 'strcpy()', 'strcat()', 'strlen()', and more.

 Header files play a crucial role in organizing and modularizing code, allowing developers to easily reuse functions and declarations from external sources. They provide a way to separate interface declarations from the implementation. details of functions, promoting better code organization and maintainability in C programs.

 Comments: Comments are used to add explanatory notes or disable code segments.Single-line comments start with //, and multi-line comments start with /*and end with */.

 Main (): The main() function has the following characteristics: 

  •   Return Type: The main() function has a return type of int, which indicates the status of program execution. A return value of 0 indicates successful execution, while non-zero values indicate some error or abnormal termination.
  • Function Name: The function name must be main. It is case-sensitive, so Main or MAIN will not work.
  • Parameters: The main() function can accept two parameters: argc and argv. These parameters are used to pass command-line arguments to the program. The argc parameter represents the count of command line arguments, and argv is an array of strings containing the actual arguments. Note that you can omit these parameters if you don't need to process command-line arguments.
  • Body: The body of the main() function contains the statements and code that make up the program. Here, you can write the logic and functionality of your program using variable declarations, control structures, function calls, and more. 
  • Return Statement: The main() function usually ends with a return statement. A return value of 0 is commonly used to indicate successful program execution, while non-zero values can convey specific error codes or exceptional conditions. The return statement is optional; if omitted, the main() function implicitly returns 0 by default.

 An example of a C program 

 #include <stdio.h>

int main() {

 printf("Hello, world!\n");

 return 0; 

}

 In this example, the program starts executing from the main() function, which prints "Hello, world!" to the console using the printf() function. Finally, the return 0; statement indicates successful execution, and the program terminates. Understanding the structure and purpose of the main() function is essential for writing and running C programs. It allows us to control the flow of execution and define the behaviour of our program.

Comments

Popular posts from this blog

ARITHMETIC OPERATIONS ON BINARY NUMBER

Binary arithmetic is simpler to learn because binary number system deals with only two digits - 0 and 1. Binary number perform all arithmetic operation such as Addition, Substraction, Multiplication and compliment as in decimal number system. Binary Addition The addition table for binary arithmetic consist of only the following four entries: 0+0=0 0+1=1 1+0=1 1+1=0 , plus a carry of 1 to the next column.     1011        + 1100    10111     1100    + 1100     11000 Binary Substraction There are three ways to do substraction between binary numbers:- 1. Simple Substraction 2. Substraction using 1's Complement 3. Substraction using 2's Complement Simple Substraction: The complete table of binary substraction is as follows: 0-0=0 0-1=1, borrow 1 from next column 1-0=1 1-1=0    1101 - 0101   1000     1001   - 0111    0...

Media Of Communication Part 2

Unguided Media Unguided media transport electromagnetic wave without using a physical connection. This type of communication referred as wireless communication. We can divided wireless transmission in :-   1. Radio Wave:- Electromagnetic   waves ranging in frequencies between 3KHz and 1GHz are normally called radio wave.   On radio wave connectivity used communication used Bluetooth, Wi-Fi, etc.     Bluetooth :- It is a wireless network connectivity that enables to connect or transfer data between devices over a short range the physical range of connection up to 100 meter. Ex- Mobile phone, barcode scanning, portable speaker, wireless keyboard, mouse etc.      Wi-Fi (Wireless Fidelity):- Wi-Fi is a network of wireless connection. Wi-Fi network establish by radio frequency like Bluetooth, but has more power, resulting a strong connection.    ·   Wi-Fi is also called wireless Ethernet. It has connectivity range up...

Application OF Computer

Communication : - In the past, people used to communication through letters, telephones etc. The advent of computers has made it possible to share or exchange information in real time (instant). Information can be textual, graphics or visual from any corner of the world in different ways. Like – Email, Online chatting, Video conferencing, FTP (File Transfer Protocol), Telnet, Social Networking. News & Broadcasting : The delivery and reception of news has changed dramatically with the advent of computers. Earlier, Journalists used snail mail, reel-to-reel recorders, dark-room processing for photos and typewriters for composing contents. But now computers have facilitated in Instant Broadcast, Online Newspapers, Online News Channels, etc. Social Networking: Before the advent of computer, people used to interact with friends and relatives in closed groups and meet them during family functions, religious ceremonies and gatherings at local temples, gurudwaras, mosques or church...