C PROGRAMS C (programming language) C programming language is a general-purpose, imperative computer programming language, s...
C PROGRAMS
C (programming language)
C programming language is a general-purpose, imperative computer programming language, structural support programming, lexical variable scope and recursion, while static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore has found lasting use in applications that had previously been coded in assembly, including operating systems, as well as various applications for computers ranging from supercomputers to embedded systems .
C was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C) and subsequently by the International Organization for Standardization (ISO).
C is an imperative procedural language. It was designed to be compiled using a relatively straightforward compiler, to provide a low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require a minimum run-time support. Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standard compliant and portable written C program can be compiled for a wide range of computer platforms and operating systems with few changes to its source code. The language has become available on a wide range of platforms, from embedded microcontrollers to supercomputers.
Syntax
C has a formal grammar specified by the standard C. Line endings are generally not significant in C; However, linear boundaries have significant significance during the preprocessing phase. Comments may appear either between the delimiters / * and * /, or (since C99) following // until the end of the line. Comments delimited by / * and * / do not nest, and these sequences of characters are not interpreted as comment on whether they appear inside string or character literals.
C source files contain declarations and function definitions. Function definitions, in turn, contain declarations and statements. Declarations may define new types using keywords such as struct, union, and enum, or assign types to and possibly reserve storage for new variables, usually by type the type followed by the variable name. Keywords such as char and int specify built-in types. Sections of code are enclosed in braces ({and}, sometimes called "curly brackets") to limit the scope of declarations and to act as a single statement for control structures.
As an imperative language, C uses statements to specify actions. The most common statement is an expression statement, consisting of an expression to be evaluated, followed by a semicolon; have a side effect of the evaluation, functions may be called and variables may be assigned new values. To modify the normal sequential execution of statements, C provides several control-flow statements identified by reserved keywords. Structured programming is supported by if (-else) conditional execution and by do-while, while, and for iterative execution (looping). The for statement has different initialization, testing, and reinitialization expressions, any or all of which can be omitted. break and continue can be used to leave the innermost enclosing loop statement or skip to its reinitialization. There is also a non-structured goto statement which is directly directed to the designated label within the function. select switch to the executed case based on the value of an integer expression.
Expressions can use a variety of built-in operators and may contain function calls. The order in which arguments to operations and operands to most operators are evaluated is unspecified. The evaluations may even be interleaved. However, all side effects (including storage to variables) will occur before the next "sequence point"; The point is included in the end of each statement statement, and the entry to and return from each function call. Sequence points also occur during the evaluation of expressions containing certain operators (&&, ||,?: And the comma operator). This allows a high degree of object code optimization by the compiler, but requires C programmers to take more care to obtain reliable results than is needed for other programming languages.
Kernighan and Ritchie say in the introduction of The C Programming Language: "C, like any other language, has its blemishes. Some of the operators have the wrong precedence; some parts of the syntax could be better." The C standard did not try to correct many of these blemishes, because of the impact of such changes on already existing software.
Character set
The basic C source character set includes the following characters:
Lowercase and uppercase letters of ISO Basic Latin Alphabet: a-z A-Z
Decimal digits: 0-9
Graphic characters:! "#% & '() * +, -. /:; <=>? [\] ^ _ {|} ~
Whitespace characters: space, horizontal tab, vertical tab, form feed, newline
Newline shows the end of a text line; It needs not correspond to a real single character, though for convenience C treats it as one.
Additional multi-byte encoded characters may be used in string literals, but they are not entirely portable. The latest C standard (C11) allows multi-national Unicode characters to be embedded portably within C source text by using \ XXXX or \ XXXXXXXX encoding (although the X is a hexadecimal character), although this feature is not yet widely implemented.
The basic C execution character set contains the same characters, along with representations for alert, backspace, and carriage return. Run-time support for extended character sets has been increased with each revision of the standard C.
Reserved words
C89 has 32 reserved words, also known as keywords, which are the words that can not be used for any purpose other than those for which they are predefined:
|
C99 reserved five more words:
C11 reserved seven more words:
|
|
|
|
Most of the recent reserved words begin with a sub-letter followed by a letter, because the identifiers of that form were previously reserved by the C standard for use only by implementations. Since existing program source code should not have been used for these identifiers, it would not be affected when C implementations started to support these extensions to the programming language. Some standard headers do define more convenient synonyms for underscored identifiers. The language previously included a word name
entry
, but this was seldom implemented, and has now been removed as a reserve word.
List of C programs
C "Hello, World!" Program
A simple C program to display "Hello, World!" on the screen. Since, it's a very simple program, it is often used to illustrate the syntax of a programming language.
Input
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
Output
Hello, World!
How "Hello, World!" program works?
- The
#include <stdio.h>
is a preprocessor command. This command is compiled to include the contents ofstdio.h
(standard input and output) file in the program.
Thestdio.h
file contains functions such asscanf()
andprint()
to take input and display output respectively.
If you useprintf()
function without writing#include <stdio.h>
, the program will not be compiled. - The execution of a C program starts from the
main()
function. - The
printf()
is a library function to send formatted output to the screen. In this program, theprintf()
displays Hello, World! text on the screen. - The
return 0;
statement is the "Exit status" of the program. In simple terms, program ends with this statement.
C Program to Add Two Integers
In this program, user is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen.
Input
#include <stdio.h>
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
printf("Enter two integers: ");
// Two integers entered by user is stored using scanf() function
scanf("%d %d", &firstNumber, &secondNumber);
// sum of two numbers in stored in variable sumOfTwoNumbers
sumOfTwoNumbers = firstNumber + secondNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);
return 0;
}
Output
Enter two integers: 12 11 12 + 11 = 23
In this program, user is asked to enter two integers. Two integers entered by the user are stored in firstNumber and secondNumber variables respectively. This is done using
scanf()
function.
Then, firstNumber and secondNumber variables are added using + operator and the result is stored in sumOfTwoNumbers .
C Program to Find ASCII Value of a Character
In C programming, variable variable holds ASCII value (an integer number between 0 an 127) rather than character itself. You will learn how to find the ASCII value of a character in this program.
For example , ASCII value of 'A' is 65.
This means that, if you assign 'A' to a variable character, 65 is stored in that variable rather than 'A' itself.
Program to Print ASCII Value
Input
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
// Reads character input from the user
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output
Enter a character: G ASCII value of G = 71
In this program, user is asked to enter a character that is stored in variable c. The ASCII value of that character is stored in variable c instead of that variable itself.
When% d string format is used, 71 (ASCII value of 'G') is displayed.
When% c string format is used, 'G' itself is displayed.
C Program to Find the Size of int, float, double and char
This program describes 4 variables of type int, float, double and char. Then, the size of each variable is evaluated using sizeof operator.
Example: Program to Find the Size of a variable
Input
#include <stdio.h>
int main()
{
int integerType;
float floatType;
double doubleType;
char charType;
// Sizeof operator is used to evaluate the size of a variable
printf("Size of int: %ld bytes\n",sizeof(integerType));
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));
return 0;
}
Output
Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of char: 1 byte
In this program, 4 integerType variables , floatType , doubleType and charType are reported with int, float, double and char type respectively.
Then, the size of each variable is ascertained using sizeof operator.
C Program to Demonstrate the Working of Keyword Long
The long is a size modifier, indicated by keyword long, that can increase the size of a variable during declaration. This program will show the work of long keyword.
Example: Program to Demonstrate the Working of Long
Input
#include <stdio.h>
int main()
{
int a;
long b;
long long c;
double e;
long double f;
printf("Size of int = %ld bytes \n", sizeof(a));
printf("Size of long = %ld bytes\n", sizeof(b));
printf("Size of long long = %ld bytes\n", sizeof(c));
printf("Size of double = %ld bytes\n", sizeof(e));
printf("Size of long double = %ld bytes\n", sizeof(f));
return 0;
}
Output
Size of int = 4 bytes Size of long = 8 bytes Size of long long = 8 bytes Size of double = 8 bytes Size of long double = 16 bytes
In this program, the
sizeof
operator is used to find the size of int
, long
, long long
, double
and long double
. The long keyword can not be used with float
and char
type variables.C Program to Check Leap Year
This program checks whether an year (integer) entered by the user is a leap year or not. A leap year is exactly divisible by 4 except for the century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
Example: Program to Check Leap Year
Input
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
Output 1
Enter a year: 1900 1900 is not a leap year.
Output 2
Enter a year: 2012 2012 is a leap year.
COMMENTS