What is C?
C is one of the most popular programming language that is well structured (programming can be done in organized way), high-level (portable, easy to understand) and machine independent (no issue to run a program). This language is originated in 1972 and developed by Mr. Dennis Ritchie at laboratory named BELL.
C language has an important feature as it is the best suitable for using in both type of environment named as software for systems and packages for business purpose. There are some important points on C to remember that it is highly portable so that user can written a program on one computer system and run it on other computer system, computation has been done very fast while using C language. Total 32 keywords have been used in C. There are so many built-in type functions that increase the strength of the language. Let us look at the syntax as given below:
main( )
{
/*…….starting of printing data……*/
printf(“write data which you want to print”);
/*…….starting of printing data……*/
}
- It is necessary to use only one main function during programming so that the computer can easily understand from which it has to start.
- Empty pair ( ) of parenthesis has been indicating having no arguments.
- {, has been indicating the beginning of the function.
- }, has been indicating the ending of the function.
- { } are known as opening and closing brackets for any programming computation.
- The data which has been written under the opening and closing brackets has been known as a function body.
- /*…..*/ has been known as comments which usually ignored by the compiler at the time of compilation and execution.
- For printing, any output on a computer system print f( ) function has been used. It is a predefined library function of the C language.
- Predefined stands for the meaning of the previous compilation of programs.
- n stands for the characters which are used at the ending of string without spaces in them.
- ; has been indicating the ending of any statement in the function body.
Constants, Variables, and Data Types
Constants:
During the execution of a program, fixed values are represented as constants. They are not going to be changed throughout the program. Basically two types of constants are there: numeric and character. Further these numeric constant has been divided into two parts named as integer and real constants. Similarly, character constant has been divided into two parts named as single character and string constants. There are so many variables t (used for horizontal tab), n (used for new line), v (used for vertical tab), (define null) and many more as an example of character constant.
Variables:
For storing a data value variable has been used as a memory location identifier. C language is case sensitive means uppercase and lowercase both will be identified differently. For example AGE and age both have different meanings.
Data Types:
Before using a variable it is necessary for the compiler to know the type of the variable. There are so many data types in the C language. Each data types have a different range of values. For example, char data type comes under the range of -128 to 127, int data type comes under the range of -32768 to 32,767, and so on. Here is a list of fields along with the 32-bit GCC compiler format and memory requirements.
Data Type | Memory (in bytes) | Range | Format Specifier |
short int | 2 | -32,768 to 32,767 | %hd |
unsigned short int | 2 | 0 to 65,535 | %hu |
unsigned int | 4 | 0 to 4,294,967,295 | %u |
int | 4 | -2,147,483,648 to 2,147,483,647 | %d |
long int | 4 | -2,147,483,648 to 2,147,483,647 | %ld |
unsigned long int | 4 | 0 to 4,294,967,295 | %lu |
long long int | 8 | -(2^63) to (2^63)-1 | %lld |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 | %llu |
signed char | 1 | -128 to 127 | %c |
unsigned char | 1 | 0 to 255 | %c |
Operators and Expressions
Operators: For performing various mathematical computations and logical functions different types of symbol has been introduced, named operators. Arithmetic operators like “+” (addition),”-“(subtraction),”*” (multiplication),”/” (division),”%” (modulo operation). Relational operators like “<” (is less than),”>=” (is greater than and equal to),” ==” (is equal to),”!=” (is not equal to) and many more. Logical operators like “&&” (AND),”||” (OR),”!” (NOT). Apart from these there are Assignment operator (op=), Increment (++) and Decrement operators (--), ternary operator (? :) , Bitwise operators(AND, OR, XOR, NOT) and many more.
Expressions: Combination of constants, variables, and operators is called expression (arithmetic). For example x = a * bc; always starts from left to right when solving any mathematical computation.
Managing Input and Output Operation
Standard input and output function: Reading an input data can be done by scanf() and the printing of output on the terminal can be done using printf(). These are known as standard input and output functions. The header file which includes these function is known as <stdio.h>. printf( ) returns the total number of characters printed, or a negative value if there is an output or encoding error.
scanf( ) returns the total number of successfully scanned inputs or zero if an input failure occurs before the first receiving argument is allocated.
Control Flow
For controlling the flow of the program some special statements are used termed as decision making statements (control statements). They are of different types like: if, switch and many more. Here explaining about if statements only.
If statement: For controlling the flow of execution during the computation if statement stands as the main decision maker. True and false are the two possibilities as the result of an if condition.
If (condition)
{
Block of statement(s);
}
If the condition satisfies then it will be executed otherwise will skipped and jump to next statement.
If-else statement:
If (condition)
{
execute if true
}
Else
{
execute if false
}
There is so many data types that has been used for processing a large number of data, these are termed as an array, pointers, structures, strings, linked list and many more.
- Array: It is a collection of data of similar types (large-sized group).
- String: Characters that arranged in a sequential way as a single frame termed as a string.
- Structures: It is a collection of different data types (large-sized groups).
- Union: It used the same location for all the specified variables or data items.
- Pointers: For addressing the data values and memory it has been used.
- Linked list: It can be used as a linker from one item to another item.
Array and Strings in C
In C, an array is a list of identical data items stored in contiguous memory locations, with elements accessed at random using the array's indices. It can also be used to store a list of primitive types of data of any kind, such as int, float, double, char, and so on. Furthermore, C array can store derived data types such as structures, pointers, and so on.
When dealing with a limited object's number, normal-variables can be used, but when dealing with instances of large numbers, normal variables become difficult to control. An array's purpose is to show multiple instances in a single variable.
A string is described as a collection of characters. The only difference between a character array and a string is that a string ends with the special character as .
Pointers
Pointers store variables address or location of memory. We use the unary operator ampersand (&), which returns the variable address to a pointer, to access the variable's address. For instance &x gives us variable x address. Another operator is the unary Asterisk (*) for two purposes: Declaring a variable pointer, if a variable in C is declared, * must be given before its name. The unary operator (*) is used to obtain the data values situated at the address specified by its operand to access the value in the address. This is also known as derogation.
Linked List
Linked List is a linear data structure as are arrays. Unlike the arrays, the linked list elements are not stored in a contiguous location.
Arrays can be used to store similar linear data, but arrays are limited by the following.
- The array size is determined: We, therefore, need to know the upper limit on the number of elements in advance. In addition, the assigned memory is generally equal to the upper limit, regardless of the use.
- It is expensive to insert a new element into an array of items because space must be created and existing elements shifted in the space to create the element.
A pointer to the first node of a connected list is used to represent it. The head is the first node in the chain. The value of the head is NULL if the linked list is zero.
A list's nodes are made up of at least two parts:
- Data.
- A pointer to the next node (or a reference to it).
Structure and Union in C
A user-defined data type in C is called structure. A structure creates a type of data that can be used for grouping items of various types in one type. For creating a structure, “struct” keyword is used. A variable of structures may be declared as basic types with a structure declaration or as a separate declaration. With declaration, members of structure cannot be initialized. Curly braces { } can be used to initialize structure members. The dot operator (.) is used to access structure members. The Union is a user-defined type of data like Structures. All members in the union share the same place of memory. The union size is determined by the size of the biggest union member. Unions can also be beneficial for two or more members in many situations where the same memory is used.
Memory Management
The following parts make up a standard memory representation of a C programme.
- Text segment: A text segment, also known as a code segment or simply text, is one of the parts of a programme that includes executable instructions in an object file or memory.
- Data segment that has been initialized: A data segment is a section of a program's virtual address space that includes the programmer-initialized global variables and static variables.
- Uninitialized data section: The “bss” segment is an uninitialized data segment.
- Stack: Historically, the stack area was adjacent to the heap area and grew in the opposite direction; free memory was depleted when the stack pointer entered the heap pointer.
- Heap: The heap is the area where dynamic memory storage usually occurs. The heap region starts at the end of the BSS section and expands from there to larger addresses. Malloc(), Realloc(), and free() manage the Heap region.
Memory-Leak: When programmers build a heap memory and fail to erase it, a memory leak occurs. Memory leaks are especially problematic for programs like daemons and servers, which are never terminated by design. To prevent memory leaks, heap memory should be freed when it is no longer required.
File Handling
C programs are written on a prompt / terminal that is not saved. However, in the industry of software, the majority of applications are written to save the data retrieved from the application. One method is to save the retrieved data in a register. The following are some of the operations that can be performed on a file:
- Fresh file creation (with attributes like "a" or "a+" or "w" or "w++").
- using a previously saved file.
- Taking information from a file.
- Creating a file.
- Getting to a certain part of a file.
- Putting an end to a file.
Functions in C
A function is a collection of statements that take inputs, perform a particular calculation, and return the result. By nature, functions in C are global. When you use the static keyword before a function name, it becomes static. There are two types of functions: Pre-defined and User-defined. We already know that the main function is the heart of a program, and it is both pre-defined and user-defined function. The function which is calling a function is called the caller function. The parameter used at the time of calling a function is called actual parameter and the function use at the time of the definition is called the formal parameter. The Syntax of a function is:
return_type function_name ( parameters){ }
return type represents the data type of the value which will be the result of the function.
Keywords in C
The most commonly used Keyword in C are as follows:
- auto: States automatic variables with the auto keyword.
- break and continue: When they break expression is used, the innermost loop is immediately terminated. It's also used to bring the turn sentence to a close. The continue statement forgoes the statements that come after it in the loop's iteration.
- default, switch, and case: When a block of statements must be executed among several blocks, the switch and case statement are used.
- char: The keyword char declares a character variable.
- const: The const keyword can be used to declare an identifier as constant.
- double and float: The keywords double and float are used to declare variables of the floating kind.
- If and else: They are decision-making constructors used in C programming.
- enum: The keyword enum is used in C programming to declare enumeration forms.
- extern: Extern declares that a variable or attribute has external linkage outside of the file in which it is declared.
- goto: The goto statement is used to transfer the program's control to the designated mark.
- return: The return keyword recovers the value after the operation has been terminated.
- register: The register keyword generates register variables, which are significantly faster than regular variables.
- static: The static keyword is used to construct a static variable. The static variables' values are preserved until the end of the program.
- volatile: The volatile keyword is used to create objects that are volatile. The hardware can change a volatile object in an unspecified way.
- void: The keyword void means "nothing" or "no value."
- typedef: The typedef keyword is used to link a type to an identifier explicitly.
- sizeof : The sizeof keyword determines the data size.
Context and Applications
This topic is significant in the professional exams for both undergraduate and graduate courses, especially for
- Bachelor in Computer Science
- Masters in Computer Science
- Bachelor of Technology in Computer Science/ Information Technology
- Bachelor of Computer Application
Want more help with your computer science homework?
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.
C (Programming) Homework Questions from Fellow Students
Browse our recently answered C (Programming) homework questions.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.