How to Begin Learning C Programming

How to Begin Learning C Programming

Introduction😊

  • Human beings have interacted with machines through high-level programming (binary language).

  • Hardware- Machine language- Assembly language - High-level languages - Forten, COBOL, Pascal

  • Machine language instructions are difficult to remember so the assembly language is introduced in symbol forms.

  • Symbols in assembly languages are also difficult to remember so high level is introduced such as c languages, c++, C#, java etc.

History of programming language:-

LanguageReleased date
ALGOL1960
BCPL1967
B-19701970
Traditional C1972-73
ANSI C1989
ANSI / ISOC1990
  • C17 is an updated version of C

  • Dennis Ritchie received the Turing Award in 1983.

  • The computer doesn't know English & only knows high-level language which a computer will help

  • There are three types of translators:-

  1. Compiler - Convert high-level to machine language

  2. Interpreter - Convert high-level to low-level

  3. Assembler - Convert assembly code into machine code

Important Points 👇

  • You should include the header file i.e. <stdio.h> in c and then int main- where program execution starts.

  • printf tells print the format.

  • printf information is only under the stdio.h file

  • C provides libraries that include different header files having different purposes

  • #include is a preprocessor

  • C is fast & case sensitive

  • Features in c are simplicity, speed, rich libraries, memory management, modularity, portability, extendibility, and pointers.

  • variables is a place in RAM that holds the values in the program

  • rules for naming c variables

Letter or underscore

Case sensitive

Digits, letters

Bo special symbols allowed

  • Typespacifier - %d to print int value

  • There are 32 keywords & that are reserved

  • Constant - You cannot change the variable

  • Types of constant - Decimal ,Floating point ,Actual ,Hexadecimal ,Character ,String

Basic Structures of a C Program

Documentation - The comment on the programs is not included in the compilation

Link - includes a library we use in our c program

Definition - Specifies the body or implementation of a function or variable

Global declaration - If we declare any data globally we can access it throughout the programs

Main function - Execution starts here

subprograms - Here Functions are used throughout the programs

  • Tokens in C - They are the smallest Units
  1. Keywords - int, char, if, while

  2. Constant- 15,0.26,-22

  3. Strings - “Hello”,“World”

  4. Operators - +,-,=

  5. Identifier - marks, amount

  6. Special symbols - [ ], { }

Datatype

It is a type of data that a variable will hold in the memory given by a user.

Datatype in C

  1. Primitive(already known to the compiler) - int, char, float, double

  2. Derived(Derived from primitive datatype) - array, structure, union, enum, pointer.

DATATYPEMEMORY(BYTES)FORMAT SPECIFIER
singlechar1%c
singleint2 or 4%d
complexlong int4%ld
complexlong long int8%lld
fractionalfloat4%f
complexdouble8%lf
complexlong double16%ld

Operators

It is a special symbol in c that specifies some task

Types of Operator

  1. Arithmetic Operator - performs mathematical operations such as addition, subtraction, multiplication, and division. Ex- + , - , * , / , %

  2. Relational Operator - They are used to compare two quantities or values. Ex - == , !=, >, < ,>= , <=

  3. Logical Operator - They are used when we test multiple conditions to make decisions.Ex- &&(And operator) , ||(Or operator) , !(Not operator).

  4. Assignment Operator - It is used to assign value to a variable. Ex - = ,+= ,-=

  5. Bitwise & Operator - It is used to perform the operations on the data at the bit-level. it consists of two digits, either 0 & 1. Ex - & , | , ^, ~ , << , >>

  6. Increment & Decrement operator - They are unary operators that add or subtract one from their operand Ex - if a=5; ++a; a=6: a++; a=7(In pre-increment operator first it increases the value by 1 then assign the value), --a; a=6: a - -; a=5(In post increment first assign the value then increment by 1)

  7. Conditional Operator (ternary) - It's an operator that is the decision-making operator depends upon the output of the expression. Ex - condition? true statement: false statement

  • Main() is a function, Void is not recommended.

  • Type Specifier - It is a sequence format by initial(%) which is used to specify the type and format of data that tells the compiler what type of data a variable is going to hold.

