0% found this document useful (0 votes)
4 views6 pages

Study Material C Program Basics

It includes some of C programming common mistakes to be avoided.

Uploaded by

zafarshaikhn336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Study Material C Program Basics

It includes some of C programming common mistakes to be avoided.

Uploaded by

zafarshaikhn336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Study Material

C Programming Basics
____________________________________________________________________________

Topic: Declarations, Input/Output Functions, Header Files

1. Variable Declaration in C

A variable is a named memory location used to store data during program execution.

Syntax:
data_type variable_name;

Examples:

int a; // declares an integer variable


float b; // declares a float variable
char ch; // declares a character variable

Data Type Description Example

int Integer numbers int age = 20;

float Decimal numbers float pi = 3.14;

char Single character char grade = 'A';

double Double precision float double value = 0.123456;


2. Variable Initialization

You can assign a value to a variable at the time of declaration:

Syntax:
variable = value;

Example:
int a = 10;

3. Common Declaration Mistakes


Mistake Example Correct Form

Missing semicolon int a int a;

Missing assignment operator int a 5; int a = 5;

Wrong data type used num a = 10; int a = 10;

Re-declaring same variable int a; int a; int a;

Using a reserved word as name int int = 5; Use another name

4. Input/Output Functions in C

printf() – Used to display output

int a = 5;
printf("Value of a = %d", a);

scanf() – Used to accept input

int a;
scanf("%d", &a); // %d for int, &a is the address of a

Format Specifier Data Type

%d int

%f float

%c char

%s string (char array)

Common scanf() Errors:


Error Example Correct Form

Forgetting & in scanf() scanf("%d", a); scanf("%d", &a);

Wrong format specifier scanf("%f", &a); where a is int scanf("%d", &a);

5. Header Files in C

Header files contain predefined functions (declarations) used in your program.

Header File Purpose Examples of Functions

<stdio.h> Standard Input/Output printf(), scanf()

<conio.h> Console I/O (Turbo C only) clrscr(), getch()

<math.h> Mathematical functions sqrt(), pow()

<string.h> String handling strlen(), strcpy()


6. Common Errors in C Programming

Error Type Example Description

Missing semicolon int a = 5 Every statement must


end with ;

Using undeclared variable x = 5; Variable x must be


declared first

Forgetting & in scanf() scanf("%d", a); Should be scanf("%d",


&a);

Mismatched specifier scanf("%d", &x); where Use %f instead


x is float

Using reserved keywords as int int = 5; Invalid use of keyword


variable names

7. Clarity on Special Functions (Turbo C)

Functio Purpose
n

clrscr() Clears the console screen

getch() Waits for a key press (used to pause)

These are available in <conio.h> and mostly used in Turbo C.

8. Commenting in C

Use comments to explain code (ignored by compiler).

●​ Single-line:

// This is a comment
●​ Multi-line:

/*
This is a multi-line comment
Useful for documentation
*/

9. Rules for Naming Variables

●​ Must begin with a letter or underscore _​

●​ Can contain letters, digits, underscores​

●​ Cannot use C keywords (int, float, etc.)​

●​ Case-sensitive (Score and score are different)​

✅ Valid: marks, _total, num1


❌ Invalid: 1num, float, my-name

Points to remember:

●​ printf() is for output, scanf() is for input.​

●​ %d, %f, %c, %s → match format with variable type.​

●​ Always include the required header files like <stdio.h>.​

●​ Semicolon ; is mandatory after every statement.​


●​ Use comments to make code readable.​

●​ Variables must be declared before use.

●​ Always end a statement with ;​

●​ Use & with scanf() for variables​

●​ Match format specifiers with variable types​

●​ Include proper header files at the top​

●​ Variable names cannot be keywords like int, float, etc.

You might also like