Chapter 3 My first C Program

 

no-pain-no-gain-in-programming-language-code

Contributed By: Shehrevar Davierwala

A C program, whatever its size, consists of functions and variables. A function contains
statements that specify the computing operations to be done, and variables store values
used during the computation.

Below is the simple program in C

1 /* First C program: Hello World */
2 #include <stdio.h>
3
4 int main(void)
5 {
6 printf(“Hello World!\n”);
7 }

(Please avoid writing line numbers ūüôā )


Explanation


1 start with /* and are terminated with */. They can span multiple lines and are not
nestable. For example,
/* this attempt to nest two comments /* results in just one comment,
ending here: */ and the remaining text is a syntax error. */
2 Inclusion of a standard library header-file. Most of C’s functionality comes from libraries.             Headerfiles contain the information necessary to use these libraries, such as function         declarations  and macros.
4 All C programs have main() as the entry-point function. This function comes in two forms:
   int main(void)
   int main(int argc, char *argv[])
The first takes no arguments, and the second receives command-line arguments from the environment¬†in which the program was executed‚ÄĒtypically a command-shell.

The function returns a value of type int (i.e., an integer).

5 and 7 The braces { and } delineate the extent of the function block. When a function completes,               the program returns to the calling function. In the case of main(), the program terminates             and control returns to the environment in which the program was executed. The integer                 return value of main() indicates the program’s exit status to the environment, with 0                     meaning normal termination.

6 This program contains just one statement: a function call to the standard library function                printf(), which prints a character string to standard output (usually the screen). Note, printf() is      not a part of the C language, but a function provided by the standard library                       (declared in header stdio.h).
The standard library is a set of functions mandated to exist on all systems conforming to the ISO C
standard. In this case, the printf() function takes one argument (or input parameter): the string
constant “Hello World!\n”. The \n at the end of the string is an escape character to start a new
line. Escape characters provide a mechanism for representing hard-to-type or invisible characters
(e.g., \t for tab, \b for backspace, \” for double quotes). Finally, the statement is terminated ¬† ¬† ¬†with¬†a semicolon (;). C is a free-form language, with program meaning unaffected by whitespace ¬† ¬† in most¬†circumstances. Thus, statements are terminated by ; not by a new line.

Note::¬†You may notice in the example program above, that main() says it returns int in its interface declaration, but¬†in fact does not return anything; the function body (lines 5‚Äď7) contains no return statement. The reason is that for¬†main(), and main() only, an explicit return statement is optional.


Outlines


1 What is a header file in C?

A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files.

A header file is a file with extension .h

Example: <stdio.h>
/* Include files for input/output functions*/

Both the user and the system header files are included using the preprocessing directive #include. It has the following two forms ‚ąí

#include <file>

This form is used for system header files. It searches for a file named ‘file’ in a standard list of system directories. You can prepend directories to this list with the -I option while compiling your source code.

#include "file"

This form is used for header files of your own program. It searches for a file named ‘file’ in the directory containing the current file. You can prepend directories to this list with the -I option while compiling your source code.

2 What is C preprocessing directive?

  • Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is called preprocessing.
  • Commands used in preprocessor are called preprocessor directives and they begin with ‚Äú#‚ÄĚ symbol.
  • Below is the list of preprocessor directives that C language offers.
S.no Preprocessor Syntax Description
1 Macro #define This macro defines constant value and can be any of the basic data types.
2 Header file inclusion #include <file_name> The source code of the file ‚Äúfile_name‚ÄĚ is included in the main program at the specified place
3 Conditional compilation #ifdef, #endif, #if, #else,  #ifndef Set of commands are included or excluded in source program before compilation with respect to the condition
4 Other directives #undef, #pragma #undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program.

Example program for #define, #include preprocessors in C:

  • #define ¬†– ¬†¬†This macro defines constant value and can be any of the basic data types.
  • #include <file_name> ¬†– ¬†¬†The source code of the file ‚Äúfile_name‚ÄĚ is included in the main C program where ‚Äú#include <file_name>‚ÄĚ is ¬†mentioned.

3 Name few commonly used header files in C

Standard File File Pointer Device
Standard input stdin Keyboard
Standard output stdout Screen
Standard error stderr Your screen

Below is the list of frequently used header files

<complex.h> (since C99) Complex number arithmetic
<ctype.h> Functions to determine the type contained in character data
<errno.h> Macros reporting error conditions
<fenv.h> (since C99) Floating-point environment
<float.h> Limits of float types
<inttypes.h> (since C99) Format conversion of integer types
<iso646.h> (since C95) Alternative operator spellings
<limits.h> Sizes of basic types
<locale.h> Localization utilities
<math.h> Common mathematics functions
<stdlib.h> General utilities: memory management, program utilities, string conversions, random numbers
<stdnoreturn.h> (since C11) noreturn convenience macros
<string.h> String handling
<tgmath.h>     Type-generic math (macros wrapping math.h and complex.h)
<threads.h> Thread library
<time.h> Time/date utilities
<uchar.h> UTF-16 and UTF-32 character utilities
<wchar.h> Extended multibyte and wide character utilities
<wctype.h> Wide character classification and mapping utilities

4 What is the use of main function in C programming?

In C, the “main” function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is “called” by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts.

5 What is the extension of C file with source code.?

The source code is saved with .C extension.

6. What is the use of Semicolon in C code.?

In a C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.

Given below are two different statements ‚ąí

printf("Hello, World! \n");
return 0;

About the author: admin

Leave a Reply

Your email address will not be published.