echo '' ;
C Programming Language

C – Basic

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 – Basic

Get start

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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 TypeConstant Sub TypeTypeTypeDescriptionExample
Primary ConstantNumeric Constant (Three Type)IntegerDecimalDecimal constants are 0 to 9 numbers86 , 94 , -133
Primary ConstantNumeric Constant (Three Type)IntegerOctalOctal constants are 0 to 7 numbers. First number should be ‘00137 , -0567 , 034
Primary ConstantNumeric Constant (Three Type)IntegerHexadecimalHexadecimal constants are 0 to 9 and A to F. First number should be start with ‘0x’ or ‘0X’0X73A , 0x89FA
Primary ConstantReal or floating point Constant (Two types)Fractional formFractional formdot points are consider as fractional forms-0.567 , .64 , 24.0
Primary ConstantReal or floating point Constant (Two types)

Exponential formExponential formRules: 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 ConstantCharacter ConstantCharacter ConstantCharacter ConstantCharacter 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 ConstantString constantString constantString constantString constant are come with two double quotes ()“ARUN” – Valid
“2020” – Valid
“A” – Valid
“ABC – Invalid
‘sum’ – Invalid
Secondary ConstantArray
Secondary ConstantPointer
Secondary Constantstructure
Secondary Constantunion
Secondary Constantenum

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

Typecharacter set
Alphabets A, B, …. Y, Z a, b, …. y, z
Digits0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special symbols~ `! @ # $ % ^ & * ( ) _ - + = | \ { } [ ] : ; ” ’ < > , . ? /
White spaceBlank space, Horizontal tab, Carriage return, New line , Form feed.

Reserved Keywords

C89 Reserved Keywords

According to C89 standards c has 32 reserved keywords

autodogotosignedunsigned
breakdoubleifsizeofvoid
caseelseintstaticvolatile
charenumlongstructwhile
constexternregisterswitch
continuefloatreturntypedef
defaultforshortunion

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 on enum 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: 8a – First letter should not be numbers, 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; //here a,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 and double.
  • type modifier (Qualifier) : short , long , signed and unsigned
  • Rules : Every variable should have any one of the following data type

Typesbit SizeByte SizeRange
char (or)
signed char
81127 to 127
unsigned char810 to 255
int (or)
signed int
162-32,767 to 32,767
unsigned int1620 to 65,535
short int (or)
signed short int
81-127 to 127
unsigned short int810 to 255
long int (or)
signed long int
324-2,147,483,647 to 2,147,483,647
unsigned long int3240 to 4,294,967,295
float3243.4E-38 to 3.4E+38
double6481.7E-308 to 1.7E+308
long double80103.4E-4932 to 1.1E+4932
unsigned sort162

The various general types of data are:

General typeGeneral Sub typeData Type
Number type dataInteger Typeint
Number type dataFloat type (Three Types)Float
Number type dataFloat type (Three Types)double
Number type dataFloat type (Three Types)long double
Character type datachar
String type data
Boolean type databool

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.

Escape sequenceExample
\a  – alarm character 
\b  – back space 
\f  – form feed 
\n  – new line 
\r  – carriage return

Carriage return is known as cartridge return and often shortened to CR, <CR> or return.

It is a control character or mechanism used to reset a device position to the beginning of a line of text.

It is closed associate with the line feed and newline concepts.
 
\t  – horizontal tab 
\v  – vertical tab 
\\  – back slash 
\?  – question mark 
\’  – single quote 
\”  – double quote 
\000  – character representation

\xbh – character representation
 
\0  – nul character

Difference between ‘/0’  and ‘\0’  in C programming?‘\0’ – Null terminator in array.
 

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

SpecifierDescription
%iCan be used to input integer in all the supported format.
%csingle character
%ddecimal character
%efloating point value
%ffloating point value
%gfloating point value
%hshort integer
%ldlong integer
%ooctal integer
%sstring
%uunsigned decimal integer
%xhexa decimal integer
%pprint the address of the variable
[...] string which may include white spaces

Little Endian & Big Endian

Next Topic

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading