Starting with C programming offers great opportunities! Let’s kick things off with a brief overview of the basics to get you started:
Remember, mastering programming requires time and practice, so don’t get discouraged if you face difficulties along the way. Keep coding and experimenting, and you’ll improve over time!
Read more: C – BasicContents
- 1 Get start
- 2 Write your first program
- 3 Join a community
- 4 Constants
- 5 Character Set
- 6 Reserved Keywords
- 7 Identifiers
- 8 Variables
- 9 Data types
- 10 C Programming Structure
- 11 Hello World C program
- 12 Escape sequence
- 13 Header files (Two types)
- 14 Command Statements
- 15 Format specifier
- 16 Little Endian & Big Endian
- 17 Next Topic
Get start
- Setup your environment: You need a compiler to write and run C programs. Popular choices include GCC (GNU Compiler Collection), Clang, and Visual Studio for Windows. Install one based on your operating system.
- Choose a text editor or IDE: You can write C code in any text editor, but using an Integrated Development Environment (IDE) can make your life easier. Some popular options include Visual Studio Code, Atom, Sublime Text, and Eclipse.
- Learn the basics: Familiarize yourself with basic C syntax, such as variables, data types, operators, loops (like for and while loops), conditional statements (if-else), functions, arrays, and pointers. These are the building blocks of any C program.
- Compile and run: Save your program with a “.c” extension (e.g., hello.c). Open a terminal or command prompt, navigate to the directory containing your program, and compile it using the compiler you installed. For GCC, the command would be:
gcc hello.c -o hello
This command compiles your code into an executable named “hello”. Then, run the executable:./hello
You should see “Hello, World!” printed to the screen. - Practice: Once you’ve got the basics down, practice writing more complex programs. Try solving small programming challenges or work on projects that interest you.
- Learn from resources: There are plenty of online resources, tutorials, and books available to help you learn C programming. Websites like GeeksforGeeks, Tutorialspoint, and the C programming subreddit can be valuable learning resources.
- Debugging: Learn how to debug your programs when something goes wrong. Understanding concepts like breakpoints, stepping through code, and reading error messages will be immensely helpful.
- Explore more advanced topics: Once you’re comfortable with the basics, you can delve into more advanced topics like memory management, file handling, data structures, and algorithms.
Write your first program
A classic first program is the “Hello, World!” program. It simply prints “Hello, World!” to the screen. Here’s how you can write it:
#include <stdio.h> int main() { printf("Arun E, World!\n"); return 0; }
Join a community
Participating in online forums or joining a local programming group can provide support, encouragement, and opportunities to learn from others.
Constants
- (Two types : Primary, Secondary)
- When the program execution time constants should not change their value (A constant is an entity that doesn’t change)
Constant Type | Constant Sub Type | Type | Type | Description | Example |
---|---|---|---|---|---|
Primary Constant | Numeric Constant (Three Type) | Integer | Decimal | Decimal constants are 0 to 9 numbers | 86 , 94 , -133 |
Primary Constant | Numeric Constant (Three Type) | Integer | Octal | Octal constants are 0 to 7 numbers. First number should be ‘0‘ | 0137 , -0567 , 034 |
Primary Constant | Numeric Constant (Three Type) | Integer | Hexadecimal | Hexadecimal constants are 0 to 9 and A to F. First number should be start with ‘0x’ or ‘0X’ | 0X73A , 0x89FA |
Primary Constant | Real or floating point Constant (Two types) | Fractional form | Fractional form | dot points are consider as fractional forms | -0.567 , .64 , 24.0 |
Primary Constant | Real or floating point Constant (Two types) | Exponential form | Exponential form | Rules: May use mantissa and exponent symbols Should not use .dot point in exponent Should have at-least one digit in exponent | 0.2571e-5, mantissa – 0.2571, exponent – -5 |
Primary Constant | Character Constant | Character Constant | Character Constant | Character constant are come with two single quotes (‘) | ‘A’ – (ASCII-65) ‘O’ – (ASCII-48, EBCDIC-240) ‘a’ – (ASCII-97, EBCDIC-129) ‘z’ – (ASCII-122, EBCDIC-269) % – (ASCII-37, EBCDIC-108) |
Primary Constant | String constant | String constant | String constant | String constant are come with two double quotes (“) | “ARUN” – Valid “2020” – Valid “A” – Valid “ABC – Invalid ‘sum’ – Invalid |
Secondary Constant | Array | ||||
Secondary Constant | Pointer | ||||
Secondary Constant | structure | ||||
Secondary Constant | union | ||||
Secondary Constant | enum |
Floating Points
- floating points may contain . dot point.
- Rule-1: Should use 0 to 9 numbers.
- Rule-2: Dot point may come front or back
- Rule-3: In the case of float numbers, place sign symbols in front of them.
- Example : 537.36, -158.77
How does the system store a negative integer?
- Usually, all the negative integers are store in computers in the form of the two’s complement of the same positive integer.
- Eg: 1011 (-5) Step-1 − One’s complement of 5: 1010, Step-2 − Add 1 to above, giving 1011, which is -5.
Character Set
Type | character set |
---|---|
Alphabets | A, B, …. Y, Z a, b, …. y, z |
Digits | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |
Special symbols | ~ `! @ # $ % ^ & * ( ) _ - + = | \ { } [ ] : ; ” ’ < > , . ? / |
White space | Blank space, Horizontal tab, Carriage return, New line , Form feed. |
Reserved Keywords
C89 Reserved Keywords
According to C89 standards c has 32 reserved keywords
auto | do | goto | signed | unsigned |
break | double | if | sizeof | void |
case | else | int | static | volatile |
char |
| long | struct | while |
const | extern | register | switch | |
continue | float | return | typedef | |
default | for | short | union |
Enum:
- Enumeration (or enum) represents a user-defined data type in C. It primarily assigns names to integral constants, making programs easier to read and maintain
--
or++
are can’t be done onenum
value.
C99 Reserved Keywords
According to C99, C has 5 more reserved keywords
_Bool
_Complex
_Imaginary
inline
restrict
C11 Reserved Keywords,
According to C11, C has 7 more reserved keywords
_Alignas
_Alignof
_Atomic
_Generic
_Noreturn
_Static_assert
_Thread_local
Identifiers
Rules
- Should not use keyword as a identifier.
- First letter should be English word.
- May use Uppercase and Lowercase letters.
- Can use _ underscore as a first letter of Identifier.
- Identifiers are case sensitive(below both identifiers are not same)
Example
- Valid Identifiers:
Sum, basic_pay, a1, Ab, ab.
- Invalid Identifiers:
– First letter should not be numbers,8a
auto
–auto
is a keyword
Variables
- When the program execution time variable may be change their value.(A variable is an entity that does change),
- Example:
Sum
,average
,basic_pay
,basic-pay
,A0
etc (valid variables) - Generally variables are store in different storage class types :
automatic
static
,extern
,register
Declaring a variable
Rules
- should be declared data type of variable.
- data types name should be declared data type’s
- if declare multiple variables in a single data type, separate each by, operator
- Every declaration should end with
;
semicolon
Syntax
- datatype
variable_1
,
, …..variable_2
variable_n
; - Example-1:
int a, b , c;
//herea,b, c
are integer variables - Example-2:
float salary;
//here salary is a floating type variable.
Use of variable declaration
- Compiler can allocate a memory when we declared data type variable
Variable Initializing
- Syntax: datatype variable_name = initial value;
- Example:
int sum = 1
;
Assigning Value to Variable
Assigning Operator = equal is used to assign value to variable in
- Syntax for value :
Variable_name = value;
Ex:x = 20;
- Syntax for another variable:
variable_name = variable
; Ex:y = x;
- Syntax for expressions:
variable_name = expression;
Ex:z = x+y;
Exercise
Q : Which of the following is not a valid variable name in C?
A. 1 a @
B. a 1 2
C. a b 123
D. a b c 123
Answer: Option A (Explanation: First character must be alphabet).
You cannot print (automagically) the type of a variable in C. Variables and their types are only known at compile time.
At runtime, there is no variables (only locations & values) and almost no types:
Data types
- Data types in any of the language means that what are the various type of data the variables can have in that particular language.
- Whenever a variable is declared it becomes necessary to define data type that what will be the type of data that variable can hold.
- basic datatype:
char
,int
,float
anddouble
. - type modifier (Qualifier) :
short
,long
,signed
andunsigned
- Rules : Every variable should have any one of the following data type
Types | bit Size | Byte Size | Range |
---|---|---|---|
char (or)signed char | 8 | 1 | 127 to 127 |
unsigned char | 8 | 1 | 0 to 255 |
int (or)signed int | 16 | 2 | -32,767 to 32,767 |
unsigned int | 16 | 2 | 0 to 65,535 |
short int (or)signed short int | 8 | 1 | -127 to 127 |
unsigned short int | 8 | 1 | 0 to 255 |
long int (or) signed long int | 32 | 4 | -2,147,483,647 to 2,147,483,647 |
unsigned long int | 32 | 4 | 0 to 4,294,967,295 |
float | 32 | 4 | 3.4E-38 to 3.4E+38 |
double | 64 | 8 | 1.7E-308 to 1.7E+308 |
long double | 80 | 10 | 3.4E-4932 to 1.1E+4932 |
unsigned sort | 16 | 2 |
The various general types of data are:
General type | General Sub type | Data Type |
---|---|---|
Number type data | Integer Type | int |
Number type data | Float type (Three Types) | Float |
Number type data | Float type (Three Types) | double |
Number type data | Float type (Three Types) | long double |
Character type data |
| |
String type data | ||
Boolean type data |
|
C Programming Structure
- Pr-processor Commands
- Type definition
- Function prototype – Declare function type and variable passed to functions
- Variable – We must have a main function in every c programs
//pre processor command #include <stdio.h> //variable type definition ("a" is a variable and integer type) int a; //Function prototype ("Func" is a integer type function name with arguments "a", "b" is a variables integer type) int Func(int b, int c); //main function integer type int main() { //"E" Local variable integer type int E; C statements; }
Hello World C program
Code
#include<stdio.h> void main() { printf("ArunEworld"); }
Output : ArunEworld
Note : All the programs are run in gnu gcc of linux.
Escape sequence
Escape sequence is a character constant or string constants, its a non graphical character or printable.
Header files (Two types)
Predefined Header files
- Syntax :
#include<file_name>
(Stored in Specified directories). - If a header file is included with in
< >
then the compiler searches for the particular header file only with in the built in include path.
User defined Header files
- Syntax :
#include “file_name”
(Stored in Locally saved programs). - If a header file is included with in
“ “
, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path.
Command Statements
/* Commands */
– Commands in paragraph// commands
– command in single line
Format specifier
Specifier | Description |
---|---|
%i | Can be used to input integer in all the supported format. |
%c | single character |
%d | decimal character |
%e | floating point value |
%f | floating point value |
%g | floating point value |
%h | short integer |
%ld | long integer |
%o | octal integer |
%s | string |
%u | unsigned decimal integer |
%x | hexa decimal integer |
%p | print the address of the variable |
[...] | string which may include white spaces |
Little Endian & Big Endian
- Explanation: https://aruneworld.com/embedded/little-endian-and-big-endian/
- Code Example: https://aruneworld.com/programming-language/c/c-examples/c-example-little-endian-and-big-endian/