Input and Output


    

    

Output

C uses printf() to print output text or values to the console. printf() does not insert a new line at the end of the output.

Syntax:   printf("some random text") is used to print text inside the " ".

printf("format specifier", variable_name) is used to print the variable specified. Format specifier is used to specify which data type that you want to use. You can see what is a format specifier in the variable and data type page.

Example:

#include <stdio.h> int main() { int myNum = 19; printf("Hello World!"); printf("%d",myNum); return 0; }

Input

In C, you can use the scanf() function to get user input.

Syntax:   scanf("format specifier", &variable_name)

Format specifier is used to specify which data type that you want to use. You can see what is a format specifier in the variable and data type page. scanf() can also accept multiple inputs.

Example:

#include <stdio.h> int main() { int myNum; char myChar; printf("Input a number and a character:"); scanf("%d %c",&myNum, &myChar); return 0; }

scanf() can also take string input, but it will only accept the string until it sees a white space. To avoid this error, we use fgets(fullName, sizeof(fullName), stdin) instead of scanf(). We can still use scanf("%s", &variable_name) if the string does not contain any white space between the contents.

Example:

#include <stdio.h> int main() { char myName[20]; printf("Input a name:"); fgets(myName, sizeof(myName), stdin); return 0; }

Comments

Comments can be used to explain what does a block of code do. Comments can also be used to prevent the execution of some lines of code. There are two types of comments in C:

  • Single Line Comment
  • Any text between // and the end of the line will not be executed by the compiler.
    Syntax: //some comments here

  • Multiple Line Comments
  • Syntax:

    /* Hello world! Some comment here */

    
    

Example:

#include <stdio.h> int main() { char myName[20]; //this line will not be executed printf("Input a name:"); fgets(myName, sizeof(myName), stdin); /* printf("hello") The console will not show "hello" because the printf("hello") is in the comment and will not Be executed. */ return 0; }

Escape Sequences

Escape sequences are mostly used to control or format the output.

Example:

#include <stdio.h> int main() { printf("Hello,\nWorld!"); /*The output will be Hello, World! Instead of Hello,World! */ return 0; }

Below are the most commonly used escape sequences.

Escape Sequences Description
\n (newline) Insert a new line on the output.
\t (tab) Insert a horizontal tab on the output.
\\ (backslash) Backslash can't be printed directly as it is used for escape sequences in C, so
we have to place another backslash following the backslash used for
an escape sequence. So, to print backslash, we type \\ not \
\' (single quote) Print a single quote in the output
\" (double quote) Print a double quote in the output
\? (question mark) Print a question mark in the output
\b (backspace) Move the cursor one position back in the current line of text.
\a (alert or bell) A beep is generated by the computer on execution.
\r (carriage return) Move the cursor to the beginning of the current line.
\v (vertical tab) Insert a vertical tab on the output.