Data TypeFormatSize(in bytes)
int%d4
char%c1
float%f4
double%lf8
long int%ld4

Precedence of operator

  • It tells which operator is executed first if there is more than one operator in an expression.
OperatorType
( ) [ ] . ->
(++)(- -)(+ -) ! ~ * &unary
* / %arithmetic
+ -arithmetic
<< >>shift
< <= > >=relational
\== !=relational
&bitwise and
^bitwise or
&&logical and
? ;ternary conditional operator
\= += -= *= /= &= ^=assignment
\= <<= >>=

C library function

  • They are predefined functions that are built-in functions already known to the compiler.
  1. stdio.h - printf(), scanf(), gets(), puts() , fopen(), fclose()

  2. math.h - sin() , cos() , pow() , sqrt(), floor() ,ceil()

  3. string.h - strlen(), strcmp() , strcpy()

  4. conio.h - clrscr() , getch() , getche()

  5. time.h - time() , setdate() , getdate()

  • If you want to print the name write it in printf(“”) quotation mark

  • If you want to print integer value use %d

scanf() use to take input from the user use %d for integer value.

Type casting

changes the behaviour of one data type to another data type.

Two types of typecasting

  1. Implicit typecasting - smaller type to larger type

  2. Explicit typecasting - larger type to smaller type

For 1 decimal place write %1d or %1f

Function

  • Input Function - allows you to take input from the user through the keyboard. Ex - getchar(), gets() - use to input characters & strings.

  • Output Function - It prints a message or value of a variable at the output screen. Ex - printf() - 1 word , putchar() - character , puts() - string.

  • Formatted Input - we can design as per our need

Control/ characterExplanation
%ca single character
%da decimal integer
%ian integer
%oan ictal no
%sa string
%xa hexadecimal no

Flow Control

Flow control in the c program describes the order in which statements will be executed at run time.

Three types of flow charts

Decision control

It is executed when the condition is true, otherwise, the else part statements are executed.

Types of decision control statements

  1. if statement - execute for a true statement

  2. if else statement - if execute otherwise else execute

  3. if else if ladder - execute with the condition given multiped

  4. nested if - including if-else statement inside the body of if else statement

  5. switch case - alternate to if else- if ladder statement which allows us to execute multiple operations

Looping control

It allows to execute sequence of statements again & again until the condition becomes false

Types of loop control statement

  1. while loop - If a condition is true & only the body of a loop is executed.

  2. do while loop - always executed after the body of a loop. It is also called an exit-controlled loop.

  3. for loop - It performs only at once

Jumping control

It allows one to jump to another section of the unconditionally when encountered.

Types of jump control are

  1. break - It is used to end the loop immediately after the encounter.

  2. Continue - It allows us to skip a particular statement if we don’t want to write any statement inside a condition.

  3. Goto - It is used to transfer the program control to a predefined label.

Function

a function is a self-contained block of code that is written in a curly { } braces.

Function benefits

  1. To improve the readability of code.

  2. Debugging the code will be easier if you are using functions, as errors can be easily traced.

  3. Reduce the size of the code.

Types of Functions

  1. Library function - already known to the compiler(predefined)

  2. User-defined - created by the user

  • The function has 3 things
  1. function declaration

  2. function call

  3. function definition

  • Classified functions into 4 Types depending upon argument & return type
  1. Function with argument & a return type

  2. Function with argument & no return type

  3. Function with no argument & no return type

  4. Function with no argument & return type

Conclusion
This article delves into the evolution and fundamentals of programming languages, focusing on C. It discusses key concepts like data types, operators, functions, flow control, and C program structure. It also covers the use of libraries, type casting, and functions, including library and user-defined options.

Follow Me On Socials :

LinkedIn

Twitter

GitHub

Like👍| Share📲| Comment💭

Did you find this article valuable?

Support MOHD NEHAL KHAN by becoming a sponsor. Any amount is appreciated!