C is a popular programming language known for its versatility and efficiency. While C doesn’t have “commands” like a command-line interface, it does have various keywords, functions, and constructs that form the foundation of the language.
Here are some of the basic elements of C:
Table of Contents
Keywords
These are reserved words that have a specific meaning and purpose within the language. Some of the fundamental keywords in C include:
- int: declares an integer variable
- float: declares a floating-point variable
- double: declares a double-precision floating-point variable
- char: declares a character variable
- if, else: conditional statements
- for, while, do: loop constructs
- switch, case, default: multi-branch selection constructs
- break, continue: loop control statements
- return: returns a value from a function
- void: indicates a function without a return value or an empty parameter list
Functions
Functions are blocks of code designed to perform a specific task. Some standard library functions in C include:
- printf(): prints formatted output to the console
- scanf(): reads formatted input from the console
- strlen(): returns the length of a string
- strcpy(): copies one string to another
- strcmp(): compares two strings
- strcat(): concatenates two strings
- atoi(): converts a string to an integer
- atof(): converts a string to a floating-point number
Preprocessor Directives
These are instructions for the C preprocessor, which is a program that processes the source code before compilation. Some common preprocessor directives are:
- #include: includes a header file containing function declarations, macros, and other definitions
- #define: defines a macro, which is a symbolic constant or a function-like construct
- #ifdef, #ifndef, #else, #endif: conditional compilation directives, allowing for parts of the code to be compiled only under certain conditions
- #pragma: provides additional information or directives to the compiler
Variables
Variables store values in C. You need to declare a variable with a specific data type before using it. Some basic data types in C are:
- int: integer values
- float: single-precision floating-point values
- double: double-precision floating-point values
- char: character values
Operators
Operators perform operations on variables and values. Some fundamental operators in C include:
- Arithmetic operators: +, -, *, /, %
- Relational operators: ==, !=, >, <, >=, <=
- Logical operators: &&, ||, !
- Bitwise operators: &, |, ^, ~, <<, >>
- Assignment operators: =, +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=
Control Structures
Control structures help you control the flow of your program. Some essential control structures in C are:
- if, else: used for decision making
- for, while, do-while: used for looping
- switch: used for multi-way branching
- break: used to exit a loop or switch statement prematurely
- continue: used to skip the current instance of a loop and proceed to the next one
These basic elements provide the foundation for creating programs in the C language.