C Programming Basics 1

BASICS OF C PROGRAMMING LANGUAGE....😊






C 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 the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development. 
Many later languages have borrowed syntax/features directly or indirectly from the C language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based on the C language. C++ is nearly a superset of C language (Few programs may compile in C, but not in C++).

1. Structure of C Program

A C program typically consists of several parts, including a preprocessor directive, a global variable declaration, a function declaration, and a main function. Here is a general outline of a C program:




#include <stdio.h>

// Preprocessor directive
#define MAX_SIZE 100

// Global variable declaration
int array[MAX_SIZE];

// Function declaration
int sum(int arr[], int size);

// Main function
int main() {
  // Code to read input and initialize variables
 
  int result = sum(array, MAX_SIZE);
 
  // Code to print result
 
  return 0;
}


// Function definition
int sum(int arr[], int size) {
  // Code to calculate the sum of the elements in the array
}


The preprocessor directive, indicated by #include, is used to include header files in the program. The global variable declaration is used to declare any global variables that will be used throughout the program. The function declaration is used to declare any functions that will be used in the program. The main function is the entry point of the program, and it contains the code that will be executed when the program is run. The function definition is where the code for the function is written.


2. Global Variable Declaration

In the C programming language, a global variable is a variable that is declared outside of any function and is available for use in any function in the program. Here is an example of a global variable declaration in C:

// Global variable declaration
int global_variable = 5;

// Function that uses the global variable
void some_function() {
  // Use the global variable
  printf("Global variable: %d\n", global_variable);
}


In this example, the global variable global_variable is declared and initialized to the value 5. The some_function function is able to access this global variable and print its value to the screen

3. A Function Declaration in C Language

In C, a function declaration typically follows this syntax:

return_type function_name(parameter1_type parameter1_name, parameter2_type parameter2_name, ...)


For example:


int addTwoNumbers(int num1, int num2)

This declares a function named addTwoNumbers that takes two int parameters named num1 and num2, and returns an int value.

Note that a function declaration does not include the function's implementation (i.e., the code that the function executes when it is called). It only provides information about the function's name, parameters, and return type. The function's implementation is typically provided in a separate function definition.


4. Main Function in C

A main function is the entry point of a C program. It is the point at which execution of the program begins. Every C program must have a main function, and the execution of a C program always starts at the main function. Here is an example of a main function in C:

int main(void)
{
// code goes here

return 0;
}

The main function is defined with the int keyword, which indicates that the function will

 return an integer value. The void keyword in the parentheses indicates that the function 

does not take any arguments. 

The code inside the body of the function, between the opening 

and closing braces, is where the program's instructions are written.

In the example above, the main function ends with a return statement, which returns the integer

 value 0 to indicate that the program ran successfully. 

The value returned by the main function is often referred to as the "exit status" of the program.



Comments

Popular Posts