- This topic has 0 replies, 1 voice, and was last updated 2 years, 5 months ago by
Yusuf.
- AuthorPosts
- February 28, 2020 at 12:58 pm #86809Spectator@yusuf
C is a general-purpose procedural programming language, developed by Dennis Ritchie between 1972 and 1973 at Bell Labs. It has a simple syntax and considered a beginner-friendly programming language. A low-level feature like memory access makes C suitable for writing operating systems or compilers. It is believed that many modern high-level programming languages like JavaScript, PHP, and so many other languages got their basis from C programming language.
C++ is believed to be the superset of C, and possesses features like Object Oriented Programming (Supporting objects, classes, inheritance, etc), unlike the procedural nature of C. Both C and C++ are believed to be the oldest programming languages that are still relevant in modern times. Although many believe C++ was derived from C but possesses more abilities, we can’t conclude that C++ is a better option at every given time.
Starting programming with C
1. Structure of a C program
Let us take a glance and see how a typical program in C looks like. Every program written in C should be written exactly in this structure. Writing a C program in another way will lead to a compile-time error.
i. Header Files
At the beginning of every C program, the notable component is the header files. A header file denoted by a *.h extension is a file that contains C functions and macro definitions, which is to be shared among many source files. There are two categories of a header file. The one written by a programmer, and the one that comes with the C compiler. A header file must be preceded by a #include directive, as seen in the image above. Some examples of header files include.
- string.h – String header, defines string manipulating functions.
- stdio.h – Standard input-output header, contain core input and output functions.
- conio.h – Console input-output header, contains functions for inputting and outputting data in the console.
- time.h -Time header defines macros and functions used in time manipulation.
- math.h – Math header, define mathematical functions.
- assert.h – Diagnostic or test functions.
- complex.h – Defines some set of functions for manipulating complex numbers.
and so many others.
From the list above, we have seen that every header file provides functions that perform some sort of task. Check the syntax for including a header file below.
ii. Main function declaration
An important component in a C program is the main function. It is the entry point of execution to all functions written in the program.
iii. Variable declaration
In C, before any variable is used, it must first be declared. A variable declaration doesn’t take up space in the memory, space is allocated upon variable definition. Below we declare an integer variable holding a value of 10.
iv. Body
The body of a program is the main purpose the program is written. It can be just to print “Hello World”, or a sorting algorithm that sorts and cleans an array. In our case, we only printed an integer variable content, which is 10. and returned 0, to exit the code.
v. Return statement
A very important component in any C program is the return statement. This statement returns a value based on the function return type. If a function’s return type is int, the return statement should return an integer value.
2. Writing your first C program
Its time we use what we’ve learned about the structure of a C program, and write a simple program that prints Hello World.
#include int main(){ printf("Hello World"); return 0; }
Let’s study the program in detail.
1. #include
This line is responsible for copying the preprocessed code of stdio.h into our source file. In stdio.h there’s a function we need to print text to the output console, and a lot of others needed to make standard input-output tasks easier, in fact, there are about 35 functions defined in the stdio.h file purposely for input-output. The compiler looks for a file called “stio.h” and copies all the content, replacing the
#include
. In our case, the#include
gives us the ability to write “Hello World” using theprintf()
function in our program.2. int main()
The main function is the entry point of execution in C. Every compiled C code that is needed to execute must be written in or called from
main()
function. the int before main is the return type of the function, it is also the status that describes the program termination. Since the function must return an integer, 0 stands as success status to show that the program ran and exited successfully.3. curly brackets
Curly brackets are used to enclose a body of code. They define the scope of your code they are used in loops, if statements, functions, etc. Curly brackets also called braces, they usually define the boundary of the loop, function, or what have you. The start ({) and end (}) signifies a body of code that is scoped.
4. printf(“Hello World”)
This line is responsible for outputting “Hello World” on the console. recall, in line 1. The #include makes it possible for us to output our “Hello World” using the
printf()
function. The trailing semicolon is used after every statement as a terminator. the compiler knows that the statement has ended once it sees a semicolon.5. return 0
As discussed earlier this return statement from main, gives the exit status of your program. zero (0) is a value that denotes a successful exit. The semicolon is the line terminator.
Executing your C program
CodeBlocks is a very good C and C++ IDE (Integrated Development Environment). It is free, opensource, and it offers extensibility. It gives the essential tools needed by developers, with features such as syntax highlighting, code folding, code completion, smart indents, and so many others are found in CodeBlocks. It is available for Windows, Linux, and Mac. For more details, see here.
Apart from CodeBlocks, Devcpp is another good C and C++ IDE. Like CodeBlocks, Devcpp is also free and opensource. Check it out here.You can also run your C program easily online, you don’t need to make a fresh installation of a C compiler on your machine. There are a number of compilers available on the internet, to run your C programs. Online GDB compiler and IDEone are good places to start. Online IDEs are good options because your code can be accessed from anywhere, there is also less consideration about hardware limitations. Since it is stored on the cloud, there is no need for any setup or configuration, all you need is a web browser. This is the concept that made coding on low-end devices possible, which used to be unsuitable before now.
- AuthorPosts
- You must be logged in to reply to this topic.