Ad Code

Start Programing with { C } Part: #1.01

Introduction to C Language Code:1.01
C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Equipment Corporation PDP-11 computer in 1972.

The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.

  • Easy to learn
  • Structured language
  • It produces efficient programs.
  • It can handle low-level activities.
  • It can be compiled on a variety of computers.

Facts about C

  • C was invented to write an operating system called UNIX.
  • C is a successor of B language which was introduced around 1970
  • The language was formalized in 1988 by the American National Standard Institue (ANSI).
  • By 1973 UNIX OS almost totally written in C.
  • Today C is the most widely used System Programming Language.
  • Most of the state of the art software have been implemented using C

Why to use C ?

C was initially used for system development work, in particular the programs that make-up the operating system. C was adoped as a system development language because it produces code that runs nearly as fast as code written in assembly 

 C Program File

All the C programs are writen into text files with extension ".c" for example hello.c.


C Compilers

When you write any program in C language then to run that program you need to compile that program using a C Compiler which converts your program into a language understandable by a computer. This is called machine language (ie. binary format). So before proceeding, make sure you have C Compiler available at your computer. It comes alongwith all flavors of Unix and Linux.

If you are working over Unix or Linux then you can type gcc -v or cc -v and check the result. You can ask your system administrator or you can take help from anyone to identify an available C Compiler at your computer.

If you don't have C compiler installed at your computer then you can use below given link to download a GNU C Compiler and use it.

Getting started with C Language

Hello World

To create a simple C program which prints "Hello, World" on the screen, use a text editor to create a new file (e.g. hello.c — the file extension must be .c) containing the following source code:

hello.c

#include <stdio.h>

int main(void)

{

puts("Hello, World");

return 0;

}

O/p: Hello,World

Let's look at this simple program line by line

#include <stdio.h>

This line tells the compiler to include the contents of the standard library header file stdio.h in the program. Headers are usually files containing function declarations, macros and data types, and you must include the header file before you use them. This line includes stdio.h so it can call the function puts().

See more about headers.

int main(void)

This line starts the definition of a function. It states the name of the function (main), the type and number of arguments it expects (void, meaning none), and the type of value that this function returns (int). Program execution starts in the main() function.

{

...

}

The curly braces are used in pairs to indicate where a block of code begins and ends. They can be used in a lot of

ways, but in this case they indicate where the function begins and ends.

puts("Hello, World");

This line calls the puts() function to output text to standard output (the screen, by default), followed by a newline.

The string to be output is included within the parentheses.

"Hello, World" is the string that will be written to the screen. In C, every string literal value must be inside the double quotes "...".

See more about strings.

In C programs, every statement needs to be terminated by a semi-colon (i.e. ;).

return 0;

When we defined main(), we declared it as a function returning an int, meaning it needs to return an integer. In this example, we are returning the integer value 0, which is used to indicate that the program exited successfully. After the return 0; statement, the execution process will terminate.

Editing the program

Simple text editors include vim or gedit on Linux, or Notepad on Windows. Cross-platform editors also include Visual Studio Code or Sublime Text.

The editor must create plain text files, not RTF or other any other format.

Compiling and running the program

To run the program, this source file (hello.c) first needs to be compiled into an executable file (e.g. hello on Unix/Linux system or hello.exe on Windows). This is done using a compiler for the C language.

See more about compiling

Compile using GCC

GCC (GNU Compiler Collection) is a widely used C compiler. To use it, open a terminal, use the command line to navigate to the source file's location and then run:

gcc hello.c -o hello

If no errors are found in the the source code (hello.c), the compiler will create a binary file, the name of which is given by the argument to the -o command line option (hello). This is the final executable file.

We can also use the warning options -Wall -Wextra -Werror, that help to identify problems that can cause the program to fail or produce unexpected results. They are not necessary for this simple program but this is way of adding them:

gcc -Wall -Wextra -Werror -o hello hello.c

Using the clang compiler

To compile the program using clang you can use:

clang -Wall -Wextra -Werror -o hello hello.c

By design, the clang command line options are similar to those of GCC.

Using the Microsoft C compiler from the command line

If using the Microsoft cl.exe compiler on a Windows system which supports Visual Studio and if all environment variables are set, this C example may be compiled using the following command which will produce an executable hello.exe within the directory the command is executed in (There are warning options such as /W3 for cl, roughly analogous to -Wall etc for GCC or clang).

cl hello.c

Executing the program

Once compiled, the binary file may then be executed by typing ./hello in the terminal. Upon execution, the compiled program will print Hello, World, followed by a newline, to the command prompt.

Ad Code

Responsive Advertisement