c

Synopsis

C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.

Data Types

Data TypeDescriptionSize
charCharacter1 byte
intInteger2 or 4 bytes
floatFloating point4 bytes
doubleFloating point8 bytes
voidVoid1 byte

Operators

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
++Incrementa++
Decrementa—
=Assignmenta = b
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b

Control Flow

StatementDescription
ifExecutes a block of code if a condition is true
if…elseExecutes a block of code if a condition is true, otherwise executes another block of code
switchExecutes a block of code depending on the value of a variable
whileExecutes a block of code while a condition is true
do…whileExecutes a block of code while a condition is true, and then executes the code once more

Functions

FunctionDescription
printf()Prints formatted output
scanf()Reads formatted input
strlen()Returns the length of a string
strcpy()Copies a string
strcat()Concatenates two strings
strcmp()Compares two strings
strrev()Reverses a string
strlwr()Converts a string to lowercase
strupr()Converts a string to uppercase
getch()Reads a character from the keyboard
getche()Reads a character from the keyboard and displays it on the screen
putch()Writes a character to the screen

Arrays

FunctionDescription
int a[10];Declares an array of 10 integers
int a[5] = {1, 2, 3, 4, 5};Declares an array of 5 integers and initializes it
int a[] = {1, 2, 3, 4, 5};Declares an array of 5 integers and initializes it
int a[5] = {1, 2, 3};Declares an array of 5 integers and initializes the first 3 elements

Pointers

FunctionDescription
int *p;Declares a pointer to an integer
int *p = &a;Declares a pointer to an integer and initializes it to the address of a
int *p = a;Declares a pointer to an integer and initializes it to the address of a
int *p = NULL;Declares a pointer to an integer and initializes it to NULL
*p = 10;Assigns the value 10 to the variable pointed to by p
p++;Increments the pointer p by 1
p—;Decrements the pointer p by 1

Structures

FunctionDescription
struct student { int rollno; char name[20]; };Declares a structure named student
struct student s1;Declares a variable s1 of type student
struct student s1 = {1, “John”};Declares a variable s1 of type student and initializes it
struct student s1 = {.name = “John”, .rollno = 1};Declares a variable s1 of type student and initializes it
struct student s1; s1.rollno = 1; strcpy(s1.name, “John”);Declares a variable s1 of type student and initializes it

Unions

FunctionDescription
union student { int rollno; char name[20]; };Declares a union named student
union student s1;Declares a variable s1 of type student
union student s1 = {1};Declares a variable s1 of type student and initializes it

Enumerations

FunctionDescription
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };Declares an enumeration named week
enum week day;Declares a variable day of type week
enum week day = Tuesday;Declares a variable day of type week and initializes it

Bit Fields

FunctionDescription
struct { unsigned int widthValidated; unsigned int heightValidated; } status1;Declares a structure with two bit fields
struct { unsigned int widthValidated : 1; unsigned int heightValidated : 1; } status1;Declares a structure with two bit fields

Typedef

FunctionDescription
typedef int INTEGER;Declares a new type named INTEGER
typedef struct { int rollno; char name[20]; } STUDENT;Declares a new type named STUDENT

Preprocessor Directives

DirectiveDescription
#includeIncludes a header file
#defineDefines a macro
#undefUndefines a macro
#ifdefChecks if a macro is defined
#ifndefChecks if a macro is not defined
#ifChecks if a condition is true
#elseExecutes a block of code if a condition is false
#elifChecks if a condition is true
#endifEnds an if statement
#errorGenerates an error
#pragmaGenerates a compiler directive

Input/Output

FunctionDescription
printf()Prints formatted output
scanf()Reads formatted input
fprintf()Prints formatted output to a file
fscanf()Reads formatted input from a file
sprintf()Prints formatted output to a string
sscanf()Reads formatted input from a string
puts()Prints a string
gets()Reads a string
putchar()Prints a character
getchar()Reads a character

String Functions

FunctionDescription
strlen()Returns the length of a string
strcpy()Copies a string
strcat()Concatenates two strings
strcmp()Compares two strings
strrev()Reverses a string
strlwr()Converts a string to lowercase

Math Functions

FunctionDescription
abs()Returns the absolute value of an integer
fabs()Returns the absolute value of a floating point number
ceil()Returns the smallest integer greater than or equal to a floating point number
floor()Returns the largest integer less than or equal to a floating point number
sqrt()Returns the square root of a number
pow()Returns the value of a number raised to a power
exp()Returns the value of e raised to a power

Time Functions

FunctionDescription
time()Returns the current time
clock()Returns the number of clock ticks elapsed since the program was launched
difftime()Returns the number of seconds between two times
mktime()Converts a time structure to a time value
asctime()Converts a time structure to a string
ctime()Converts a time value to a string
gmtime()Converts a time value to a time structure in UTC
localtime()Converts a time value to a time structure in the local time zone

File Functions

FunctionDescription
fopen()Opens a file
fclose()Closes a file
fgetc()Reads a character from a file
fputc()Writes a character to a file
fgets()Reads a string from a file
fputs()Writes a string to a file
fread()Reads data from a file
fwrite()Writes data to a file
fseek()Sets the position indicator associated with a file
ftell()Returns the current value of the position indicator of a file
rewind()Sets the position indicator associated with a file to the beginning of the file
remove()Deletes a file
rename()Renames a file
tmpfile()Creates a temporary file
tmpnam()Generates a temporary file name