C Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

C programming

Description

printf()
include <stdio.h>

int main() {

// printf() displays the content inside the double quotes

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

return 0;

Explanation

Command

Description

#include <stdio.h>
It is a preprocessor command which consists of a standard input output header
file(stdio.h) before compiling a C program.

int main()

The main function from where execution of any C program starts.

Indicates the beginning of the main function.

/*_some_comments_*/

Any content inside “/* */” will not be used for compilation and execution.

printf(“Hello_World! “);

Prints the output on the screen.

getch();

Used for any character input from keyboard.

return 0;
Terminates a C program or main function and returns 0.

Indicates the end of the

Basic Commands in C Programming

Command

Description

printf()

Outputs formatted text to the console or other output stream.

scanf()

Reads formatted input from the console or other input stream.

if

Tests a condition and executes a block of code if the condition is true.


else

Executes a block of code if the condition tested by an if statement is false.

while

Repeatedly executing a block of code while a condition is true.

do-while

Repeatedly executes a block of code at least once and then continues to execute
the block as long as a condition is true.

for

Repeatedly executing a block of code a fixed number of times based on a counter


variable and a condition.

switch

Tests a variable against a series of case statements and executes a block of code
corresponding to the first matching case.

break

Terminates the execution of a loop or switch statement.


continue

Skips the current iteration of a loop and continues with the next iteration.

return

Exits a function and returns a value to the calling code.

typedef

Creates a new data type based on an existing data type.

struct

Defines a composite data type that groups together variables of different types.

enum

Defines a set of named integer constants.

#define

Defines a macro that can replace text in the source code with a predefined value.

How does a C Program Work?


1.Writing the code: In this step, we write the source code for our program using a
text editor or an IDE. For example, let's say we want to write a simple program
that prints the message "Hello, world!" to the console. Here is what the code
might look like:

#include <stdio.h>

int main() {

printf("Hello, world!\n");

return 0;

2. Compiling the code: Once it has been written, we need to compile it using a C
compiler. For example, we might use the GCC compiler on a Linux system. We
would run the following command from the terminal:

gcc -o hello_world hello_world.c

3. Linking the code: In this step, the C compiler may link our code with any
necessary libraries or other code modules. Our simple "Hello, world!" program
requires no additional libraries, so this step is relatively straightforward.
4. Running the program: With our program compiled and linked, we can now run
it. On a Linux system, we would run the following command from the terminal:

./hello_world

This would execute the hello_world binary and print the message "Hello, world!"
to the console.

5. Input and output: In our "Hello, world!" program, there is no input required,
and the only output is the message printed to the console.

6. Memory management: Our simple program does not require dynamic memory
allocation or deallocation, so memory management is not a concern.

7. Debugging and testing: Finally, we want to test and debug our program to
ensure it is working correctly. For a simple program like this, we might manually
run it and verify that the output is correct. For more complex programs, we might
use debugging tools like gdb or automated testing frameworks to help identify
and fix issues.

Basic Commands in C Programming

Command

Outputs formatted text to the console or other output stream.

scanf()

Reads formatted input from the console or other input stream.

if

Tests a condition and executes a block of code if the condition is true.

else

Executes a block of code if the condition tested by an if statement is false.

while

Repeatedly executing a block of code while a condition is true.


do-while

Repeatedly executes a block of code at least once and then continues to execute
the block as long as a condition is true.

for

Repeatedly executing a block of code a fixed number of times based on a counter


variable and a condition.

switch

Tests a variable against a series of case statements and executes a block of code
corresponding to the first matching case.

break

Terminates the execution of a loop or switch statement.

continue

Skips the current iteration of a loop and continues with the next iteration.

return

Exits a function and returns a value to the calling code.


typedef

Creates a new data type based on an existing data type.

struct

Defines a composite data type that groups together variables of different types.

enum

Defines a set of named integer constants.

#define

Defines a macro that can replace text in the source code with a predefined value

Terminology

Definition

Variable

A named location in memory that stores a value. Variables in C have a data type
that determines the type of data they can store.
Data Type

A classification of data that determines the size and type of operations that can be
performed on that data. C has several built-in data types, including integer,
floating-point, and character.

Function

A named block of code that performs a specific task. Functions can accept input
parameters and return a value to the calling code.

Pointer

A variable that stores the memory address of another variable. Pointers are a
powerful feature of C that allows for dynamic memory allocation and efficient
manipulation of data structures.

Array

A collection of variables of the same data type that are stored sequentially in
memory. Arrays can be used to store and manipulate large sets of data.

Struct

A composite data type that groups together variables of different types under a
single name. Structs are commonly used to represent complex data structures.
Operator

A symbol or keyword that performs a specific operation on one or more values.


Examples of operators in C include arithmetic operators (+, -, *, /), comparison
operators (==, !=, <, >), and logical operators (&&,

Loop

A programming construct that allows a block of code to be executed repeatedly.


The most common types of loops in C are the for loop, while loop, and do-while
loop.

Condition

A logical expression that evaluates to either true or false. Conditions are used in
control structures, such as if statements and loops, to determine which code is
executed.

Control Structure

A programming construct that allows the flow of control to be altered based on


certain conditions. Examples of control structures in C include if statements,
switch statements, and loops.

Header File

A file that contains declarations for functions, variables, and data types that are
used in a C program. Header files are typically included in a C source file using the
#include directive.
Preprocessor Directive

A special instruction that is processed by the C preprocessor before the source


code is compiled. Examples of preprocessor directives in C include #define,
#include, and #ifdef.

Compiler

A program that translates source code into machine code that a computer can
execute. C programs are typically compiled using a C compiler such as GCC or
Clang.

Linker

A program combines object files generated by the compiler into an executable


program. The linker resolves symbols and references between object files to
create a single executable program.

Library

A collection of precompiled object files can be linked with a C program to provide


additional functionality. C comes with a standard library that provides functions
for common tasks such as input/output, string manipulation, and memory
management.

You might also like