zoukankan      html  css  js  c++  java
  • 输入输出函数

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
        /*
        字符串的赋值:
        给 char* 类型的字符串赋值,可以直接使用 "=" 号
        给 char[] 类型的字符串赋值,需要使用 strcpy 函数
    
      字符串的特点:
        需要明白的一点就是字符串以结尾, 没有就不是字符串
        只要是用双引号括起来的都是字符串
        字符串的本质就是数组(字符数组)
        */
    
        /*
         输入:
            利用scanf函数接收字符串
            利用gets函数接收字符串
            利用fgets函数接收字符串(推荐常用!)
        */
        printf("请输入一个字符串:");
        char name[12];
        //scanf("%s", name);//利用scanf函数接收字符串,会以空格、Tab、回车 作为结束符
    
        //gets(name);//利用gets函数接收字符串,可以输入空格、Tab,以回车作为结束符;
                    //不安全,输入字符串没有长度限制,无限输入会造成内存溢出
    
        fgets(name, 12, stdin);
        //解决了安全性问题
        //fgets(参数1,参数2, 参数3)
        //参数1:保存数据的首位置
        //参数2:保存的长度(包括 '')
        //参数3:stdin(标准控制台输入)
        //接收空格、Tab,以回车作为结束符
    
        printf("name = %s
    ", name);
    
        /*
        输出:
      %s的原理, 从传入的"地址"开始逐个取出, 直到遇到""位置   
    
      如何输出字符串:
            使用printf的%s占位符来输出
            使用puts函数来输出(自动换行,原样输出)
        */
    
        char str[] = "how are you";
        printf("%s
    ", str); // 使用printf的%s占位符来输出
    
        puts(str);//使用puts函数来输出(自动换行,原样输出)
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/nothx/p/8488726.html
Copyright © 2011-2022 走看看